c - 我的输出没有正确显示

标签 c

如果我的问题格式不正确,我深表歉意。我是这个网站的新手,也是编程新手。

我目前正在做一项 C 作业,我相信我已经完成了大部分代码,但有一些调整我似乎无法弄清楚。如果有任何反馈,我将不胜感激。这是我的代码

#include <stdio.h>
#include <stdlib.h>
#define SENTINAL -1

double sumOfScores = 0;
double examScore = 0;
double sumOfExams = 0;
double average = 0;
double calculateAverage();

double main(void)
{
    int i;
    for (i = 1; i <= 4; ++i)
    {
        calculateAverage();
    }
    return 0;
}

double calculateAverage()
{
    printf("Enter %d to terminate program. \n", SENTINAL);
    while(examScore != SENTINAL)
    {
        printf("Enter test score: \n");
        scanf("%lf", &examScore);
        sumOfScores += examScore;
        sumOfExams++;
        average = sumOfScores / sumOfExams;

    }
    printf("The average of the test scores entered thus far is %.2lf  \n\n", average);

return 0;
}

这是我的输出

Enter -1 to terminate program. 
Enter test score: 
99
Enter test score: 
98
Enter test score: 
97
Enter test score: 
96
Enter test score: 
-1
The average of the test scores entered thus far is 77.80  

Enter -1 to terminate program. 
The average of the test scores entered thus far is 77.80  

Enter -1 to terminate program. 
The average of the test scores entered thus far is 77.80  

Enter -1 to terminate program. 
The average of the test scores entered thus far is 77.80  

这就是我想要的样子

Enter -1 to terminate program. 
Enter test score: 
99
Enter test score: 
98
Enter test score: 
97
Enter test score: 
96
Enter test score: 
-1
The average of the test scores entered thus far is 77.80  

Enter -1 to terminate program. 
Enter test score: 
95
Enter test score: 
94
Enter test score: 
93
Enter test score: 
92
Enter test score: 
-1
The average of the test scores entered thus far is (avg goes here)

我没有在我想要的输出中包含额外的两组数字,但我希望能够使用四组数字来完成此操作。一旦我输入 (-1) 来终止第一组数字,它就会自动为我吐出剩余 3 组第一组的平均值,然后我才能输入我想要输入的数字。另外,为什么它给我的第一组值的平均值为 77.8,而它应该在 90 年代上升?

最佳答案

我建议使用局部变量而不是全局变量。也就是说,移动这些行:

double sumOfScores = 0;
double examScore = 0;
double sumOfExams = 0;
double average = 0;

这里:

double calculateAverage()
{
    double sumOfScores = 0;
    double examScore = 0;
    double sumOfExams = 0;
    double average = 0;
    // ...

这将导致每次函数启动时变量都重置为 0,而不是留下上次函数运行时的垃圾。

我认为您得到错误平均值的原因是您将 -1 作为测试分数之一。您读取该值,然后将其添加到平均值,然后再检查该值是否为 -1

printf("Enter test score: \n");
scanf("%lf", &examScore);
// Is examScore equal to -1 here? It might be.
// Don't add it to sumOfScores without checking!
sumOfScores += examScore;
sumOfExams++;
average = sumOfScores / sumOfExams;

您需要在重新计算平均值之前测试该值是否为 -1,或者需要重新构建循环,以便 examScore != SENTINAL 检查为在读取和重新计算平均值之间完成。

此外,严格来说,没有必要在循环仍在运行时进行所有平均计算。您可以保存 average = sumOfScores/sumOfExams; 行,直到循环完成。只是一个想法。

正如 Paul R 所说,您的 main 函数的函数原型(prototype)也不正确。可以找到 main 函数的有效原型(prototype) here

关于c - 我的输出没有正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37722117/

相关文章:

c - pthread_join 错误

c - 将文件读入缓冲区,但调用 fseek 后程序在 fread 处崩溃

c++ - 具有 "aggressive"可变语义的 C 和 C++ 编译器

c++ - TTF_RenderText_Solid 使我的 ram 增加到 2GB 并返回 NULL

CGI 不会运行只是在 Apache 服务器上下载

c++ - GCC中__attribute__((unused))和__attribute((unused))的区别

c - 在 Windows 操作系统中使用 C 运行 GTK 库的任何方式?

c - AGGREGATE 中的最终函数不会在 PostgreSQL 中运行

c - 为什么宽字符问题?

c - pthread,带有双指针的链表