Monday, July 27, 2009

Need help with c++ on arrays - comparing lists of 5 integers?

I have to write two functions, one void function to to read the first n values for array a which I have done and a function


"bool sameValues(int a[], int b[], int size) "that returns true if arrays a and b of the same size contain exactly the same values, otherwise false.


This is what I have written


#include %26lt;iostream%26gt;


using namespace std;





void read(int a[], int n);


bool sameValues(int a[], int b[], int size);


const int SIZE = 5;


int main()


{


int list1[SIZE], list2[SIZE];


read(list1, SIZE);


read(list2, SIZE);


if (sameValues(list1, list2, SIZE))


cout %26lt;%26lt; "The two arrays contain exactly the same values." %26lt;%26lt; endl;


else


cout %26lt;%26lt; "The two arrays contain different values." %26lt;%26lt; endl;


cout %26lt;%26lt; endl;


system("pause");


return 0;


}


void read(int a[], int n)


{


int i;


cout %26lt;%26lt; "Enter " %26lt;%26lt; n %26lt;%26lt; " integers into the array." %26lt;%26lt; endl;


for (i = 0; i %26lt; n; i++)


cin %26gt;%26gt; a[i];


}


bool sameValues(int a[], int b[], int size)

Need help with c++ on arrays - comparing lists of 5 integers?
bool sameValues(int a[], int b[], int size)


{


int i;


if(length(a) = =length(b))


{


for (i = 0;i%26lt;length(a)+1 ; i++)


if(a[i]!=b[i])


return 0;


}


else


return 0;





return 1;


}





if function returns 0 then they both are not equal


if function returns 1 then they are equal


take the return value into a variable for checking.


No comments:

Post a Comment