Jump to content

GPA Calculator

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
big-tony

big-tony

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
ok, My Program compiles and runs 100% with no errors, however, Where did I mess up in my code for the result to be like 30 digits long ?!?!

All the program does is calculate up to 30 "students" gpa and prints the average of them whenever the user presses zero. Here's an output of it:
Posted Image


Notice how the result is soo big?What's wrong with it? Here is my code:


#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main() {
float x, gpa[30], avg;
int i = 0, j;

printf("\n[GPA Calculator]\n\n");

do {
printf("Please enter a GPA score.\n To stop entering values and calculate the current GPA average, Enter 0.\n You are entering score # %d.\n", i + 1);
scanf("%f", &x);

if(x != 0) {
gpa[i] = x;
i++;
}
else {
for(j = 0; j < i; j++) {
avg += gpa[i];
avg = avg / i;
}
printf("The average GPA of the %d Entered Scores is: %f. Program exited.\n", i, avg);
break;
}

if(i == 29) {
for(j = 0; j < i; j++) {
avg += gpa[i];
avg = avg / i;
}
printf("The average GPA of the %d entered scores is %f. Program exited.\n", i, avg);
break;
}

} while (i < 30);

return 0;
}



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Change your final output to this:
printf("The average GPA of the %d entered scores is %8.2f. Program exited.\n", i, avg);
You have to specify the output if you don't want it to display as many decimal places as possible.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
big-tony

big-tony

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

WingedPanther said:

Change your final output to this:
printf("The average GPA of the %d entered scores is %8.2f. Program exited.\n", i, avg);
You have to specify the output if you don't want it to display as many decimal places as possible.
ok I changed that . Here is what my code looks like right now:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main() {
float x, gpa[31], avg;
int i = 0, j;

printf("\n[GPA Calculator]\n\n");

do {
printf("Please enter a GPA score.\n To stop entering values and 

calculate the current GPA average, Enter 0.\n You are entering score 

# %d.\n", i + 1);
scanf("%f", &x);

if(x != 0) {
gpa[i] = x;
i++;
}
else {
for(j = 0; j < i; j++) {
avg += gpa[i];
avg = avg / i;
}
printf("The average GPA of the %d entered scores is %8.2f Program 

exited.\n", i, avg);
break;
}

if(i == 30) {
for(j = 0; j < i; j++) {
avg += gpa[i];
avg = avg / i;
}
printf("The average GPA of the %d entered scores is %8.2f Program 

exited.\n", i, avg);
break;
}

} while (i < 31);

return 0;
}


But I am still getting these like 35 digit long "gpa averages" instead of the 2 digits it's suppose to be if I try to calculate the average after say around 5 scores entered.

Also when I put in the 30 gpa's, that output comes to :
"the average gpa of the 30 scores entered is 0.0"

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Your for loops are using j, but you are using i to access your array. The result is you are summing the same element that == 0.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
big-tony

big-tony

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

WingedPanther said:

Your for loops are using j, but you are using i to access your array. The result is you are summing the same element that == 0.

So I need to switch all the i's to j's and vice versa?

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You need to be consistent in what you're doing. In the for loops, use j consistently.

Are you using a debugger at all? If not, use one or trace through the code by hand. If you step through the code line by line, updating each variable as you go, a lot of the logic errors will become obvious.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog