查找数组中学生成绩的最大,最小和平均[E]

时间:2017-07-10 04:46:48

标签: c

大家好。请看一下我的编码并帮我解决两件事。

首先,我再试一次'部分应该回到原来的学生人数,而不是转移到下一个学生。我认为我使用错误的循环,但不知道如何解决它。我尝试将循环放在我的'之外(i = 1; i< 11; i ++)',但它没有用。

其次,我的min和avg工作正常,但不是最大。它一直给我一个随机数,我不知道为什么。

非常感谢你。

#include<stdio.h>

void main() {

int i,a[10],max=a[0],min=a[0],avg,sum=0;

    printf("please input student score one at a time.\n");

    for (i=1;i<11;i++) {

        printf("Student %d : ",i);
        scanf("%d",&a[i]);

            while (a[i] > 100 || a[i] < 0) {

                printf("Try again!\n");
                i-1//im trying to keep the same student count after 'try again'
                break;
            }

        if (max < a[i]) max = a[i];
        if (min > a[i]) min = a[i];
        sum+=a[i];

    }

avg=sum/10;

printf("\n***************final result ***************\n");
printf("                The maximum score is %d\n",max);//only my max is broken. min & avg works fine
printf("                The minimum score is %d\n",min);
printf("                The average score is %d",avg);

getch();

}

4 个答案:

答案 0 :(得分:1)

请试试这个

#include <stdio.h>

int main() {

    int i = 0;
    int a[10];
    int max = -1;
    int min = 101;
    int avg = 0;
    int sum = 0;

    printf("please input student score one at a time.\n");

    while(i < 10) {
        printf("Student %d : ",i);
        scanf("%d", &a[i]);
        if(a[i] > 100 || a[i] < 0) {
            printf("Try again!\n");
            continue;
        }

        if (max < a[i]) max = a[i];
        if (min > a[i]) min = a[i];
        sum += a[i];

        i++;
    }

    avg = sum/10;

    printf("\n***************final result ***************\n");
    printf("                The maximum score is %d\n",max);//only my max is broken. min & avg works fine
    printf("                The minimum score is %d\n",min);
    printf("                The average score is %d",avg);

    return 0;
}

答案 1 :(得分:0)

看起来像家庭作业,所以我只提供如何解决问题的提示

  1. 不要递减计数器,只需在while循环中放置printf和scanf,直到最终满足条件。
  2. 您将获得一个随机数,因为您在声明数组a [10]后立即将max和min分配给[0],并且您尚未初始化数组中的任何值。在C中,计算机为阵列分配空间的内存位置可能不一定是空的。因此,您的max从一开始就会获得一个随机值。这也可能发生在min,只是在你的情况下它发生在最大。解决此问题的一种方法是初始化整个阵列。另一种方法是将整数0分配给最大值,将整数100分配给分钟,因为它永远不会低于0或高于100.
  3. 希望这对您的问题有所帮助。

答案 2 :(得分:0)

#include<stdio.h>

void main() {

int i,a[10],max=a[0],min=a[0],avg,sum=0;

printf("please input student score one at a time.\n");

for (i=1;i<11;i++) {

    printf("Student %d : ",i);
    scanf("%d",&a[i]);

        if (a[i] > 100 || a[i] < 0) {

            printf("Try again!\n");
            i--;
            continue;
        }

    if (max < a[i]) max = a[i];
    if (min > a[i]) min = a[i];
    sum+=a[i];

}

avg=sum/10;

printf("\n***************final result ***************\n");
printf("                The maximum score is %d\n",max);
printf("                The minimum score is %d\n",min);
printf("                The average score is %d",avg);


}

this site

中进行测试

答案 3 :(得分:-3)

感谢大家的帮助。我的代码现在看起来更好。我拿出了&#39; = [0]&#39;从最小和最大,并且改变了&#39;而&#39;到&#39;如果&#39;与--i。如果我需要更改,请告诉我。

#include<stdio.h>

void main() {

int i,a[10],max,min,avg,sum=0;

    printf("please input student score one at a time.\n");

    for (i=1;i<11;i++) {

        printf("Student %d : ",i);
        scanf("%d",&a[i]);

        if (a[i] > 100 || a[i] < 0) {

            printf("Try again!\n");
            --i;
            continue;
        }

        if (max < a[i]) max = a[i];
        if (min > a[i]) min = a[i];
        sum+=a[i];

    }

avg=sum/10;

printf("\n***************final result ***************\n");
printf("                The maximum score is %d\n",max);
printf("                The minimum score is %d\n",min);
printf("                The average score is %d",avg);

getch();

}