-4

This is a simple code. But I don't understand this. In this code I would like to enter ten numbers by user and reports if any of them match. First enter 10 numbers that assign in i[j]. But variable match carry only 1 number.How could carry 10 numbers. Then if I enter first number 10 then i[j] would be first 10.How could third for loop run if enter 10 first.Because 10+1=11, that greater than 10.

#include<stdio.h>

int main(void)
{
int i[10], j, k, match;

printf("Enter 10 numbers:\n");
for(j=0; j<10; j++) scanf("%d", &i[j]);

for(j=0; j<10; j++){
    match = i[j];

    for(k=j+1; k<10; k++)
        if(match == i[k])
        printf("%d is duplicated\n", match);
}

return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Kawsar_Ahmed
  • 55
  • 1
  • 9
  • draw the action of the for loop on a piece of paper. Go through each iteration carefully to track what is happening in the program. It shouldn't take you long to understand this basic program. – RoadRunner Nov 02 '16 at 12:24
  • and that is why, a debugger (for stepping trough the stages) is the best friend. :) – Sourav Ghosh Nov 02 '16 at 12:27

1 Answers1

0

Please note, the third for loop is nested inside the second loop. So, for each value of j in the other loop body like 0, 1, 2, the inner loop will execute for each time.

If you're bothered that after the first loop, j will be 10, then don't worry, the statement-1 in the second for loop (for(j=0;... part) will re-assign the value to 0, so the second loop will start with a value of 0 for j.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261