Thread: Problem with average

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    1

    Exclamation Problem with average

    Hello, I have this problem,

    Write a program for a professor to calculate the average grade of a student.The program should accept as many grades as the professor wants to enter.Therefore, ask the professor for the first grade, the second grade, and so onuntil the professor enters a negative value. A negative value indicates thatthe professor is finished entering grades. Once the professor is finished, yourprogram should output:a) the average grade of the studentb) the letter grade of the student

    this is what I have so far.
    Code:
    #include <stdio.h>
    
    
    int main ()
    {
        double grade, s, total_firstgrade=0, total_secondgrade=0,average=0; //declare the variables
    
    
        do
        {
            printf("\nEnter the student grade: "); //asking the user to enter a integer
            scanf("%lf",&grade);
            printf
            if(f==-1||f<=0)
                break;
            else
            {
                printf("Enter the student grade: ");
                scanf("%lf",&s);
    
    
                //calculation of the average
                total_firstgrade+= f;
                total_secondgrade+= s;
    
    
                average:total_firstgrade+total_secondgrade/average;
    
    
            }
        } while (1);
    
    
    
    
            if (f<=0&& f!=-1)
                printf("first grade cannot be 0 or negative other than -1 to stop\n");
    
    
            if (total_firstgrade==0)
                printf("total first grade is 0, no average calculated\n");
            else
                printf("\nthe average of the student is: %lf\n",average);
    
    
    
    
        //using the if statement to find the student average grade
        if (s>100|| s,0)
            printf("non-valid average");
        else
        {
            if (s>=0)
                if (s<60)
                printf("F\n");
            else if (s<70)
                printf("D\n");
            else if (s<80)
                printf("C\n");
            else if (s<90)
                printf("B\n");
            else if (s<=100)
                printf("A\n");
        }
    }
    The thing is that it does not make the average. I would like to know what can I do to fix this problem. Thank You!

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Hmm... Calculating the live average is computationally complex in this case.

    Instead, use std::vector and push_back() to gather up all the elements first. Then use std::accumulate to sum up your vector and divide by the size of the vector as well. This will give you your average.

    Edit:

    For averages, you really want to gather up all the elements first and then sum them.

    For example, (1 + 2 + 3) / 3 + x to get (1 + 2 + 3 + 4) / 4 does not seem to be what you're doing above. Solving for x in that case would probably be much less efficient than a gather-and-solve.
    Last edited by MutantJohn; 05-22-2016 at 03:27 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I suppose the first question is whether this is really a C program (and should have been posted on the C forum), or some poorly written C++ program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. average problem
    By Th0rh4memer in forum C Programming
    Replies: 6
    Last Post: 02-09-2016, 12:15 PM
  2. Replies: 9
    Last Post: 06-25-2015, 04:10 PM
  3. Replies: 27
    Last Post: 02-14-2015, 11:17 AM
  4. Decidedly average homework problem
    By kingkobra in forum C++ Programming
    Replies: 21
    Last Post: 12-04-2009, 10:10 AM
  5. Grade program average problem
    By Zaz in forum C Programming
    Replies: 1
    Last Post: 11-20-2009, 07:11 PM

Tags for this Thread