Average = gArray[x] + 1 / j - 1;
the j is in a do while loop and every time I enter a GPA in it increments if that helps
I'll post the code if you guys need it you might want to take a look at what I have so far
Average = gArray[x] + 1 / j - 1;
the j is in a do while loop and every time I enter a GPA in it increments if that helps
I'll post the code if you guys need it you might want to take a look at what I have so far
That depends.
What is a GPA?
I assume "grade point average", but I don't think that's calculated the same everywhere.
And what exactly is the nature of the values in gArray?
And what type is Average?
However, your formula is almost certainly incorrect if j is an int, since 1/j will be 1 if j is 1, -1 if j is -1, undefined if j is 0, and 0 otherwise. I suggest you add some parentheses and possibly a cast or double-valued constant.
The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss
Yeah it stands for Grade Point Average I would like to take a list of numbers being a array and add them all up then divide by how many numbers are in the array just not sure how to go about it here is what I have even though its wrong
Code:#include <stdio.h> #include <stdlib.h> int main() { int x; const int MAX = 30; char choice = ' '; float gArray[MAX]; float Average; int j = 1; do { printf("\nEnter a GPA: "); scanf("%f",&gArray); printf("\nDo you wish to calculate current GPA: (y) YES (n) NO?) "); scanf(" %c", &choice); if (choice == 'y') for(x = 0; x <= j; x++) { Average = gArray[x] + 1 / j - 1; break; } j++; }while (choice == 'n'); for (x = 0; x <= j - 1; x++) { printf("\n%f1",gArray[x]); } printf("\nAverage = %f\n",Average); printf("%d",j); system("PAUSE"); return 0; }
Forget for a minute about expressing this in C - how would you take the average of 10 numbers mathematically?
Your code isn't even close, really.
You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.
say I had
3.5
2.8
3.0
2.5
4.0
3.7
----
19.5
average = score1 + score2 + score 3 + score4 + score5 + score6 / 6;
this would work for 6 scores but what if I had more or less?
say I had
3.5
2.8
3.0
2.5
4.0
3.7
----
19.5
average = score1 + score2 + score 3 + score4 + score5 + score6 / 6;
this would work for 6 scores but what if I had more or less?
If you have more, then the divisor would be greater than 6. So, what you need to do is to keep track of the number of scores entered.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I thought the j++ does that is there a better way
No. I have no idea where on earth you got that formula. Can you get your money back?
Keep a sum that increments with every added grade. Divide by the number of grades you have. Don't use integer division.
Code:#include <stdio.h> #include <stdlib.h> int main() { int x; int gradeCount = 0; int MAX = 30; float gArray[MAX]; float sum; float avg; char choice = ' '; do { printf("\nEnter GPA: "); scanf("%f", &gArray); gradeCount++; printf("\nCalculate GPA:(Yes or No)? "); scanf(" %c", &choice); }while ( choice == 'n'); for ( x = 0; x <=MAX-(MAX - gradeCount); x++) { sum += gArray[x]; avg = sum / MAX-(MAX - gradeCount); } printf("\nAverage = %f\n",avg); system("PAUSE"); return 0; }
I put in this list of numbers and I get this answer the answer isn't correct I don't know if its My calculations or something else I hope someone can help me figure this out heres my test data
Enter GPA: 3.5
Calculate GPAYes or No)? n
Enter GPA: 2.8
Calculate GPAYes or No)? n
Enter GPA: 3.0
Calculate GPAYes or No)? n
Enter GPA: 2.5
Calculate GPAYes or No)? n
Enter GPA: 4.0
Calculate GPAYes or No)? n
Enter GPA: 3.7
Calculate GPAYes or No)? y
Average = 12196996456264528000000000000000.000000
Press any key to continue . . .
Good attempt. You want to read into your array some grades and then do the math.
When the result is wrong, many things can be the reason. Two common ones, are: the math is wrong or the data is wrong!
If your beginner program would be a building, then, the result is the top of it and the data the base.
So, let's check the base first!
Print your array, like this
Oh and one more thing, make sure you enable -Wall flag at your compiler. -Wall show you warnings that the compiler might not show without it. Warnings are really serious!Code:/* Use gradeCount, in order to print only the part of the array you want to use. */ for(x = 0 ; x < gradeCount ; ++x) { printf("gArray[%d] = %f\n", x, gArray[x]); }
With -Wall I get
First check if your data is ok and then check your math in paper!!Code:main.c: In function `main': main.c:17: warning: float format, different type arg (arg 2)
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
I suggest "float sum;" Initializing this variable to zero.
Tim S.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
Enter GPA: 3.5
Calculate GPAYes or No)? n
Enter GPA: 2.8
Calculate GPAYes or No)? n
Enter GPA: 3.0
Calculate GPAYes or No)? n
Enter GPA: 2.5
Calculate GPAYes or No)? n
Enter GPA: 4.0
Calculate GPAYes or No)? n
Enter GPA: 3.7
Calculate GPAYes or No)? y
gArray[0] = 3.700000
gArray[1] = 0.000000
gArray[2] = 0.000000
gArray[3] = 0.000000
gArray[4] = 0.000000
gArray[5] = 177016798723160610000000000000000.000000
Press any key to continue . . .
Well, it appears that my data is way off I just don't know why its doing that
here's my code again
Code:#include <stdio.h> #include <stdlib.h> int main() { int x; int gradeCount = 0; int MAX = 30; float gArray[MAX]; float sum = 0; float avg; char choice = ' '; do { printf("\nEnter GPA: "); scanf("%f", &gArray); gradeCount++; printf("\nCalculate GPA:(Yes or No)? "); scanf(" %c", &choice); }while ( choice == 'n'); for ( x = 0; x < gradeCount; ++x) { printf("gArray[%d] = %f\n",x,gArray[x]); } //printf("\nAverage = %f\n",avg); system("PAUSE"); return 0; }
P.S. I don't know how to turn on a wall flag on my compiler too advanced for me
You aren't referencing a slot in the array.. you are just entering in each GPA into the first slot of the array.Code:scanf("%f", &gArray);