Why does it output 2 times the results?

Why does it output 2 times the results. Where do i make a mistake?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <stdio.h>
int main(void)
{
	int i, u = 0, x = 0, array[10];
	printf("Enter 10 numbers:\n");
	for (i = 0; i < 10; i++)
		scanf_s("%d", &array[i]);
	goto label2;
	label1:
	x++;
	label2:
	for (i = 0; i < 10; i++)
		if (array[x] == array[i] && x != i)
		{
			printf("%d is equal to %d\n", array[x], array[i]);
			u++;
		}
	if (x == 9) goto label3;
	else goto label1;
	label3:
	printf("Total equal numbers: %d\n", u * 2);
	return 0;
}

Take a look:
http://oi65.tinypic.com/30ivlz8.jpg
Because if array[3] == array[5] then you'll print the message twice, once when x==3 and i==5, and again when x==5 and i ==3.

You don't need to labels. Instead, use another for loop. You can have one for loop inside another:
1
2
3
4
5
for (x=0; x<10; ++x) {
    for (i=0; i<10; ++i) {
        ...
    }
}

Topic archived. No new replies allowed.