GP中的GPA计算器帮助

时间:2015-10-11 12:10:34

标签: c

我目前正在学习C,在一章结尾处的一个挑战问题是我建立了一个GPA计算器。这是挑战:

  

创建学生GPA平均值计算器。该程序应提示用户输入最多30个GPA,这些GPA存储在一维数组中。每次他或她输入GPA时,用户都应该可以选择计算当前的GPA平均值或输入另一个GPA。该计划的样本数据:

     

GPA: 3.5
  GPA: 2.8
  GPA: 3.0
  GPA: 2.5

     

提示:小心不要将空数组元素计算到学生的GPA平均值中。

我让程序看起来有些工作,但是当它计算GPA平均值时,它计算错误。有人请看看我的源代码,让我知道我做错了什么?

源代码

#include <stdio.h>

int main() 
{
    float fGrades[30] = {0};
    float fAverage = 0;
    int x;
    int y = 0;
    float fSum = 0;
    char cResp = '\0';

    printf("\nGPA Calculater");
    printf("\nYou can enter up to 30 grades");

    do 
    {
        printf("\nPlease enter a grade and press enter: ");
        scanf("%f", &fGrades[x]);

        printf("\nDo you want to calculate the GPA? (Y or N): ");
        scanf("\n%c", &cResp);

    } while (x < 30 && cResp != 'Y' && cResp != 'y');//end do while loop

    for (x = 0; x < 30; x++) 
    {
        fSum += fGrades[x];
    }//end for loop

    fAverage = fSum / x;

    printf("\nYour final GPA is: %.2f\n", fAverage);
    return 0;
}//end main function

我得到的输出如下:

GPA Calculater
You can enter up to 30 grades
Please enter a grade and press enter: 3.22

Do you want to calculate the GPA? (Y or N): n

Please enter a grade and press enter: 3.13

Do you want to calculate the GPA? (Y or N): n

Please enter a grade and press enter: 2.89

Do you want to calculate the GPA? (Y or N): n

Please enter a grade and press enter: 3.05

Do you want to calculate the GPA? (Y or N): y

Your final GPA is: 0.10

5 个答案:

答案 0 :(得分:3)

一些建议:

  • x初始化为0
  • 在初始的do / while循环中,在数组中的scanf&#39; d之后增加x
  • 在计算fSum的循环中,使用其他计数器并从counter = 0; counter < x; ++counter开始并使用fSum += fGrades[counter];

答案 1 :(得分:0)

你的问题是,你总是总结30个元素无论什么

您应该只添加用户输入的项目。

类似于:

#include <stdio.h>

int main() 
{

float fGrades[30] = {0};
float fAverage = 0;
int x;
int y = 0;
int counter = 0;
float fSum = 0;
char cResp = '\0';

printf("\nGPA Calculater");
printf("\nYou can enter up to 30 grades");

do 
{
    printf("\nPlease enter a grade and press enter: ");
    scanf("%f", &fGrades[counter]);
    counter++;

    printf("\nDo you want to calculate the GPA? (Y or N): ");
    scanf("\n%c", &cResp);

} while (counter < 30 && cResp != 'Y' && cResp != 'y');//end do while loop

for (x = 0; x < counter; x++) 
{
    fSum += fGrades[x];
}//end for loop

fAverage = fSum / x;

printf("\nYour final GPA is: %.2f\n", fAverage);
return 0;
}//end main function

答案 2 :(得分:0)

更改计算fSum的循环以使用另一个变量而不是x

int i;
for ( i = 0; i < x; i++) 
{
    fSum += fGrades[ i ];
}//end for loop

答案 3 :(得分:0)

有多个问题 1)x未初始化,因此在do-while中扫描可能会导致代码崩溃 2)在do-while中没有增加x 3)在计算平均值时,您应该仅对输入的值求和,而不是全部30个值,否则您的平均值将是错误的

修改后的代码如下,请检查

int main() 
{

float fGrades[30] = {0};
float fAverage = 0;
int x;
int y = 0;
float fSum = 0;
char cResp = '\0';

printf("\nGPA Calculater");
printf("\nYou can enter up to 30 grades");
x = 0;
do 
{
    printf("\nPlease enter a grade and press enter: ");
    scanf("%f", &fGrades[x]);

    printf("\nDo you want to calculate the GPA? (Y or N): ");
    scanf("\n%c", &cResp);
    x++;
} while (x < 30 && cResp != 'Y' && cResp != 'y');//end do while loop

for (y = 0; y < x; y++) 
{
    fSum += fGrades[y];
}//end for loop

fAverage = fSum / x;

printf("\nYour final GPA is: %.2f\n", fAverage);
return 0;
}//end main function

答案 4 :(得分:0)

您可以删除'for'循环来计算所有值的总和。您可以计算'do while'循环的总和。它将使您的代码更有效。

#include <stdio.h>

int main() 
{

float fGrades[30] = {0};
float fAverage = 0;
int x;
int y = 0;
float fSum = 0;
char cResp = '\0';
int count = 0;

printf("\nGPA Calculater");
printf("\nYou can enter up to 30 grades");

do 
{
    printf("\nPlease enter a grade and press enter: ");
    scanf("%f", &fGrades[x]);
    fSum += fGrades[x];
    count++;

    printf("\nDo you want to calculate the GPA? (Y or N): ");
    scanf("\n%c", &cResp);

} while (x < 30 && cResp != 'Y' && cResp != 'y');//end do while loop

fAverage = fSum / count;

printf("\nYour final GPA is: %.2f\n", fAverage);
return 0;
}//end main function