Thread: How to write a percentage in this problem?

  1. #16
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Looks like you're making good progress. Does your output look correct?

    Code:
    Enter the grades. Enter -1 to stop.
    75
    Enter the grades. Enter -1 to stop.
    50
    Enter the grades. Enter -1 to stop.
    -1
    1 are passing grades.
    0.00 percent are passing.
    You're performing integer division in the "percentage" calculation, which means the result is truncated (you lose any fractional part).
    Try casting one of the variables to a double:

    Code:
    percentage = (((double)passingGrades / total) * 100);
    You can also neaten up the logic in your loop. It helps to plan the logic "by hand" on paper first, before translating it to code.

    I might approach the loop as follows:

    Code:
    while grade != -1
    
      prompt for input
      read input into grade
    
      if grade is between 70 and 100
        increment passing
    
      else if grade is between 0 and 69
        increment failing
    
      else if grade is not -1
        print error

  2. #17
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by userxbw
    Code:
       ---> // int grades, passingGrades, total, failingGrades;
    // change everything to use float, or double
    // then format it properly in your printf 's
    No, integer is a more appropriate data type for counting. The OP can use casting to get a double result, as I have shown.

    Quote Originally Posted by userxbw
    Code:
    { // invalid code, because it never gets used,
      // what else is using -1 
    If the following "if" is removed, then without additional modification, the final "else" will increment failingGrades on invalid input below -1.
    This was also addressed in my suggested logic example.
    Last edited by Matticus; 10-19-2017 at 01:44 PM. Reason: clarification

  3. #18
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Quote Originally Posted by Matticus View Post
    Looks like you're making good progress. Does your output look correct?

    Code:
    Enter the grades. Enter -1 to stop.
    75
    Enter the grades. Enter -1 to stop.
    50
    Enter the grades. Enter -1 to stop.
    -1
    1 are passing grades.
    0.00 percent are passing.
    You're performing integer division in the "percentage" calculation, which means the result is truncated (you lose any fractional part).
    Try casting one of the variables to a double:

    Code:
    percentage = (((double)passingGrades / total) * 100);
    You can also neaten up the logic in your loop. It helps to plan the logic "by hand" on paper first, before translating it to code.

    I might approach the loop as follows:

    Code:
    while grade != -1
    
      prompt for input
      read input into grade
    
      if grade is between 70 and 100
        increment passing
    
      else if grade is between 0 and 69
        increment failing
    
      else if grade is not -1
        print error
    I got it to work!!!!
    Adding the double part was the solution. Thank you so much for your help and patience guys. I think planning it out first by hand will help me a lot.

  4. #19
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Matticus View Post
    No, integer is a more appropriate data type for counting. The OP can use casting to get a double result, as I have shown.



    If the following "if" is removed, then without additional modification, the final "else" will increment failingGrades on invalid input below -1.
    This was also addressed in my suggested logic example.
    yes .but if needed 34.5 then what? . too I wanted to stay away from casting ... I doubled everything and that works too. but yes, casting too works. but i wanted to stay away from casting because who knows if they've covered that in class yet or not?

    just fix it in the printf
    Code:
    , 
        total = passingGrades + failingGrades;
        printf("total %.0lf\n", total);
     //   percentage =  (( (double) passingGrades / total) * 100 );
      percentage =  (( passingGrades / total) * 100 );
        printf("%.0lf are passing grades. \n", passingGrades);
    
        printf("%.2lf percent are passing grades. \n",percentage );
    do not look at casting part I as trying to stay away from that as I said why already.

    no casting / Plus I take that all back, most of it anyways. upon further investigation, I see why you where
    trying to use that -1 further
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        double passingGrades, failingGrades;
        int grades;
        passingGrades = 0.0;
        failingGrades = 0.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("%d", &grades);
            if(grades >= 101 )
                printf("That is not a valid grade. \n");
            else if (grades < 70 &&  grades != -1 )         
                failingGrades += 1;
            else if (grades >= 70 && grades != -1 ) 
                passingGrades += 1;
        }
        // Calculates the total and percentage
        printf("total %.0lf\n", passingGrades + failingGrades);
        printf("%.0lf are passing grades. \n", passingGrades);            // gives total
        printf("%.2lf percent are passing grades. \n",((  passingGrades / (passingGrades + failingGrades) ) * 100 ) );
        return 0;
    }
    results
    Code:
    Enter the grades. Enter -1 to stop. 
    -1
    total 11
    5 are passing grades. 
    45.45 percent are passing grades.
    OP said they had it figured out so....
    Last edited by userxbw; 10-19-2017 at 03:14 PM.

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    yes .but if needed 34.5 then what? . too I wanted to stay away from casting ... I doubled everything and that works too. but yes, casting too works. but i wanted to stay away from casting because who knows if they've covered that in class yet or not?
    I think Matticus still has a point, though. If you only wanted to avoid a cast, you could have simply taken percentage variable and done this:
    Code:
    double percentage = passingGrades;
    percentage = percentage / total * 100;
    You can think of casting as an attempt by the compiler to store the value in an implicit variable of the desired type. To avoid casting, you can employ the same approach explicitly, most of the time. (Some casts are required.)

    Doing it your way was pretty silly.

  6. #21
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    I think Matticus still has a point, though. If you only wanted to avoid a cast, you could have simply taken percentage variable and done this:
    Code:
    double percentage = passingGrades;
    percentage = percentage / total * 100;
    You can think of casting as an attempt by the compiler to store the value in an implicit variable of the desired type. To avoid casting, you can employ the same approach explicitly, most of the time. (Some casts are required.)

    Doing it your way was pretty silly.

    I kept thinking about this other program I help someone with that calculated grades, and it was different input, so it kept clouding my judgment on input. but I am ok now, thanks !

    so go look at what I just posted fixing my mess up. then tell me what you think. post #19
    Last edited by userxbw; 10-19-2017 at 03:28 PM.

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    If that is you doing this to me go try the two lines that I wrote in the OP's program and tell me what you think. post #20.

  8. #23
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    If that is you doing this to me go try the two lines that I wrote in the OP's program and tell me what you think. post #20.
    either way what I ended up with fixes for my complaints to the OP, I was misleading the witness, on the -1 check thing, and I must have been modifying that post #19 while you where posting. never the less it works either way. but my checks keeps it from counting if grades are -1 on either end of the spectrum. &&

    just using this
    Code:
    double passingGrades, failingGrades;
    int grades;
    Last edited by userxbw; 10-19-2017 at 03:39 PM.

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I only think that making the counting variables double type is unnecessary, and I wanted to suggest something that I know is less of a hassle. I'm not sure I follow everything else that you are talking about.

  10. #25
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    I only think that making the counting variables double type is unnecessary, and I wanted to suggest something that I know is less of a hassle. I'm not sure I follow everything else that you are talking about.
    I was being lazy and confused, so I just made everything a double, fixed. Not taking into account, and getting confused by OP code, and that what was being done with the -1 assigning it to grade when if grade = -1 quit

    use int to take in scores. double to do the math because of floating point math. grade does not get added into that equation, which my brain keep thinking it did, so that screwed me up. then got it to no casting and using the int for count, and replacing the check to -1 so if greater than 100 it will not be counted, or if lesser then 70 which -1 is it will not count it, or cause the code to add 1 to fail count. so I just made it to not accept grade -1 period, and no casting of data types.

    either way I am done and it works properly my code does anyways. the if else 's could perhaps be removed but that is not my problem.

    and now you're probably more confused.

  11. #26
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by userxbw View Post
    I was being lazy and confused, so I just made everything a double, fixed. Not taking into account, and getting confused by OP code, and that what was being done with the -1 assigning it to grade when if grade = -1 quit
    Not to be rude (and I apologize if I come across that way), but perhaps you should focus your effort on post quality instead of quantity. Take the time to read and understand the problems, and take the time to write up a thorough reply.

    Blasting several versions of the OP's program back at them with minor modifications usually just leads to more confusion on their part. Understanding what the problems are, and addressing them in a clear and concise way, will usually go much farther in helping them.

    It's great that you want to help people - that is really good for this community - just make sure your contributions are as good as they can be.

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