Array matching

I am making a program.
Last edited on
You have to decide what you mean by match. Let's assume that you mean that they must be the same size and have the same element at each position.

Then you have to go thru your loop and compare each element. If at least one does not match, then they are different.
1
2
3
4
5
6
7
8
9
10
11
	bool match = true; // assume the entries match to begin with
	for (int i = 0; match && i < 6; ++i) // loop while entries match
	{
		if (Array1[i] != Array2[i])
			match = false; // clear the flag if the entries don't match
	}

	if (match)
		cout << "Arrays match" << endl;
	else
		cout << "Arrays don't match" << endl;
Yeah that what I was referring to, sorry. I want to see if the two match. Could you use a Do While loop instead? Sorry again I'm kind of new to C++.
@kbw
Also do the statements "++i" and "i++" have the same meaning?
They almost have the same meaning.
1
2
int x = 1, i = 5;
x = ++i;

'i' is incremented before assigning to x so that x = 6 and i = 6.
1
2
int x = 1, i = 5;
x = i++;

'i' is incremented after assigning to x so that x = 5 and i = 6.

Yeah that what I was referring to, sorry. I want to see if the two match. Could you use a Do While loop instead? Sorry again I'm kind of new to C++.


If you're new, then you need to learn how to use loops properly, and a for loop is definitely best in this situation.
@kbw
Shouldn't there be a bracket around it?
Last edited on
I haven't gotten it to work.
You only need brackets if there are multiple lines included in the if statement. The same applies to loops as well.

Post everything you have so far.
I get a "1 unresolved externals" error. Do I have to have a different header?
Last edited on
I'm not sure what the error means, but there are a couple things wrong:
1. You need return 0; before your last closing brace
2. match is declared twice

What exactly don't you understand about for loops?
Topic archived. No new replies allowed.