Like Tree1Likes
  • 1 Post By std10093

Multiple Expression Arguments in If Else Chain

This is a discussion on Multiple Expression Arguments in If Else Chain within the C Programming forums, part of the General Programming Boards category; Ok, I got everything else to work in a two part exercise except for the 2nd part. The first part ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    70

    Multiple Expression Arguments in If Else Chain

    Ok, I got everything else to work in a two part exercise except for the 2nd part.

    The first part had me take up to 20 grades, calculate total, average, and then list the grades with a '*' next to ones that were lower than the average... good to go.

    Now in the 2nd part of the exercise I have to also associate a letter grade with the grades in the list. I have done a "if else" chain, but am having an issue with multiple arguments and not sure if I have gone the right route.

    Code:
    //Cameron Taylor
    
    #include <stdio.h>
    #include <math.h>
    
    #define MAXARRAY 20
    
    int main(){
    
        int usrGrade, total, i, gradecount = 0, grades[MAXARRAY] = {0};
        double average;
    
        printf("\nThis program calculates the total and average of up to 20 student grades. \nTo stop entering grades before 20, enter a negative number.\n\n");
    
        do {
            printf ("Enter a grade: ");
            scanf ("%d", &usrGrade);
            if (usrGrade >= 0)
            {
                grades[gradecount] = usrGrade;        
                total += usrGrade;
                gradecount++;
            }
        } while (gradecount < MAXARRAY && usrGrade >= 0);
    
    
        average = total/gradecount;
        
        printf("\nThe sum total of grades entered: %d \n", total);
        printf("\nThe average of grades is: %3.2f \n", average);
         
        printf("\nThe grades entered are now associated with average.\nAn * indicates the grade is below the average.\n");
            
            for (i = 0; i < gradecount; i++){
    
                if (grades[i] >= average && <= 100 && > 89)
                    printf("A - %d\n", grades[i]);
                else if (grades[i] >= average && <= 100 && > 89)
                    printf("A - *%d\n", grades[i]);
                else if (grades[i] >= average && <= 89 && > 79)
                    printf("B - %d\n", grades[i]);
                else if (grades[i] < average && <= 89 && > 79)
                    printf("B - *%d\n", grades[i]);
                else if (grades[i] >= average && <= 79 && > 69)
                    printf("C - %d\n", grades[i]);
                else if (grades[i] < average && <= 79 && > 69)
                    printf("C - *%d\n", grades[i]);
                else if (grades[i] >= average && <= 69 && > 59)
                    printf("D - %d\n", grades[i]);
                else if (grades[i] < average && <= 69 && > 59)
                    printf("D - *%d\n", grades[i]);
                else if (grades[i] >= average && <= 59 && > 0)
                    printf("F - %d\n", grades[i]);
                else
                    printf("F - *%d\n", grades[i]);
            }
            
        getchar();
        return 0;
        
    
    }
    Part in question that I am having trouble with:

    Code:
            for (i = 0; i < gradecount; i++){
    
                if (grades[i] >= average && <= 100 && > 89)
                    printf("A - %d\n", grades[i]);
                else if (grades[i] >= average && <= 100 && > 89)
                    printf("A - *%d\n", grades[i]);
                else if (grades[i] >= average && <= 89 && > 79)
                    printf("B - %d\n", grades[i]);
                else if (grades[i] < average && <= 89 && > 79)
                    printf("B - *%d\n", grades[i]);
                else if (grades[i] >= average && <= 79 && > 69)
                    printf("C - %d\n", grades[i]);
                else if (grades[i] < average && <= 79 && > 69)
                    printf("C - *%d\n", grades[i]);
                else if (grades[i] >= average && <= 69 && > 59)
                    printf("D - %d\n", grades[i]);
                else if (grades[i] < average && <= 69 && > 59)
                    printf("D - *%d\n", grades[i]);
                else if (grades[i] >= average && <= 59 && > 0)
                    printf("F - %d\n", grades[i]);
                else
                    printf("F - *%d\n", grades[i]);
            }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Code:
    if (grades[i] >= average && <= 100 && > 89)
    This is C, so you have to write this as
    Code:
    if (grades[i] >= average && grades[i] <= 100 && grades[i] > 89)
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    ugh... That worked, that is what I get for assuming. Thanks!

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Good you had assumed it. If I enter only one negative integer, let's say -1, what will happen? First think of it and then run it.

    We also use to write
    Code:
    int main(void)
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Well when I enter a negative number it is the sentinel to stop inputing grades. It is supposed to take up to 20 grades, but you don't have to do 20.

    Print out:

    commiedic@localhost Project12$ ./Exercise5a

    This program calculates the total and average of up to 20 student grades.
    To stop entering grades before 20, enter a negative number.

    Enter a grade: 80
    Enter a grade: 90
    Enter a grade: 70
    Enter a grade: 60
    Enter a grade: 50
    Enter a grade: 40
    Enter a grade: 30
    Enter a grade: -1

    The sum total of grades entered: 420

    The average of grades is: 60.00

    The grades entered are now associated with average.
    An * indicates the grade is below the average.
    B - 80
    A - 90
    C - 70
    D - 60
    F - *50
    F - *40
    F - *30
    commiedic@localhost Project12$

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Quote Originally Posted by Cameron Taylor View Post
    Well when I enter a negative number it is the sentinel to stop inputing grades.
    Yes I agree. In your output, you have given normal data. Input -1 and hit enter. Then what the average of the grades? Why the average is what it will be?

    EDIT:
    You should always think of the case that the input is not going to be what you assume it will be. A good practice, is that you let others input your program, before you submitted. The others were just me in that case
    Here is the reason (why you should do that).
    Name:  a.aaa-Programmers-VS-User.jpg
