Thread: help with nested ifs

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    help with nested ifs

    Hey this is my homework assignment. I'm stuck on part c; which asks me to reduce the grade by 1 letter if assignement 1 or 2 is failed. I'm not sure how to do this because i can only express it if the value was to be presented as an integer; rather then a letter grade. Here is what i have so far:

    Code:
    #include<stdio.h>
    #include<conio.h>
    int main ()
    {
    int test1,test2,ass1,ass2;
    float av1,av2;
    
    
    printf("Please enter your first four marks\n");
    scanf("%d%d%d%d",&test1,&test2,&ass1,&ass2);
    
    
    av1=(test1+test2+ass1+ass2)/4;
    av2=av1-10;
    
    
    printf("Your average is %f\n", av1);
    if(test1<50 && test2<50){ 
    printf("The letter is F");
    }
    if(av1<50){ 
    printf("The letter is F");
    }
    else if(ass1<50){
    printf("The number %f is reduced by one grade",av1);
    }
    else if(ass2<50){
    printf("The number %f is reduced by one grade",av1);
    }
    else if(av1>50 && av1<59){
    printf("Your mark is D");
    }
    else if(av1>60 && av1<69){
    printf("Your mark is C");
    }
    else if(av1>70 && av1<79){
    printf("Your mark is B");
    }
    else if(av1>80 && av1<100){
    printf("Your mark is A");
    }
    else
    {
    printf("The number is invalid");
    }
    getch();
    }

    the assignment:
    1. Write the algorithm (pseudocode) for the following program definition: Read in a set of 4 marks for a student, representing two test marks followed by two assignment marks. Calculate and print a final grade, using the following information:
      a)All the marks are out of 100, and are equally weighted in the average.b)Grades are assigned based on A = 80 – 100, B = 70 – 79, C = 60 – 69, D = 50 – 59, F = 0 – 49c)If the student fails (i.e. mark < 50) either or both assignments, the grade is reduced by 1 (e.g. B becomes C).d)If the student fails either or both tests, the mark will be F.
    *you must use nested ifs*

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They wanted you to write pseudocode, not actual code. They wanted you to work out the logic on paper (or on your PC, whatever) on how to do it before attempting to do it.

    Lets say I have four ingredients, and I am supposed to make them into a sandwich. A sandwich is: one bread on the bottom, ingredients in the middle, one bread on top. No two ingredients will be next to one another. The ingredients come in random order.

    How do you make a sandwich?
    Code:
    for every ingredient
        if we have no bottom bread and this is not a piece of bread
            ignore ingredient
        else
        if we have a bottom bread and this is a bread
            if the last ingredient was not a bread
                done with sandwich
            else
                bread on bread, not a valid sandwich, ignore this item
        else
            /* some non-bread ingredient, and we have a bottom bread so... */
            if last ingredient is this ingredient
                ignore this ingredient because it's same on same
            else
                put this on the sandwich
    That looks about right.

    Now, you do something like that for your grade system.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    Ok thanks for the example. I'm still not sure about part c however. Part 2 of the assignment is to run the program using quincy and remove all errors, which i have done. I still don't understand how to display one grade below the one that has actually been calculated by the program!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You know of course, that A is right next to B, in the alphabet - naturally.

    What you may not have explored is that in the character set for your computer A is also right next to B. Typically A is 65 inside the computer, and B is 66. C is 67, etc.

    So to lower a grade, by one grade, you need to add one to the grade they have now, if that grade is A thru D. Don't lower an F grade, however. A+1 equals B!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    What you may not have explored is that in the character set for your computer A is also right next to B. Typically A is 65 inside the computer, and B is 66. C is 67, etc.

    So to lower a grade, by one grade, you need to add one to the grade they have now, if that grade is A thru D. Don't lower an F grade, however.
    Since this aspect of the character set is not guaranteed, however likely it may be at this time, I suggest associating the letter grade with a numeric grade, e.g., 0 could be mapped to 'F', 1 could be mapped to 'D', and so forth. This mapping can be easily performed by defining an array. Thus, you work with the numeric grades, but print the letter grades.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested While
    By skg29 in forum C Programming
    Replies: 4
    Last Post: 10-03-2011, 04:48 AM
  2. nested structs
    By adamantinex in forum C Programming
    Replies: 1
    Last Post: 09-14-2009, 01:13 AM
  3. nested for statements
    By EvilPickles in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2006, 05:36 AM
  4. Nested arrays in C++
    By Cat in forum C++ Programming
    Replies: 5
    Last Post: 06-09-2006, 03:37 PM
  5. Help with using nested For
    By webbizdesign in forum C Programming
    Replies: 5
    Last Post: 09-29-2003, 10:50 PM

Tags for this Thread