Cbeginner
Cbeginner

Reputation: 11

Find the average of an unknown number of inputs

I need to write a C program that calculates the average, using a function, of grades entered by the user. The number of grades entered is unknown so I have to use a sentinel value -1.

This is my code and it compiles fine, however it doesn't tell me the average and the for loop doesn't call the function four times. What am I missing?

 #include <stdio.h>

 float calculateAverage (int sumofGrades, int numberofGrades);

 int main(void) {

 int i;
 int sumofGrades = 0;
 int numberofGrades = 0;
 int grade = 0;
 float average;

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

    calculateAverage(sumofGrades, numberofGrades);

 }
 return 0;
 }

float calculateAverage (int sumofGrades, int numberofGrades) {

float average;
int grade;

printf("Enter grades and -1 to stop: ");
scanf("%d", &grade);
while (grade <= 100 && grade != -1) {
    numberofGrades++;
    sumofGrades += grade;
    average = sumofGrades / numberofGrades;

}
printf("The average is %.2f", average);

return average;

}

Update

Thanks for the help! But I can't get it right yet. For the assignment I need the program to ask me 4 times to input the unknown number of grades and each time the average is calculated.

Also, why doesn't it stop and gives me the average once I type -1?

#include <stdio.h>

float calculateAverage(int sumofGrades, int numberofGrades);

int main(void) {
    int i;
    int sumofGrades = 0;
    int numberofGrades = 0;

    for (i = 1; i <= 4; ++i) {
        calculateAverage(sumofGrades, numberofGrades);
    }
    return 0;
}

float calculateAverage (int sumofGrades, int numberofGrades) {

    float average;
    int grade = 0;

    while (grade != -1 && grade >= 0 && grade <= 100) {
        printf("Enter grades and type -1 to stop: ");
        scanf("%d", &grade);

        if (grade != -1) {
            numberofGrades++;
            sumofGrades += grade;
        }
    }

    average = sumofGrades / numberofGrades;
    printf("The average is %.2f\n", average);

    return average;
}

Update

I'm still having issues! I can't get the program to ask me to enter the grades four times and calculate the average each time!! I tried and I tried and it's so frustrating!! What am I doing wrong??

Upvotes: 1

Views: 6756

Answers (3)

user3629249
user3629249

Reputation: 16540

there is no way, given the posted code, for the sub function to update the variables in the main() function.

Suggest passing pointers to the variables and modifying the sub function to expect (and properly handle) those pointers

Upvotes: 0

BradStell
BradStell

Reputation: 500

First off you are looping through your calculateAverage funtion 4 times. Are you doing this on purpose? I think you should only do this once.

Second, in your while loop

while (grade <= 100 && grade != -1) {
    numberofGrades++;
    sumofGrades += grade;
    average = sumofGrades / numberofGrades;

}

you are infinitely looping. This will loop forever if a value other than -1 is entered. This should be changed to an if statement.

if (grade <= 100 && grade != -1)

But there is a better way to write this code (if I am guessing what you actually want it to do)

#include <stdio.h>

float calculateAverage (int sum, int size);

int main(void) 
{

    int input;
    int sum = 0;
    int count = 0;
    while (input != -1 && input >= 0 && input <= 100)
    {
       printf ("Enter a Grade: ");
       scanf ("%d", &input);

       if (input != -1)
       {
           sum += input;
           count++;
       }
    }

    float average = calculateAverage(sum, count);
    return 0;
}

float calculateAverage (int sum, int count) {

   // Do your average math here....
}

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 755006

Once you've entered one valid grade, your code doesn't try to read another grade:

printf("Enter grades and -1 to stop: ");
scanf("%d", &grade);
while (grade <= 100 && grade != -1) {
    numberofGrades++;
    sumofGrades += grade;
    average = sumofGrades / numberofGrades;
}

If the grade is valid, it goes into an infinite loop. It also calculates the average grade on each cycle, which is unnecessary. Once, after the loop, is sufficient.

Maybe you should use:

printf("Enter grades and -1 to stop: ");
while (scanf("%d", &grade) == 1 && grade <= 100 && grade >= 0)
{
    numberofGrades++;
    sumofGrades += grade;
}
if (numberofGrades == 0)
{
    fprintf(stderr, "Error: zero valid grades entered\n");
    average = 0.0;
}
else
{
    average = sumofGrades / numberofGrades;
    printf("The average is %.2f", average);
}
return average;

Note that this checks that the input was successful before using the grade.

You also really don't need to pass the variables into the function. You'd do better to make the function accept no arguments and have the parameters as regular local variables properly initialized to zero.

Your main program should capture the average since it is returned, rather than ignore it. Or maybe the function shouldn't return the average after all.

Upvotes: 1

Related Questions