Thread: How to write a percentage in this problem?

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    37

    How to write a percentage in this problem?

    Hi again,

    I almost finished my code for an assignment but I have trouble outputting a percentage. Thanks in advance.

    Need the program to output "[number] percent are passing grades.":


    We want to count how many passinggrades are entered. We don’t know how many grades there will be. Use a sentinel controlled while loop that will ask the user toenter student grades until a value of -1 is entered. Use a counter variable to count all thegrades that are passing grades, where 70 is the minimum passing grade. If there are any grades that are out of therange 0 – 100, present an error message to the user, and do not count thatgrade as passing. We also would like to see what percentage ofthe valid grades are passing.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    int grades, passingGrades, total, failingGrades;
    double percentage;
    passingGrades = 0;
    failingGrades = 0;
    while(grades != -1) {
     
    printf("Enter the grades. Enter -1 to stop. \n");
    scanf("%i", &grades);
    if(grades > 100 || grades < -1) {
      printf("That is not a valid grade. \n");}
    {
        if (grades == -1)
      {
       grades = -1;
      }
      else 
      if (grades <= 100 && grades >= 70)
      {
      passingGrades = passingGrades + 1;
      }
      else
      {
      grades = 0;
      }
      }
              }
    printf("%i are passing grades. \n", passingGrades);
    
    /* Need to add calculation of percentage! */
    system("pause");
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Have you done a search for how to calculate percentage? It's pretty easy.
    Hint: You can find the total number of grades entered by adding together "passingGrades" and "failingGrades" (the latter of which is not properly tracked in your code).

    You should also ensure your code is neatly formatted and indented:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int grades, passingGrades, total, failingGrades;
        double percentage;
        
        passingGrades = 0;
        failingGrades = 0;
        
        while(grades != -1)
        {
            printf("Enter the grades. Enter -1 to stop. \n");
            scanf("%i", &grades);
    
            if(grades > 100 || grades < -1)
            {
                printf("That is not a valid grade. \n");
            }
            
            {
            if (grades == -1)
            {
                grades = -1;
            }
            else if (grades <= 100 && grades >= 70)
            {
                passingGrades = passingGrades + 1;
            }
            else
            {
                grades = 0;
            }
            }
        }
    
        printf("%i are passing grades. \n", passingGrades);
    
        /* Need to add calculation of percentage! */
        system("pause");
    }

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Thank you for the reply.
    I attempted to do what I pasted below, and the percents are off :/ Did I add in the code in the correct places?

    Edit: I fixed the indents but its not showing correctly, whelp.

    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() 
    {
     int grades, passingGrades, total, failingGrades;
     double percentage;
    
     passingGrades = 0;
     failingGrades = 0;
    
     while(grades != -1) 
    {
     
      // Ask the user to input grades, and type -1 to stop
      printf("Enter the grades. Enter -1 to stop. \n");
      scanf("%i", &grades);
     
      // If the user enters anything lower than -1 or higher than 100, code displays an error.
      if(grades > 100 || grades < -1) {
      printf("That is not a valid grade. \n");}
    {
         if (grades == -1)
      {
       grades = -1;
      }
      else 
      if (grades <= 100 && grades >= 70)
      {
      // Calculates the number of passing grades
      passingGrades = passingGrades + 1;
      total = passingGrades + failingGrades;
      percentage = (passingGrades / total) * 100;
      }
      else
      {
      grades = 0;
      // Calculates the total and percentage
      }
      }
              }
     // Prints results to user
     printf("%i are passing grades. \n", passingGrades);
     printf("%.2lf percent are passing grades. \n", percentage);
     
     // printf("%lf percent are passing grades. \n", percentage);
     // Need percentage calculation and print results 
     // total = passingGrades + failingGrades; ?
     // percentage = (passingGrades / total) * 100; ?
     
     system("pause");
    }
    Last edited by Kayla Hoyte; 10-18-2017 at 05:52 PM.

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    put this in the matching code, with the added printf to see what your value is wihin that var
    Code:
    if (grades <= 100 && grades >= 70)
      {
      // Calculates the number of passing grades
    
      passingGrades = passingGrades + 1;
    
     printf("CAN YOU SEE ME????? %d passingGrades = passingGrades + 1\n",  passingGrades );// <-------------------------
    
      total = passingGrades + failingGrades;
      percentage = (passingGrades / total) * 100;
      }
      else
    Last edited by userxbw; 10-18-2017 at 06:15 PM.

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    I attached the output, hopefully I did so correctly.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main() 
    {
     int grades, passingGrades, total, failingGrades;
     double percentage;
     passingGrades = 0;
     failingGrades = 0;
     while(grades != -1) 
     {
     
      // Ask the user to input grades, and type -1 to stop
      printf("Enter the grades. Enter -1 to stop. \n");
      scanf("%i", &grades);
     
      // If the user enters anything lower than -1 or higher than 100, code displays an error.
      if(grades > 100 || grades < -1) {
      printf("That is not a valid grade. \n");}
    {
         if (grades == -1)
      {
       grades = -1;
      }
      else 
      if (grades <= 100 && grades >= 70)
      {
      // Calculates the number of passing grades
      printf("%i = %i + 1 \n", passingGrades);
      total = passingGrades + failingGrades;
      percentage = (passingGrades / total) * 100;
      }
      else
      {
      grades = 0;
      // Calculates the total and percentage
      }
      }
              }
     // Prints results to user
     printf("%i are passing grades. \n", passingGrades);
     printf("%.2lf percent are passing grades. \n", percentage);
     // Need to fix percentage calculation...
     
     system("pause");
    }
    Attached Images Attached Images How to write a percentage in this problem?-value-jpg 

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    moded
    Code:
    $ ./a.out
    $ ./a.out
    Enter the grades. Enter -1 to stop. 
    70
    0 = 547313808 + 1 
    1 passingG 0, failingG 0
    2 passingG 0, failingG 0
    Floating point exception
    userx@slackwhere:~/bin
    you code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main() 
    {
     int grades, passingGrades, total, failingGrades;
     double percentage;
     passingGrades = 0; // stays 0 no assinging it anything after you
     failingGrades = 0; // stays 0 get input from grades. 
     
     
     
     while(grades != -1) 
     {
      
      // Ask the user to input grades, and type -1 to stop
      printf("Enter the grades. Enter -1 to stop. \n");
      scanf("%d", &grades);
      
      // If the user enters anything lower than -1 or higher than 100, code displays an error.
        if(grades > 100 || grades < -1)
        {
            printf("That is not a valid grade. \n");
        }
        { //opens here -  
            if (grades == -1)
            {
                grades = -1;
            }
            else if (grades <= 100 && grades >= 70.0)
            {
              // Calculates the number of passing grades
              printf("%i = %i + 1 \n", passingGrades);
               printf("1 passingG %d, failingG %d\n", passingGrades, failingGrades);
               
              total = passingGrades + failingGrades;
             
               printf("2 passingG %d, failingG %d\n", passingGrades, failingGrades);
              //stays (0/0) * 100
              percentage = (passingGrades / total) * 100;
            }
            else
            {
                grades = 0;
              // Calculates the total and percentage
            }
        } // closes here :: what need is theses brackets? 
    } // end while
     // Prints results to user
     printf("passingG %d, percentage %.2lf\n", passingGrades,percentage);
     printf("%i are passing grades. \n", passingGrades);
     printf("%.2lf percent are passing grades. \n", percentage);
     // Need to fix percentage calculation...
      
     system("pause");
    }
    Last edited by userxbw; 10-18-2017 at 07:07 PM.

  7. #7
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    So failingGrades isn't getting a value properly? I'm not sure if I follow, I'm sorry.

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kayla Hoyte View Post
    So failingGrades isn't getting a value properly? I'm not sure if I follow, I'm sorry.
    you got the right idea here;
    Code:
      total = passingGrades + failingGrades;
    assigning a value to another variable and adding to it at the same time. But you did not do that with your input first
    that gives you that starting point so that the variable called, passingGrades, will even have a value inside of it to pass it into
    the next variable you are using called, total. you missed your first very important step.

    in other words,
    what variable are you using to get the starting value in the first place? you are not using that one to pass it to any other variable
    to keep its value and do the math on it.
    Last edited by userxbw; 10-19-2017 at 07:50 AM.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If the entered grade is passing, you need to increment (add one to) "passingGrades".
    If the entered grade is not passing, you need to increment (add one to) "failingGrades".

    Your percentage calculation should be after the loop, when all the values are finished being entered.

  10. #10
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    I think I understand userxbw, and thank you Matticus. So passingGrades will start at 0 and so will failingGrades. total is failingGrades added to passingGrades. percentage is passingGrades divided by total then timed by one hundred.

    My program isn't running correctly --- typing -1 does not stop from entering values. I think the problem is at line 23? I am really bad at figuring this stuff out, jesus.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() 
    {
     int grades, passingGrades, total, failingGrades;
     double percentage;
     passingGrades = 0;
     failingGrades = 0;
     while(grades != -1) 
     {
      // Ask the user to input grades, and type -1 to stop
      printf("Enter the grades. Enter -1 to stop. \n");
      scanf("%i", &grades);
     
      // If the user enters anything lower than -1 or higher than 100, code displays an error.
      if(grades > 100 || grades < -1) {
      printf("That is not a valid grade. \n");
      }
      {
         if (grades == -1)
      {
      grades = -1;
      }
      else 
      if (grades <= 100 && grades >= 70)
      {
      // Calculates the number of passing grades
      passingGrades = passingGrades + 1;
      }
      else
      if (grades < 70 || grades >= 0)
      {
      failingGrades = failingGrades + 1;
      }
      {
      grades = 0;
      }
      }
         }
     // Calculates the total and percentage
     total = passingGrades + failingGrades;
     percentage = (passingGrades / total) * 100;
     // Prints results to user
     printf("%i are passing grades. \n", passingGrades);
     printf("%.2lf percent are passing grades. \n", percentage);
     system("pause");
    }

  11. #11
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by Kayla Hoyte View Post
    I think I understand userxbw, and thank you Matticus. So passingGrades will start at 0 and so will failingGrades. total is failingGrades added to passingGrades. percentage is passingGrades divided by total then timed by one hundred.

    My program isn't running correctly --- typing -1 does not stop from entering values. I think the problem is at line 23? I am really bad at figuring this stuff out, jesus.
    Your indentation and bracket use are way off. It is hard to find a problem when reading the code becomes difficult.
    Your problem lies in line 37.

    Here's the code properly indented:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main(void) 
    {
        int grades, passingGrades, total, failingGrades;
        double percentage;
        passingGrades = 0;
        failingGrades = 0;
    
    
        while(grades != -1) 
        {
          // Ask the user to input grades, and type -1 to stop
          printf("Enter the grades. Enter -1 to stop. \n");
          scanf("%i", &grades);
      
          // If the user enters anything lower than -1 or higher than 100, code displays an error.
          if(grades > 100 || grades < -1) 
          {
              printf("That is not a valid grade. \n");
          }
    
          {
          if (grades == -1)
          {
              grades = -1;
          }
          else if (grades <= 100 && grades >= 70)
          {
              // Calculates the number of passing grades
              passingGrades = passingGrades + 1;
          }
          else if (grades < 70 || grades >= 0)
          {
              failingGrades = failingGrades + 1;
          }
          {
              grades = 0;
          }
          }
        }
        // Calculates the total and percentage
        total = passingGrades + failingGrades;
        percentage = (passingGrades / total) * 100;
    
        // Prints results to user
        printf("%i are passing grades. \n", passingGrades);
        printf("%.2lf percent are passing grades. \n", percentage);
        system("pause");
    
        return 0;
    }
    You should also have else ifs for every case of 'grades', so that when one if statement is valid it doesn't keep checking. (If grades is > 100 or < -1 it is clearly never failing nor passing nor -1).

    Also, make sure of compiling with -Wall, main returns an int, but there is no return in the function. This is made clear by the compiler if you compile with -Wall.

    Also it is:
    Code:
    int main(void)
    rather than
    Code:
    int main()
    since it is a function definition.
    Last edited by Fiskker; 10-19-2017 at 10:51 AM.

  12. #12
    Banned
    Join Date
    Aug 2017
    Posts
    861
    shouldn't grades be from 0 to 100 0 = F, fail 100 = 'A' pass. so anything between is an b c d , therefore take in what ever number up to 100, then total/how many taken in gives average, so do the math for percent. because the numbers being taken in represent a percent. yes?

    Code:
    If you want to know what percent A is of B,
     you simply divide A by B, then take that number 
    and move the decimal place two spaces to the right. 
    That's your percentage!
    that is a google, how to do percentage in C programming ... google answer, not mine.
    Code:
    printout 
    input percentages, hit q to printout total percentage gained
    that is the basic set up. right?
    Last edited by userxbw; 10-19-2017 at 11:56 AM.

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    this does not complete your code, but I hope it helps you in writing it more logically.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int grades, passingGrades, total, failingGrades;
        double percentage;
        passingGrades = 0;
        failingGrades = 0;
    
    
        while(grades != -1)
        {
          // Ask the user to input grades, and type -1 to stop
          printf("Enter the grades. Enter -1 to stop. \n");
          scanf("%i", &grades);
    
          // If the user enters anything lower than -1 or higher than 100, code displays an error.
          // no need to check for both
          // just check for over 100
          // your while uses -1 to quit it will check that value then quit.
          if(grades > 100 || grades < -1)
          {
              printf("That is not a valid grade. \n");
          }
    
          {
            // is there such a grade as negative 1 ?
          if (grades == -1)
          {
              grades = -1;
          } // it looks like your spliting them into two separate 
          // categories 70 and less fail, over 70 pass. one check 
          // is all you need to do, the rest automatically goes
          // to the other batch 
          // if one thing put them over there
          // else it is the other put them somewhere else.
          //
          // or it is one way or the other. 
          //
          // if the magic number or greater then put them in that
          //pile. else put them in the other pile.
          //
          else if (grades <= 100 && grades >= 70)
          {
              // Calculates the number of passing grades
              passingGrades = passingGrades + 1;
          }
          else if (grades < 70 || grades >= 0)
          {
              failingGrades = failingGrades + 1;
          }
          {
              grades = 0;
          }
          }
        }
        // Calculates the total and percentage
        total = passingGrades + failingGrades;
        percentage = (passingGrades / total) * 100;
    
        // Prints results to user
        printf("%i are passing grades. \n", passingGrades);
        printf("%.2lf percent are passing grades. \n", percentage);
        system("pause");
    
        return 0;
    }
    oh yeah, way are you pausing the system when you're quitting the program?

    this is what I am getting after I fixed it to my specs, I do not know what yours are, but I think I got them right.
    I did not mess with your math computations.
    Code:
    userx@slackwhere:~/bin
    $ ./grading_percent
    Enter the grades. Enter -1 to stop. 
    30
    failingGrades 1
    Enter the grades. Enter -1 to stop. 
    30
    failingGrades 2
    Enter the grades. Enter -1 to stop. 
    43
    failingGrades 3
    Enter the grades. Enter -1 to stop. 
    50
    failingGrades 4
    Enter the grades. Enter -1 to stop. 
    100
    passingGrades 1
    Enter the grades. Enter -1 to stop. 
    100
    passingGrades 2
    Enter the grades. Enter -1 to stop. 
    73
    passingGrades 3
    Enter the grades. Enter -1 to stop. 
    -1
    failingGrades 5
    total 8
    3 are passing grades. 
    0.00 percent are passing grades.
    just for a little , just so you know,
    Code:
      passingGrades += 1;  //  passingGrades + 1;
    their is a different way you can add them up as well.
    Last edited by userxbw; 10-19-2017 at 11:58 AM.

  14. #14
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    (I should note that I am using Dev-C++)

    I fixed some of the issues I made. I tested and saw that when I put in two, three, four numbers that are passing it displays the percentage correctly (at 100 as expcted). Anything else does not compute the percentage. I still do not see the issue.

    I apologize for testing your patience.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
        int grades, passingGrades, total, failingGrades;
        double percentage;
        
        passingGrades = 0;
        failingGrades = 0;
        
        while(grades != -1)
        {
            printf("Enter the grades. Enter -1 to stop. \n");
            scanf("%i", &grades);
            if(grades > 100)
            {
                printf("That is not a valid grade. \n");
            }
            else
            {
            if (grades == -1)
            {
                grades = -1;
            }
            else if (grades <= 100 && grades >= 70)
            {
                passingGrades += 1;
            }
            else
            {
                failingGrades += 1;
            }
            }
        }
    
     total = passingGrades + failingGrades;
     percentage = ((passingGrades / total) * 100);
     
     printf("%i are passing grades. \n", passingGrades);
     printf("%.2lf percent are passing. \n", percentage);
    
     return 0;
    }

  15. #15
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Kayla Hoyte View Post
    (I should note that I am using Dev-C++)

    I fixed some of the issues I made. I tested and saw that when I put in two, three, four numbers that are passing it displays the percentage correctly (at 100 as expcted). Anything else does not compute the percentage. I still do not see the issue.

    I apologize for testing your patience.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
       ---> // int grades, passingGrades, total, failingGrades;
    // change everything to use float, or double
    // then format it properly  everywhere it is applicable 
        double percentage;
        
        passingGrades = 0;
        failingGrades = 0;
        
        while(grades != -1)
        {
            printf("Enter the grades. Enter -1 to stop. \n");
            scanf("%i", &grades);
            if(grades > 100)
            {
                printf("That is not a valid grade. \n");
            }
              // invalid code, because it never gets used,
              // what else is using -1 
            else
            {
            if (grades == -1)
            {
                grades = -1;
            }
            else if (grades <= 100 && grades >= 70)
            {
                passingGrades += 1;
            }
            else
            {
                failingGrades += 1;
            }
            }
        }
    
     total = passingGrades + failingGrades;
     percentage = ((passingGrades / total) * 100);
     
     printf("%i are passing grades. \n", passingGrades);
     printf("%.2lf percent are passing. \n", percentage);
    
     return 0;
    }
    Code:
    failingGrades 4.000000
    total 9.000000
    5.000000 are passing grades. 
    total / passingGrades 0.555556
    55.555556 percent are passing grades.
    Code:
    .....// you already have preventive measures to not get 
       // anything over 100 set prior to getting to this code. so....
    
        if (grades  equal to or are greater than magic number )
          {
              what ever is the magic number or greater gets added here
          }
          else 
          {
             apply process of elimination 
            so the rest just get added here. 
          }
    ........
    worse yet,
    Code:
    grades <= 100 && grades >= 70
    a number less than or equal to 100 get taken, and a number greater than or equal to 70 get taken.

    so what is left between 0 thru 100?

    just a few little fixes and can end up with it looking like this
    Code:
    -1
    total 9
    3 are passing grades. 
    33.33 percent are passing grades.
    Last edited by userxbw; 10-19-2017 at 02:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem with percentage variable
    By shansajid in forum C Programming
    Replies: 7
    Last Post: 01-14-2013, 12:31 PM
  2. Help with a percentage
    By Alecroy in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2010, 07:24 PM
  3. Calculating a percentage
    By swgh in forum C++ Programming
    Replies: 3
    Last Post: 11-24-2006, 10:24 AM
  4. problem with fout.write: cannot write 0x0A (10 dec)
    By yvesdefrenne in forum C++ Programming
    Replies: 7
    Last Post: 05-23-2005, 12:53 PM
  5. Percentage
    By skyglin in forum C Programming
    Replies: 7
    Last Post: 06-23-2003, 05:18 PM

Tags for this Thread