Thread: Would this formula give me The GPA of a list of numbers

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    Would this formula give me The GPA of a list of numbers

    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

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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;
    }

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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?

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I thought the j++ does that is there a better way

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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 . . .

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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
    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]);
    }
    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!
    With -Wall I get
    Code:
    main.c: In function `main':
    main.c:17: warning: float format, different type arg (arg 2)
    First check if your data is ok and then check your math in paper!!
    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

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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

  13. #13
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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;
    }

  14. #14
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    P.S. I don't know how to turn on a wall flag on my compiler too advanced for me

  15. #15
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    scanf("%f", &gArray);
    You aren't referencing a slot in the array.. you are just entering in each GPA into the first slot of the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  2. priority list won't work, i give up
    By teamster in forum C Programming
    Replies: 8
    Last Post: 10-19-2006, 12:26 PM
  3. max/min in a list of numbers
    By shockalaca in forum C Programming
    Replies: 2
    Last Post: 05-23-2005, 02:11 AM
  4. get a list of numbers
    By aoe in forum C Programming
    Replies: 2
    Last Post: 06-07-2002, 11:57 PM
  5. Replies: 1
    Last Post: 10-01-2001, 10:39 AM