0

I apologize if I'm not formatting my question correctly. I am new to this site and new to programming.

I'm currently working on a C assignment and I believe I have most of the code done, but there is some tuning I can't seem to figure out. I would appreciate any feedback. Here is my code

#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;
}

Here is my output

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  

Here is what I would like it to look like

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)

I did not include an additional two sets of numbers in the output I am going for, but I would like to be able to do this with four sets of numbers. As soon as I enter (-1) to terminate the first set of numbers, it automatically spits me out the average of the first set for the remaining 3 sets before i can even input the numbers I would like to enter for those. Also, why is it giving me an avg of 77.8 for the first set of values when it should be up in the 90s?

René Vogt
  • 40,163
  • 14
  • 65
  • 85
  • 1
    After you "terminated" the `calculateAverage` function, what is the value of `examScore`? The solution is to not use global variables. – Some programmer dude Jun 09 '16 at 09:40
  • Thank you @JoachimPileborg for the quick response. I did as you said (as well as what another user said) and it fixed one of the issues. But the value for "average" I am getting is still not correct. – infiniteloop Jun 09 '16 at 09:52
  • `double main(void)` ??? Makes a change from `void main()` I suppose... – Paul R Jun 09 '16 at 09:58
  • If you look at your output you will see that the average isn't wrong. It isn't what you want, but it is what you coded. Your output shows 5 entries, 99, 98, 97, 96, and -1, which adds up to 389. Then you divide by the number of entries (5) and you get 77.8. The thing you need to fix is to not include the final -1 in either the sum or the number of entries. (That and resetting your variables or using local variable, as others have already pointed out.) – Michael Gorsich Jun 09 '16 at 10:06
  • 1
    @PaulR The `double` is wrong indeed, but the `void` is fine. –  Jun 09 '16 at 10:18

1 Answers1

0

I would recommend using local variables rather than global ones. That is to say, move these lines:

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

Here:

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

This will cause the variables to be reset to 0 every time the function starts rather than leaving the garbage from the last time the function ran.

I think that the reason that you are getting the wrong average is that you are including -1 as one of the test scores. You read the value, then add it to the average BEFORE you check if the value is -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;

You either need to test if the value is -1 before you recalculate the average, or you need to restructure your loop such that the examScore != SENTINAL check is done between reading and recalculating the average.

Also, strictly speaking, there is no need to make all the averaging calculations while the loop is still running. You can save the average = sumOfScores / sumOfExams; line until after the loop has finished. Just a thought.

As Paul R said, you also have the incorrect function prototype for your main function. Valid prototypes for the main function can be found here

Community
  • 1
  • 1
bytesized
  • 1,420
  • 11
  • 22