c) Which searches the first n elements of an array for an element with a given value. If the value is found then the function should return true and also return the index
// of the element in the array. If not found then the function should return false.
The problem is with the "bool isSame(int number,int arraysize)"
it's not returning true false i also want to ask how to print the index.
If someone can help me till tomorrow.
Thanks
#include<iostream>
usingnamespace std;
int minArrayFunc(int arraysize);
int correspondingOnes(int arraysize);
bool isSame(int number,int arraysize);
int main()
{
cout <<"ENTER CHOICE"<<endl;
int choice;
cin >>choice;
switch(choice)
{
case 1 :
cout <<"PART A"<<endl;
cout <<"ENTER array size";
int array_size1;
cin >>array_size1;
minArrayFunc(array_size1);
break; //optional
case 2 :
cout <<"PART B"<<endl;
cout <<"ENTER array size";
int array_size2;
cin >>array_size2;
correspondingOnes(array_size2);
break; //optional
case 3 :
cout <<"PART C"<<endl;
cout <<"ENTER NUMBER AND SIZE"<<endl;
int num,siz;
cin >>num>>siz;
isSame(num,siz);
break; //optional
}
}
int minArrayFunc(int arraysize)
{
int array_[arraysize], minimum, size_, c, location = 1;
cout << "Enter integers" <<endl;
for ( c = 0 ; c < arraysize ; c++ )
{
cin>>array_[c];
}
minimum = array_[0];
for ( c = 1 ; c < arraysize; c++ )
{
if ( array_[c] < minimum )
{
minimum = array_[c];
location = c+1;
}
}
cout <<"Minimum element is " <<minimum;
return 0;
}
// (b) To return a count of the number of corresponding elements which differ in the first n elements of two arrays of the same size.
int correspondingOnes(int arraysize)
{
int arr1[arraysize] ;
int arr2[arraysize] ;
for ( int c = 0 ; c < arraysize ; c++ )
{
cin>>arr1[c];
}
cout <<"SECOND"<<endl;
for ( int c = 0 ; c < arraysize ; c++ )
{
cin>>arr2[c];
}
int unique_ = 1; //incase we have only one element; it is unique!
for(int i = 0; i < arraysize;i++)
{
if(arr1[i]==arr2[i])
continue;
else
unique_++;
}
cout<<"The number of unique elements is "<<unique_ <<endl;
}
// (c) Which searches the first n elements of an array for an element with a given value. If the value is found then the function should return true and also return the index
// of the element in the array. If not found then the function should return false. In these functions incorporate error testing for a number of elements greater than the array size.
//
bool isSame(int number,int arraysize)
{
int array1[arraysize];
cout << "ENTER"<<endl;
for ( int c = 0 ; c < arraysize ; c++ )
{
cin>>array1[c];
}
for(int i=0;i<arraysize;i++)
{
if(number == array1[i])
{
returntrue;
}
else
{
returnfalse;
}
}
}
bool isSame(int number,int arraysize)
{
bool match = false;
int array1[arraysize];
cout << "ENTER" << endl;
for (int c = 0; c < arraysize; c++)
cin >> array1[c];
for (int i = 0; i < arraysize; i++)
{
if (array1[i] == number)
{
cout << "Number matches at index number " << i << endl;
match = true;
}
}
return (match == true) ? true : false;
}