Please write these problems in C++ format 35. a) Write a function named all_same
ID: 3626520 • Letter: P
Question
Please write these problems in C++ format
35.a) Write a function named all_same that passes in a vector of integers and returns true if
all values in the vector have the exact same value and
false otherwise. For example, if the
function is passed a vector with the values, 2, 2, 2, 2, then the function should return
However, if the function is passed a vector with the values, 6, 6, 6, 4, 6, 8, 6, then the
function should return
same as all of the other values. In this case there are three different values, 6, 4, and 8.
b) Write a program (main function) that tests your
vector, you may initialize it with any values you choose, but you must give it at least 3
values. This main function must output “
(all_same returns the correct value) or “Failed” if the function does not pass the test
(all_same returns an incorrect value).
Explanation / Answer
#include<iostream>
#include<vector>
using namespace std;
bool all_same(const vector<int>& v)
{
for(int i=0; i<v.size()-1; i++)
if(v[i]!=v[i+1]) return false;
return true;
}
int main()
{
int a[]={2,2,2,2};
vector<int> v(a,a+4);
cout<<all_same(v)<<endl;
return 0;
}