C Programming (GPA Calculator)
Very stuck on this problem. I FINALLY got the average to开发者_JAVA技巧 come out, but it gives it as negative 32 million or something. Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float fArray[30];
int choice = 0;
int x = 0;
float total = 0;
float avg = 0;
printf("1. Calculate GPA Average");
printf("\n2. Enter GPA");
printf("\n3. Quit");
printf("\n\nEnter your choice (1-3): ");
scanf("%d", &choice);
if(choice == 2)
{
printf("\n\nEnter GPA: ");
scanf("%.2f\n\n", &fArray[x]);
total = total + fArray[x];
}
else if(choice == 3)
{
return 0;
}
else if(choice == 1)
{
printf("The average is: %f", total / x);
}
for(x = 1; x < 30; x++)
{
fflush(stdin);
int temp = 0;
printf("1. Calculate GPA Average");
printf("\n2. Enter GPA");
printf("\n3. Quit");
printf("\n\nEnter your choice (1-3): ");
scanf("%d", &temp);
if(temp == 2)
{
printf("\n\nEnter GPA: ");
scanf("%.2f\n\n", &fArray[x]);
}
else if(temp == 3)
{
break;
}
else if(temp == 1)
{
printf("The average is: %f", total / x);
}
}
system("pause");
}
This is clearly a homework problem, and you are clearly a beginning programmer. Your teacher can probably give you better help than we can.
Some tips:
- You have copy-and-pasted code. You should get rid of about the first half of the program; you should only keep the variable declarations and the loop.
- For clarity's sakes C programmers usually have the \n at the end of a string, rather than the beginning of the next string.
- You shouldn't use a "for" loop iterating over values of 'x' to drive the user interface. If the user pressed "1" 28 times to calculate the GPA average, then pressed "2" to enter a grade, then pressed "1" to calculate the GPA average again, you'd divide the grade by 30 even though they only entered one grade. You should not iterate over 'x'. Instead, you should drive the user interface with a "while (True)" loop. You should increment 'x' only when the user chooses to enter a GPA.
- Your code for calculating 'total' is only run the very first time (when 'x' is 0). There are no other assignments to 'total'. Now do you see the danger of copy-and-pasted code?
Where to start... use do{...}while(). You're not initializing your array of floats. You're not keeping track of how many grades have been input. You're also never updating the total variable in your for loop, so no matter what they enter, the displayed total will never change. You never even use the declared avg variable.
Here's a tip: think about the algorithm first; i.e., HOW are you going to perform these operations? Can you separate any operations (such as calculating an average) from others (such as getting user input)? Once you've figured out your process you want to use, consider what types of data structures and program flow (looping) constructs will be appropriate, THEN start coding; it'll make a big difference, and it'll be easier to implement.
Forgot to add to total in the loop
Try putting some debug statements like
printf("Total is: %d\n", total);
and
printf("x is: %d\n", x);
inside your 'if' statements (or anywhere that you want to check the value of your total). You will very quickly realise where you are going wrong.
精彩评论