help with nested ifs

This is a discussion on help with nested ifs within the C Programming forums, part of the General Programming Boards category; Hey this is my homework assignment. I'm stuck on part c; which asks me to reduce the grade by 1 ...

  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
    22,303
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:48 AM
  2. nested structs
    By adamantinex in forum C Programming
    Replies: 1
    Last Post: 09-14-2009, 02:13 AM
  3. nested for statements
    By EvilPickles in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2006, 06:36 AM
  4. Nested arrays in C++
    By Cat in forum C++ Programming
    Replies: 5
    Last Post: 06-09-2006, 04:37 PM
  5. Help with using nested For
    By webbizdesign in forum C Programming
    Replies: 5
    Last Post: 09-29-2003, 11:50 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21