Simple if statement question?

Hi,

I am new to C++ and are teaching myself from a book and have come across this small and simple program(Below) and I understand 90% of it but just need someone to clarify the if statement.

Before, you read the code the program is: a program that searches an array of ten integers for duplicates values. Have the program display each duplicate found.

The code that I'm a little stuck on is: if(nums[i] == nums[j]) and the j for loop now I try to work it out for myself but after I go through the program myself once I get lost. I know this program is very simple but sometimes the book I am teaching myself from doesn't explain things very well.

Here is the whole program:

1
2
3
4
5
6
7
8
9
10
11
12
13
  #include <iostream>
using namespace std;

int main() {

	int nums[] = {1, 1, 2, 3, 4, 4, 4, 5, 6, 6 };

	for(int i=0; i < 10; i++)
for(int j=i+1; j<10; j++)
	if(nums[i] == nums[j]) cout << "Duplicate: " << nums[i] << "\n";
return 0;
}


Thanks,

Luke.
Last edited on
Okay, let's focus on this:

1
2
3
4
5
6
int n[] = { 1, 2, 2, 3 };

for(int i = 0; i < 3; i++)
    for(int j = i + 1; j < 4; j++)
        if(n[i] == n[j])
            cout << n[i] << endl;

For clarity's sake, I have reduced the size of the array.

Let's picture this array as follows:

http://oi42.tinypic.com/29di7t2.jpg

Let i be red and j be blue. Then, this is the effect the loops achieve (highlighted squares denote a found duplicate):

http://oi40.tinypic.com/2qlev5i.jpg

Sorry for the links, but this forum apparently doesn't implement image tags.

Just for fun, notice that if n is the size of the array, then this algorithm performs a total of n * (n - 1) / 2 comparisons, thus rendering it an O(n2) algorithm.

There are better alternatives. For example, if loop through the array and use a hash table to both store its elements and check for duplicates, then we'd end up with an O(n) algorithm.
Last edited on
Yes I understand arrays. Its just the for loop for the int value 'j': for(int j=i+1; j<10; j++) and the if: if(nums[i] == nums[j]) .

I understand the rest of the program.

Sorry, I've been told the book I have isn't very good for beginner's even though it is a beginner's guide.

Thanks you for your time. :)
Last edited on
The first parameter of the while loop is the initial value, so for the code you quoted, j starts at whatever i is plus 1. It is still just an integer. nums[j]==nums[i] doesn't mean j must be equal to i, just that those 2 elements have the same value.
Last edited on
Erhm... I'm sure manudude has the right idea, but I'll explain it in my own words.

There are three things going on in a for loop's statement:
1
2
3
4
5
for(
   int j = I + 1; //Create a variable j and initialize it with I + 1. j will act as a counter.
   j < 10; //Keep running iterations (keep looping) while j is less than 10
   j++ //After each iteration (loop), increment j by 1
)

Then, the if statement:
1
2
3
4
5
6
7
/*
   The == operation checks if the two values are equal to each other.
   Here it tests if the integer at index I is equal to the integer at index j.
   If they are equal, print a statement.
*/
if(num[I] == nums[j])
   cout << "Duplicate: " << nums[I] << "\n";


Overall, this code iterates through the array, and then for each value in the array, iterate again through the array--starting one position forward--to find any matches.
Arrhh, see my book doesn't explain it like that. In fact its nothing like the way you explain it. Yeah I couldn't se how the for loop for the j worked first time round.

You all really helped me out, cheers!

Thanks for describing this program in a better way.
I have edited my first reply above.
Thank you
Josue Molina
.

I understand it now, cheers.

Luke.
Topic archived. No new replies allowed.