CODE
#include <iostream>
using namespace std;
void read(int a[], int n);
// read the first n elements of array a
bool sameValues(int a[], int b[], int size);
// compare two arrays of the same size
// It returns true if a and b contain the same values
const int SIZE = 5;
int main()
{
int list1[SIZE], list2[SIZE];
read(list1, SIZE);
read(list2, SIZE);
if (sameValues(list1, list2, SIZE))
cout << "The two arrays contain exactly the same values." << endl;
else
cout << "The two arrays contain different values." << endl;
cout << endl;
system("pause");
return 0;
}
void read(int a[], int n)
{
int counter;
int list1[SIZE];
cout << "Enter 5 values:" << endl;
for (counter = 0; counter < 5; counter++)
{
cin >> list1[SIZE];
}
}
bool sameValues(int a[], int b[], int size)
{
int list1[SIZE], list2[SIZE];
if (list1[SIZE]=list2[SIZE])
return true;
else return false;
}
My program is suppose to do that following:
1. write a C++ function void read(int a[], int n) to read the first n values for array a
2. write a C++ 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.
My output is:
Enter 5 values:
1 2 3 4 5
Enter 5 values:
1 2 3 4 5
The two arrays contain exactly the same values.
Press any key to continue . . .
But if it has different numbers it outputs:
Enter 5 values:
1 1 1 1 2
Enter 5 values:
2 1 1 1 2
The two arrays contain exactly the same values.
Press any key to continue . . .
But should be:
Enter 5 values:
1 1 1 1 2
Enter 5 values:
2 1 1 1 2
The two arrays contain different values.
Press any key to continue . . .
It just outputs that "The two arrays contain exactly the same values." even though the five integers are all different. What's wrong? thx in advance!