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;

// 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];

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

{
// 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

All 9 Replies

probably the easiest way is to sort both arrays then all you have to do is check then from top to bottom, if any of the elements in array A are not the same as the corresponding element in array B then they are different.

I agree with Ancient Dragon.

Check the sizes of the arrays (if they aren't equal then you can bail right there)

If they are equal then sort them.

Then a basic loop:

``````bool is_equal=false;
for( int i=0; i<array_size; ++i )
{
is_equal=(a[i]==b[i]);
}``````

Thanks for the advice so far but it stll doesn't seem to be working.
I have tried the code in the previous post & that doesn't work.

The algorithm I think I should be using is:- to use a For Loop to check all the numbers(values) of Array A against all the values in Array B & when a value of Array A (say '3') is not found in Array B (there is no '3' in Array B) the for loop terminates & returns Is_equal as false meaning that the values in Array A are not the same as the values in Array B.

Can you help me code this algorithm, pleeeaaase lol.
I have tried so many different ways I have confused myself :(

This code here I made should terminate whenever it finds a number in Array A that does not occur in Array B but doesn't quite seem to be working?

``````bool is_equal= false;

for( int i=0; i < size; i++ )
{
for (int j= 0; a[j] != b[i]; j++)
{
cout << "a[" << j << "] = " << a[j]<< " b[" << i << "]= " << b[i] << endl;
is_equal= true;
}
}

return is_equal;``````

Thanks for the advice so far but it stll doesn't seem to be working.
I have tried the code in the previous post & that doesn't work.
---
This code here I made should terminate whenever it finds a number in Array A that does not occur in Array B but doesn't quite seem to be working?

Why does everyone leave it to us to figure out what your code does wrong? Can't you describe what makes you think it's wrong? When you take a car in to get fixed you never just say "it doesn't work", you describe the problem!!! :icon_rolleyes:

Just use paper and pencil to run through the code. Define `A` to contain 3,5,7,9. Define `B` to contain 3,5,4,6. What happens when `i=0` and `j` starts at 0 until the comparison is true?
Then go to `i=1` , etc.

Yet another approach (undestructive test):

``````bool is1in2(const int* a1, const int* a2, int n)
{
int j;
for (int i = 0; i < n; i++) {
for (j = 0; j < n && a[i] != b[j]; j++)
;
if (j == n)
return false;
}
return true;
}
bool isTheSameContents(const int* a1, const int* a2, int n)
{
return is1in2(a1,a2,n) && is1in2(a2,a1,n);
}``````

Yet another approach (undestructive test):

``````bool is1in2(const int* a1, const int* a2, int n)
{
int j;
for (int i = 0; i < n; i++) {
for (j = 0; j < n && a[i] != b[j]; j++)
;
if (j == n)
return false;
}
return true;
}
bool isTheSameContents(const int* a1, const int* a2, int n)
{
return is1in2(a1,a2,n) && is1in2(a2,a1,n);
}``````

Did you try this one with {1, 1, 2, 2, 2} and {1, 1, 1, 2, 2}?

I didn't try this code at all. After obvious misprint correction ;) of the line #5:

``for (j = 0; j < n && a1[i] != a2[j]; j++)``

it works fine for these arrays.
What's a problem?

It's just a well known feeling I got when I read the code... If I find the time I'll setup a test project and investigate, maybe this is one of the seldom cases where the feeling is wrong

I agree with Ancient Dragon.
Check the sizes of the arrays (if they aren't equal then you can bail right there)
If they are equal then sort them.
Then a basic loop:

``````bool is_equal=false;
for( int i=0; i<array_size; ++i )
{
is_equal=(a[i]==b[i]);
}``````

Your code will check if the corresponding elements of the two arrays are same or not. This is not what OP wants. OP doesn't care about the order of the elements in array.

To OP: as the data is small, use ArkM's solution-
For each i from 0 to n-1
{
for each j from 0 to n-1
{
check if a==b[j]
}
}
where n is the size of array a and b.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.