如何找到单个成绩的平均值,然后是C中的多个成绩?

时间:2014-02-15 08:17:47

标签: c if-statement while-loop average

我正在努力制作一个程序,该程序将要求获得的成绩,分配的总数,然后平均。他们一直这样做直到他们想停下来,此时他们会输入-1。我不能正确地得到它,它总是以0.00的形式出现,我是C的新手所以我确信这是一个简单的疏忽,谢谢你提供任何帮助。

    #include <stdio.h>
    #include <conio.h>
    int main( void )
   {
    unsigned int counter;
    int grade;
    int total;
    int asavg;
    int asavgv;

    float average;

    total = 0;
    counter = 0;

    printf("%s", "Enter grade, -1 to end: " );
    scanf("%d", &grade );

    printf("Enter total possible: ");
    scanf("%d", &asavg );

    asavgv = grade / asavg;

    while ( grade !=-1) {
        total = total + asavgv;
        counter = counter + 1;

        printf ("%s", "Enter grade -1 to end: ");
        scanf("%d" , &grade);


        printf("Enter total possible: ");
        scanf("%d", &asavg );

        asavgv = grade / asavg;
    }

    if ( counter != 0 ) {

        average = ( float ) total / counter;

        printf("Average is %.2f\n", average );
    }
    else {
        puts("no grades were entered");
    }
     getch();
    }

1 个答案:

答案 0 :(得分:0)

更改

int asavgv;  

double asavgv;  

然后再做

asavgv = grade*1.0 / asavg;  

asavgv = (double)grade / asavg;