Thread: Guidance - Grading Issues/variable problem

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    8

    Guidance - Grading Issues/variable problem

    Ugh...I'm probably pulling up threads from the grave here, but I've read the ones that deal with sort of this issue, however it's setup differently than what I'm currently doing.

    I'm stumped and need a bit of guidance on this particular code.

    I've built everything out, however I'm trying to figure out how to collect the data for my "validGrades" variable.

    Is there a way to collect the data from the "else" and have it dump it's information into the "validGrades"?

    As you can see from my code, I have it commented out, along with my equation/formula below. The only thing missing is my "validGrades" that I just can't figure out how to implement.

    My lack of knowledge in this is preventing me from moving forward. Can anyone drop some knowledge bombs on me?
    Sorry, I'm completely new to this.

    Thanks!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main(){
    
    
        //variables
        int score;
        int passingGrades = 0;
        int percentagePassing = 0;
        int validGrades = 0;
    
    
        //getting data from the user
        printf("Enter a test score (-1 to quit): \n");
        scanf_s("%i", &score);
    
    
        //while loop
        while (score != -1){
            if (score > 100 || score < 0){
                printf("This is NOT a valid grade. \n");
                printf("Please enter a valid grade. \n");
            } else {
                passingGrades = passingGrades + (score >= 70);
                //validGrades = 
                printf("Is a valid grade. \n");
                printf("Number of valid grades: %i \n", validGrades);
                printf("Number of passing grades: %i \n", passingGrades);
            }
            printf("Enter a test score (-1 to quit): \n");
            scanf_s("%i", &score);
        }
    
    
        //output results
        printf("The number of passing grades is: %i \n", passingGrades);
    
    
        //percentagePassing = (passingGrades / validGrades) * 100;
        printf("%i%% of the valid grades entered are passing grades. \n", percentagePassing);
    
    
        //bring up console window
        system("pause");
    
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    well looking at your code, starting off your MAIN should be a returning function (int main)
    which also means your "end of program" would include return 0

    now running the program, this was what I got the first grade entered

    Enter a test score (-1 to quit):
    10
    Is a valid grade.
    Number of valid grades: 0
    Number of passing grades: 0

    and if I put in 100, says same thing, but does add the passing grade!

    //------edit-----//

    ok missed that, why use scanf_s?
    and I would looking into Loop Counter for your valid grade count!
    Last edited by Crossfire; 02-23-2015 at 11:50 AM.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    i think in the else part you should check if score is greater than equal to 70, in that if loop you should add score with passing grade as passinGrades = passingGrades + score and increment validGrades as validGrades+1.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    ok I would say your percentage formula is backwards! (follows the solving of cross multiply)
    passing grades X
    ---------------- = ---
    valid grades 100

    either way, you need to put a check in there, to make sure your not dividing by 0! it will crash your program!


    Satya

    if you check score the way you say, it would add score to the passingGrade!

    in her current code with the if, (score >= 70) returns 1 if true, and 0 if not, so the code is fine!

  5. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    8
    Okay...I'm sort of understanding, especially adding an extra counter in there.
    However now it's counting double on the passingGrades as 1...then the next for validGrades is catching the 1 as well, and adding another 1...so creating 2. When there is only 1.

    I created this, but yeah, I need to fix the formula...

    Code:
    main(){
    
    
        //variables
        int score;
        int passingGrades = 0;
        int percentagePassing;
        int validGrades = 0;
    
    
        //getting data from the user
        printf("Enter a test score (-1 to quit): \n");
        scanf_s("%i", &score);
    
    
        //while loop
        while (score != -1){
            if (score > 100 || score < 0){
                printf("This is NOT a valid grade. \n");
                printf("Please enter a valid grade. \n");
            } else {
                passingGrades = passingGrades + (score >= 70);
                validGrades = passingGrades + (score >= 70);
                printf("Is a valid grade. \n");
                printf("Number of valid grades: %i \n", validGrades);
                printf("Number of passing grades: %i \n", passingGrades);
            }
            printf("Enter a test score (-1 to quit): \n");
            scanf_s("%i", &score);
        }
    
    
        //output results
        printf("The number of passing grades is: %i \n", passingGrades);
    
    
        percentagePassing = (passingGrades / validGrades) * 100;
        printf("%i%% of the valid grades entered are passing grades. \n", percentagePassing);
    
    
        //bring up console window
        system("pause");
    
    
    }

  6. #6
    Registered User
    Join Date
    Feb 2015
    Posts
    8
    Oops...however I guess this wouldn't work, because I need all valid Grades, 0-100...not just scores of 70.

  7. #7
    Registered User
    Join Date
    Feb 2015
    Posts
    8
    Uhmm...did I just get this? lol...I feel so dumb as I'm so new to this stuff...

    Code:
    main(){
    
    
    	//variables
    	int score;
    	int passingGrades = 0;
    	int percentagePassing;
    	int validGrades = 0;
    
    
    	//getting data from the user
    	printf("Enter a test score (-1 to quit): \n");
    	scanf_s("%i", &score);
    
    
    	//while loop
    	while (score != -1){
    		if (score > 100 || score < 0){
    			printf("This is NOT a valid grade. \n");
    			printf("Please enter a valid grade. \n");
    		} else {
    			passingGrades = passingGrades + (score >= 70);
    			validGrades = validGrades + (score >= 100);
    			printf("Is a valid grade. \n");
    			printf("Number of valid grades: %i \n", validGrades);
    			printf("Number of passing grades: %i \n", passingGrades);
    		}
    		printf("Enter a test score (-1 to quit): \n");
    		scanf_s("%i", &score);
    	}
    
    
    	//output results
    	printf("The number of passing grades is: %i \n", passingGrades);
    
    
    	percentagePassing = (passingGrades / validGrades) * 100;
    	printf("%i%% of the valid grades entered are passing grades. \n", percentagePassing);
    
    
    	//bring up console window
    	system("pause");
    
    
    }

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Why cannot you write validGrades = validGrades + 1;?

  9. #9
    Registered User
    Join Date
    Feb 2015
    Posts
    8
    Yeah, sorry, it's still having issues.

    Also, sorry, it's because if I do a validGrades + 1...won't it read all inputs?
    An example is because what I'm following is, validGrades are from 0 - 100.

    So if someone types in 123, that shouldn't be valid. Or a -2.

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    SATYA, you are still a little off!!!

    AccipiterZ, that is why I said look into the "Loop Counter for your valid grade count"

    in a loop for example

    Code:
    for(int x = 0;x < 10;x++)
    everyloop through the for, x is counted up one (x++)

    and because the "counter" would be in your else section, it will only add it once per every valid grade!

  11. #11
    Registered User
    Join Date
    Feb 2015
    Posts
    8
    Well, I guess with a
    Code:
    validGrades = validGrades +  1;
    It looks like it's counting correctly.
    So if I input, example:

    45
    90
    70
    87
    123
    100

    It is counting 5 valid grades, and 4 passing grades. Which is correct.

    Is that because it's hitting the if statement first?



    Now my problem seems to be the formula.

    Code:
    main(){
    
    
        //variables
        int score;
        int passingGrades = 0;
        int percentagePassing = 0;
        int validGrades = 0;
    
    
        //getting data from the user
        printf("Enter a test score (-1 to quit): \n");
        scanf_s("%i", &score);
    
    
        //while loop
        while (score != -1){
            if (score > 100 || score < 0){
                printf("This is NOT a valid grade. \n");
                printf("Please enter a valid grade. \n");
            } else {
                passingGrades = passingGrades + (score >= 70);
                validGrades = validGrades + 1;
                printf("Is a valid grade. \n");
                printf("Number of valid grades: %i \n", validGrades);
                printf("Number of passing grades: %i \n", passingGrades);
            }
            printf("Enter a test score (-1 to quit): \n");
            scanf_s("%i", &score);
        }
    
    
        //output results
        printf("The number of passing grades is: %i \n", passingGrades);
    
    
        percentagePassing = (passingGrades / validGrades) * 100;
        printf("%i%% of the valid grades entered are passing grades. \n", percentagePassing);
    
    
        //bring up console window
        system("pause");
    
    
    }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is problematic because of integer division:
    Code:
    percentagePassing = (passingGrades / validGrades) * 100;
    Furthermore, you should check that validGrades is greater than 0, otherwise you risk dividing by 0.
    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

  13. #13
    Registered User
    Join Date
    Feb 2015
    Posts
    8
    Okay...I'm understanding a bit more now...

    I think I've ended up forcing/changing it into a double, which looks like it's helping the problem. I wasn't thinking about the integer division.

    I think I've got it with all of your help...geez...this is baby code probably compared to what you guys do.

    So if I put in the examples:

    45
    90
    70
    87
    123
    100
    It comes up with 80%.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main(){
    
    
        //variables
        int score;
        int passingGrades = 0;
        double percentagePassing;
        int validGrades = 0;
    
    
        //getting data from the user
        printf("Enter a test score (-1 to quit): \n");
        scanf_s("%i", &score);
    
    
        //while loop
        while (score != -1){
            if (score > 100 || score < 0){
                printf("This is NOT a valid grade. \n");
                printf("Please enter a valid grade. \n");
            } else {
                passingGrades = passingGrades + (score >= 70);
                validGrades = validGrades + 1;
                printf("Is a valid grade. \n");
                printf("Number of valid grades: %i \n", validGrades);
                printf("Number of passing grades: %i \n", passingGrades);
            }
            printf("Enter a test score (-1 to quit): \n");
            scanf_s("%i", &score);
        }
    
    
        //output results
        printf("The number of passing grades is: %i \n", passingGrades);
    
    
        percentagePassing = ((double)passingGrades / validGrades) * 100;
        printf("%.2lf%% of the valid grades entered are passing grades. \n", percentagePassing);
    
    
        //bring up console window
        system("pause");
    
    
    }
    Last edited by AccipiterZ; 02-23-2015 at 01:20 PM.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    passingGrades = passingGrades + (score >= 70);
    While this line technically accomplishes what you're trying, I'd suggest using an "if" to determine whether "passingGrades" should be incremented - it's just more clear.

    In fact, I'd be curious to know if you understand how "passingGrades" is incremented in that snippet.

  15. #15
    Registered User
    Join Date
    Feb 2015
    Posts
    8
    I had someone else tell me to adjust that as well, with something like...

    Code:
    passingGrades += (score >= 70);
    Which honestly I don't understand the "+" of it and I'll have to look that up.

    However, I do understand...

    Code:
    passingGrades = passingGrades + (score >= 70);
    passingGrades is currently set to 0.
    When a "score" is typed in by the user, it's checking whether it's greater than or equal to 70.
    If it is, it's creating a "1".
    As passingGrades is currently set to 0, 0 +1 is now 1.
    And so on.

    passingGrades is anything 70 and above that is typed in by the user.
    passingGrades is now being saved into memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Grading
    By archriku in forum C Programming
    Replies: 12
    Last Post: 03-25-2009, 08:46 AM
  3. Grading in c++
    By findme in forum C++ Programming
    Replies: 9
    Last Post: 11-18-2005, 09:02 AM
  4. Program for grading
    By MasterLika in forum C Programming
    Replies: 3
    Last Post: 04-04-2005, 01:33 AM
  5. Grading AP CS Exams
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 06-18-2004, 03:46 PM