好吧我在這裏做錯了什麼?奇怪的陣列輸出
該程序應該讀取20個整數,然後輸出一個不重複的整數數組(每個整數只輸出一次)。
//Program to read 20 integers and return each integer only once (no duplicates).
#include <stdio.h>
int main()
{
int a, b, count=0, temp, array1[20];
printf("Enter 20 array elements between 1 and 10 inclusive\n");
for (a=0; a<20; a++) //Loop to enter 20 elements
{
scanf("%d", &temp);
for (b=0; b<=20; b++) //Loop to test each new element against all previous entered elements
{
if (array1[b] == temp) //If duplicate increment count
{
count++;
}
else if (count == 0 && b == 20) //If there have been no duplicates and 20 numbers have been tested... add entered number to the array
{
array1[a] = temp;
}
}
}
for (a=0; a<20; a++)
{
printf("%d\t", array1[a]);
}
return 0;
}
您的輸入是什麼?你看到的輸出是什麼? – skrrgwasme
如果我輸入例如1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1,2。我得到的輸出爲:4200256,0,24,0,7344944,0,1,0,-1,-1,4200357,0,1,0,4200,233,0,0,24,0 0 –
@JakeRitter請通過編輯而不是在評論中提出問題中的所有信息。 –