【问题标题】:Using Arrays, Loops, and conditions in C programming在 C 编程中使用数组、循环和条件
【发布时间】:2018-01-29 03:09:24
【问题描述】:

我要解决的问题:

  • 向用户询问五个数字并将其存储在一个数组中。
  • 确定输入的最大数字和数组的平均值
  • 将使用 for 循环来解决此问题。

我为解决这个问题而编写的代码,但我不知道为什么会这样 不工作。有人可以告诉我我做错了什么吗?

#include <stdio.h>

  int main()
   {
     int i, n;
     float arr[5], sum = 0, average;

     printf("Please enter five numbers, separated by spaces!\n");
     scanf("%d", &n);

     //Stores numbers entered into an array
     for(i = 0; i < n; i++)
     {
        printf("%d: ", i++);
        scanf("%d", &arr[0]);


        for(i = 1; i < n; i++)
        {
            if(arr[0] < arr[i])
                arr[0] = arr[i];
        }
        printf("The highest of the five numbers is %d\n", arr[0]);

        for(i = 0; i < n; i++)
        {
            printf("%d. Enter number: ", i+1);
            scanf("%f", &arr[i]);
            sum += arr[i];
        }

        average = sum / n;
        printf("The average of the five numbers is %f\n", average);
      }
    }    

【问题讨论】:

  • 欢迎来到 Stack Overflow。请使用tour 并阅读How to Ask。来吧,只是在代码的 cmets 中输入代码并提出问题是低于任何标准的。请以可读的方式编辑您的问题。向我们表明您至少愿意花时间正确提出问题。
  • 是的,如果不出意外,请准确解释您的代码是如何失败的。

标签: c arrays loops


【解决方案1】:
#include <stdio.h>

  int main()
   {
     int i, n;
     float sum = 0, average;

     printf("Please enter five numbers, separated by spaces!\n");
     scanf("%d", &n);

     float arr[n];

     // no need to do i++ inside
     //instead of saving all the values in arr[0], save it in arr[i]
     //for loop needs to be closed here itself,which you had not done
     for(i = 0; i < n; i++)
     {
       // printf("%d: ", i++); //no need to do this
        scanf("%f", &arr[i]);
     }
    //initialize a max variable equal to first element of array
     float max=arr[0];

      //compare the max element with rest of the elements of the array and update it as you get any greater element than it
        for(i = 1; i < n; i++)
        {
            if(max < arr[i])
                max = arr[i];
        }

        //use %f format specifier for float
        printf("The highest of the five numbers is %f\n", max);

        for(i = 0; i < n; i++)
        {
            //no need to do here what u did since the value is already stored in array
    //this for loop sums all the values present in array
            sum += arr[i];
        }

       //takes out average and prints it
        average = sum / n;
        printf("The average of the five numbers is %f\n", average);

    }    

我已经指出了您在 cmets 中的错误。希望有帮助!

【讨论】:

  • 你还应该指出,如果用户输入6或更多,代码会产生缓冲区溢出。
【解决方案2】:

这是你可以做的,

  • 您可以从用户那里获取您的数组大小,并在 & for 循环中使用一个变量来帮助输入数组元素。那主要是你做错了。

这是正确的代码,

#include <stdio.h>
int main()
   {
     int i, arrsize; //arrsize variable declared to store array size
     float arr[5], sum = 0, average;
     printf("Enter the size of the array : ");
     scanf("%d",&arrsize);

 printf("Please enter %d numbers, separated by spaces!\n",arrsize);

 for(i = 0; i < arrsize; i++)
 {
    scanf("%f", &arr[i]);
 }
 float max=arr[0];

    for(i = 1; i < arrsize; i++)
    {
        if(max < arr[i])
            max = arr[i];
    }
    printf("The highest of the five numbers is %f\n", max);

    for(i = 0; i < arrsize; i++)
    {
        sum += arr[i];
    }
    average = sum / arrsize;
    printf("The average of the five numbers is %f\n", average);

}

您在代码的第一次 scanf 中犯了另一个错误,您能说出您想使用该语句输入的具体内容吗?我的意思是如果它是一个数组,你显然需要循环来获取输入。所以第一个 scanf 是毫无价值的(你的代码)。

希望对您有所帮助。期待您的回复。

快乐编码。

【讨论】:

    【解决方案3】:

    试试这个代码。我也附上代码的输出。

    #include <stdio.h>
    
      int main()
       {
         int i, n=5;
         float arr[5], sum = 0.0, average,max;
    
    
         //Stores numbers entered into an array
         for(i = 0; i < n; i++)
         {
            printf("Enter value # %d :", i+1);
            scanf("%f", &arr[i]);
         }
    
         max=arr[0];
            for(i = 1; i < n; i++)
            {
                if(max < arr[i])
                    max = arr[i];
            }
            printf("The highest of the five numbers is %f\n", max);
    
            for(i = 0; i < n; i++)
            {
    
                sum += arr[i];
            }
    
            average = sum / n;
            printf("The average of the five numbers is %f\n", average);
    
        }    
    

    【讨论】:

    • 不要张贴文本输出的图片,张贴正确缩进4个空格的文本本身。 (它加起来,文本约 250 字节,图像 2500 字节)
    猜你喜欢
    • 2017-09-16
    • 2016-01-28
    • 1970-01-01
    • 2014-11-19
    • 2014-02-02
    • 1970-01-01
    • 2013-03-14
    • 1970-01-01
    • 2021-11-29
    相关资源
    最近更新 更多