Views: 96
Size:  45.7 KB
    Last edited by std10093; 07-09-2013 at 07:48 PM.
    Salem likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Ah, I think I see what you are saying. Their should be a check and message for the user if they enter a negative first or possibly a character, symbol etc...

    Well I will definitely look into it, but I got another assignment I gotta get done before midnight tonight. As it stands right now it works enough to get the credit, but if I have time I will probably go back and add those other fail saves.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    I see. Ok. Well, when you have time, I suggest you make the program taking into consideration if the input is going to be a negative number only. Do not mind for a character, symbol, etc. yet

    Good luck with the next assignment, but I would say you are going to do great, as you did with that one

    Goodnight from Greece!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    It seems to me that you could use a switch statement to clean up your code, it would probably increase readability also. I think there are some schools that say you can't use certain things before you learn them, but I'll show it to you anyways

    Code:
    switch( variable )
    {
    	case 'A' :
    		do_something();
    		break;
    	case 'b' : 
    		do_something_else();
    		break;
    	default :
    		fprintf( stdout, "I don't know the value %c!", variable);
    		break;
    }
    That is just a pseudo example of what the switch statement could do. If you can use it in your code, I would. It would make dealing with unknown input a lot easier, and remove those nasty if-else statements. I've never tried it before, but I think you could even do something like case ('A'...'Z') : to test for the whole uppercase alphabet ( you could try it, I'm just making assumptions since that would work with integers, and technically, characters are just numbers too. ). Anyways, that is my suggestion.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by HelpfulPerson
    It seems to me that you could use a switch statement to clean up your code (...) It would make dealing with unknown input a lot easier, and remove those nasty if-else statements.
    The letter grades are awarded for grades within a range, whereas a switch is better suited for particular integral values. There isn't anything nasty about using an if-else chain here compared to using a switch.

    Quote Originally Posted by HelpfulPerson
    I've never tried it before, but I think you could even do something like case ('A'...'Z') : to test for the whole uppercase alphabet ( you could try it, I'm just making assumptions since that would work with integers, and technically, characters are just numbers too. ).
    That is not valid standard C, so no, that would not work with integers either, and in fact 'A' is of type int.
    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. Pthread question, multiple arguments
    By Alix in forum C Programming
    Replies: 2
    Last Post: 02-12-2012, 09:58 AM
  2. Strtok - Expected expression and too few arguments?
    By TheWhiffet in forum C Programming
    Replies: 5
    Last Post: 07-26-2011, 07:22 AM
  3. help with pthread and multiple arguments
    By user_name_void in forum C Programming
    Replies: 4
    Last Post: 02-17-2010, 02:23 PM
  4. Sending multiple arguments to function..
    By Zzaacchh in forum C++ Programming
    Replies: 3
    Last Post: 09-06-2008, 03:20 PM
  5. order of multiple test expression?
    By wolf in forum C Programming
    Replies: 5
    Last Post: 02-05-2002, 03:33 PM

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