C 奇怪的阵列输出

C 奇怪的阵列输出,c,C,好吧,我做错了什么 这个程序应该读取20个整数,然后输出一个不重复的整数数组(每个整数只输出一次) //程序读取20个整数,每个整数只返回一次(无重复项)。 #包括 int main() { int a,b,count=0,temp,array1[20]; printf(“输入介于1和10之间的20个数组元素(含1和10)); 对于(a=0;a,您有多个未初始化变量的读取,这是未定义的行为。您还可以访问超出范围的数组 for (b=0; b<=20; b++) ^^

好吧,我做错了什么

这个程序应该读取20个整数,然后输出一个不重复的整数数组(每个整数只输出一次)

//程序读取20个整数,每个整数只返回一次(无重复项)。
#包括
int main()
{
int a,b,count=0,temp,array1[20];
printf(“输入介于1和10之间的20个数组元素(含1和10));

对于(a=0;a,您有多个未初始化变量的读取,这是未定义的行为。您还可以访问超出范围的数组

for (b=0; b<=20; b++)
           ^^
           This will result in b in the range [0..20]
{
    if (array1[b] == temp) //If duplicate increment count
        ^^^^^^^^^
        array1[b] is uninitialized
        and when b is 20 you access out of range
请注意,您从未重置
count
,因此在第一次匹配后,您将不会再次写入数组

顺便说一句,你打印:

 Enter 20 array elements between 1 and 10 inclusive

但您从未对输入值进行任何检查,以使其处于该范围内。

您多次读取未初始化的变量,这是一种未定义的行为。您还可以访问超出范围的数组

for (b=0; b<=20; b++)
           ^^
           This will result in b in the range [0..20]
{
    if (array1[b] == temp) //If duplicate increment count
        ^^^^^^^^^
        array1[b] is uninitialized
        and when b is 20 you access out of range
请注意,您从未重置
count
,因此在第一次匹配后,您将不会再次写入数组

顺便说一句,你打印:

 Enter 20 array elements between 1 and 10 inclusive

但您从未对输入值进行任何检查,以使其在该范围内。

这里有以下错误

  • 在内部循环中,在第一次检查期间,您将与20个元素进行比较。在接收到第一个元素时,您没有任何要比较的元素。我添加了一个变量
    size
    ,以指示数组的大小。
    size
    初始化为0

  • if(count==0&&b==20)
    应该移到for循环之外,并且可以简化为
    if(count==0)

  • 当一个元素被添加到数组中时,它被添加到
    array1[size]
    并且
    size
    被递增

  • 您需要在每个外部for循环重新初始化
    count
    ,如下所示

  • 打印将打印不重复的
    大小
    元素

  • 代码如下

    //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];
    
        int size = 0;
        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);
            count = 0;
            for (b=0; b<size; b++) //Loop to test each new element against all previous entered elements
            {
                if (array1[b] == temp) //If duplicate increment count
                {
                    count++;
                }
            }
            if (count == 0) //If there have been no duplicates and 20 numbers have been tested... add entered number to the array
            {
                array1[size] = temp;
                size++;
            }
        }
        for (a=0; a<size; a++)
        {
            printf("%d ", array1[a]);
        }
        return 0;
    }
    
    //程序读取20个整数,每个整数只返回一次(无重复项)。
    #包括
    int main()
    {
    int a,b,count=0,temp,array1[20];
    int size=0;
    printf(“输入介于1和10之间的20个数组元素(含1和10));
    
    对于(a=0;a,这里有以下错误

  • 在内部循环中,在第一次检查期间,您将与20个元素进行比较。在接收到第一个元素时,您没有任何要比较的元素。我添加了一个变量
    size
    ,以指示数组的大小。
    size
    初始化为0

  • if(count==0&&b==20)
    应该移到for循环之外,并且可以简化为
    if(count==0)

  • 当一个元素被添加到数组中时,它被添加到
    array1[size]
    并且
    size
    被递增

  • 您需要在每个外部for循环重新初始化
    count
    ,如下所示

  • 打印将打印不重复的
    大小
    元素

  • 代码如下

    //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];
    
        int size = 0;
        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);
            count = 0;
            for (b=0; b<size; b++) //Loop to test each new element against all previous entered elements
            {
                if (array1[b] == temp) //If duplicate increment count
                {
                    count++;
                }
            }
            if (count == 0) //If there have been no duplicates and 20 numbers have been tested... add entered number to the array
            {
                array1[size] = temp;
                size++;
            }
        }
        for (a=0; a<size; a++)
        {
            printf("%d ", array1[a]);
        }
        return 0;
    }
    
    //程序读取20个整数,每个整数只返回一次(无重复项)。
    #包括
    int main()
    {
    int a,b,count=0,temp,array1[20];
    int size=0;
    printf(“输入介于1和10之间的20个数组元素(含1和10));
    
    对于(a=0;a你的输入是什么?你认为输出是什么?如果我输入例如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,4200357,0,1,0,4200 233,0,0,0,24,0@JakeRitter请通过编辑问题而不是注释将所有信息放入问题中。
    if(array1[b]==temp)
    此时,您没有在数组1中放入任何内容。它只包含垃圾。您需要以某种方式对其进行初始化,或者跟踪它包含多少有效项。您的输入是什么?您看到的输出是什么?如果我输入例如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, 0, 24, 0@JakeRitter请通过编辑而不是注释将所有信息放入问题中。
    if(array1[b]==temp)
    此时,您没有在array1中放入任何内容。它只包含垃圾。您需要以某种方式对其进行初始化,或者跟踪它包含的有效项的数量。