user2985083
user2985083

Reputation: 59

Understanding the logic of nested for-loops

I wrote a program where the user inputs numbers n times; the program prints out the the numbers removing all duplicates.

On the code you will see that I initialized seen = 0 at the beginning of the code and when I do so without initializing again on my for loop It won't print out the numbers correctly but when I do it will. Can somebody explain why? I would really like to understand this. I clarified my question on the code itself. Have a look:

 #include <stdio.h>

int main()
{
    int arr[100];
    int i, j, num, seen = 0; // here is where I first initialized it
                             // but I see that this is not necessary

    printf("Enter Number: ");
    scanf("%d", &num);

    for (i=0; i<num; i++)
    {
        printf("Arr[%d] ", i);
        scanf("%d", &arr[i]);
    }

    for (i=0; i<num; i++)
    {
         //seen = 0; If I initialize it here
         //          it will print out the numbers correctly
        for (j=0; j<i; j++)
       {
           if (arr[i]==arr[j])
           {
               seen = 1;
               break;
           }
       }
       if (!seen)
           printf("%d", arr[i]);
    }
    printf("\n");

}

Upvotes: 0

Views: 1086

Answers (3)

Keith Thompson
Keith Thompson

Reputation: 263177

Think about what the value of your seen variable means.

You iterate over the array of numbers. For each number, you do the following:

  • Loop over the previous numbers in the array, and check whether the current number has already been seen.
  • Print the current number only if it hasn't already been seen.

seen should be true only if you've already seen the current number. The way your program is currently written, it remains true as long as the program continues to run, though the old value becomes irrelevant as soon as the outer loop advances to a new number.

In fact, it would be even better to move the declaration of seen (along with its initialization to 0 inside the outer loop. Since its value is not meaningful outside one iteration of the outer loop, it might as well only exist within each iteration of the outer loop.

(Running your program under a source level debugger such as gdb is one way to find errors like this, but not the only one. Re-reading your code and reasoning about what it does vs. what it should do is another useful approach. Adding printf calls to show the values of variables as the program runs is another.)

Upvotes: 1

JB0x2D1
JB0x2D1

Reputation: 920

If you don't reset seen=0 in the outer loop, the program thinks that it has seen every one after the first one that it finds.

Googling gdb tutorial yielded this that looks promising.

Upvotes: 2

Zan Lynx
Zan Lynx

Reputation: 54325

Please learn about debugger programs.

In an IDE like Visual Studio or Eclipse it is built in.

Using GCC on Linux command line it is called gdb. You compile your program with the -g flag to make GCC include debug information. Then you run gdb program-name and start and n, then hit return as it steps through the program. There are also many other commands.

With that you should be able to solve this problem and many others.

Upvotes: 0

Related Questions