comparing arrays

I have a program that takes an array of numbers A, and shuffles them and saves that value into array B.
I need to find how many steps it would take to return the shuffled numbers to their original conditions.
but I am unsure of how to compare array A to array B
I tried writing a program that returns yes or no depending if the arrays have the same elements but it does not work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;

const int SIZE = 1000;

int main () {

	int A[SIZE] = {1,2,3};
	int B[SIZE] = {2,3,4};
	int C[SIZE] = {1,2,3};
	const int i = 5;
	if (C[i] = A[i])
		cout << "yay" << endl;
	else
		cout << "nay" << endl;
	return 0;
}

arrays C and A have the same values in same placs but it keeps returning "nay"
Last edited on
You're looking for if (C[i] == A[i])

Also A[i] is past the defined elements of the array. A only contains [0] [1] [2], not [5]. Same for C.
Last edited on
I did that, and it keeps returning "yay" even if I change the values of A to lets say {1,3,5}
I need to compare every single value that is in array A to every single value that is in C, not just the number of numbers in the array
const int i = 5;
you know u just need to put those const int within the square braces ONLY for the declaration of arrays... aftr that in the above code i posted(from ur program!) u can just put int i=5, just in case u needed to change the value of i in the program... as it represents index, it can change in the program!
well i think this wud have a skeleton?
1
2
3
4
5
6
7
8
9
10
int same=0,diff=0;
for(int i=0;i<5;i++)
{
  if(C[i]==A[i])
  ++same;
  else
  ++diff;
}
cout<<"Number os same elements : "<<same;
cout<<endl<<"Number of different elements : "<<diff;

well i didnt understand ur qn well mayb... if u want to shuffle the array just use a loop for shuffling...
I did this. This works but prints out 3 yays or nays. how can I modify this so that it prints out "nay" if even one element does not match? and "yay" only if all of them match?
1
2
3
4
5
6
7
8
9
10
11
              int A[3] = {1,2,3};
	int B[3] = {2,3,4};
	int C[3] = {1,2,3};

	for(int i = 0; i < 3; i++)
	{
		if (C[i] == A[i])
		cout << "yay" << endl;
	else
		cout << "nay" << endl;
	}
SwatSid gave you the answer by counting the number of same. The only difference from his code is you want the following for lines 9 and 10.

1
2
3
4
if (same == 3)
  cout << "yay" << endl;
else
  cout << "nay" << endl;


Of course, this is only counting the number the same in A and C.
Not sure what you want to do with B. Count A nd B? B and C?
Got it thanks guys!
It is now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;

const int SIZE = 1000;
const int n = 3;

void totalShuffles (int A[], int B[], int n) {

int same = 0;
int diff = 0;
for(int i=0; i<n; i++)
{
  if(B[i] == A[i])
  ++same;
  else
  ++diff;
}
if (same == 3)
  cout << "yay" << endl;
else
  cout << "nay" << endl;

}
/*
void equal_arrays(int A[], int B[], int n) {
    int i, result;
        for (i=0; i<n; ++i){
            if (A[i] == B[i])
				cout << "yay";
				else 
				cout << "nope";
		}			
}
*/

int main () {

	int A[SIZE] = {1,2,3};
	int B[SIZE] = {1,2,4};
	
	totalShuffles(A, B, n);
	
	return 0;
}

I will now have to modify this to fit my other program, but since i have the basics I should not have any problem doing so.
Topic archived. No new replies allowed.