# Thread: Multiple Expression Arguments in If Else Chain

1. ## 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. 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)`

3. ugh... That worked, that is what I get for assuming. Thanks!

4. 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)`

5. 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. Originally Posted by Cameron Taylor
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).

7. 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. 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!

9. 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. 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.

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.

Popular pages Recent additions