Hi I have to create a program that takes in 2 arrays (each array containing 5 values). The program will check whether the 5 values of array one contains the same values of array 2.
I have done everything to make this program work except ....how write an algorithm that checks whether array 1 & 2 contain the same values. Can you suggest an algorithm?
For example
Example 1:
Enter 5 values:
1 2 3 4 5
Enter 5 values:
5 3 1 2 4
The two arrays contain the same values.Example 2:
Enter 5 values:
1 1 1 1 2
Enter 5 values:
2 2 1 1 2
The two arrays contain the same values.Example 3:
Enter 5 values:
1 3 1 1 2
Enter 5 values:
2 1 1 1 2
The two arrays contain different values.
#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()
{
// don't modify anything in main()
int list1[SIZE], list2[SIZE];
read(list1, SIZE);
read(list2, SIZE);
if (sameValues(list1, list2, SIZE))
cout << "The two arrays contain the same values." << endl;
else
cout << "The two arrays contain different values." << endl;
return 0;
}
// write the body of following functions
void read(int a[], int n)
{
// define the body
cout << "Enter 5 values: " << endl;
for( int i= 0; i < n; i++)
{
cin >> a[i];
}
}
bool sameValues(int a[], int b[], int size)
{
// define the body
bool same_value = true;
for (int i= 0; i < size; i++)
{
if (b[i] == a[0] || b[i] == a[1] || b[i] == a[2] || b[i] == a[3] || b[i] == a[4])
{
same_value = true;
}
else
same_value = false;
}
return same_value;
}
I have tried lots of methods but they dont work, eg
- Add the values of Array A (A) & if it equals the addition of B values they r the same (doesnt work)
- Create a counter which = 5 * 5; check a[0] with b[0] - b[4] if they contain the same values counter--. Thend do the same for a[1], a[2]...a[4]. This doesn't work either
Any advice? :)