# Thread: New guy help

1. ## New guy help

All

First thanks for whatever help you can offer. i am trying to finish up a degree is statistics and the powers that be say i have to have a programming class. I am woefully uneducated when it comes to everything besides email and google. So I humbly beg of your help.

My second assignment goes something like this:

write a program to convert test scores from numeric to letter grade

Question one:

what is the best way to do inequalities? I can get "(score >= 90) printf("Grade is A) but I can only use one other inequality because of the left side right side thing.

Question two:

Teacher wants program to loop so user can continue to enter grades. I have the loop set up, but don't know how make the program read y and n for commands to yes repeat or no stop.

Any help that can be offered would be greatly apprecaited. I am currently reading the rules on how to post code.

chip

2. Code:
```/*Define maximum and minimum numeric grade values*/
#define LOW 0
#define HIGH 100
#define YES 1
#define NO 0
#define A 90
#define B 80
#define C 70
#define D 60
#define F 59

int grading (int score);

int main()  {

int a_score, process, again, intvar;

again = YES;
printf("This program will assign a letter grade to a numeric test grade");

while ( again == YES) {
/*prompt user and call function to get the test grade, assign returned value to grade in main */
printf("\n\nPlease enter the numeric test grade.  Grade must be between %d and %d:  ", LOW, HIGH);
scanf("%d%*c",&a_score);
process = grading (a_score);
/*prompt user and call function to determine whether another test grade will be entered*/
printf("\nWould you like to enter another test grade? y/n");
scanf("%d%*c", &again);
}

return 0;

}

int grading (int score)
{

if (score >= A)
if (score < A)

return YES;

}

int repeat (again)
{

while (again == 'y' || again == 'Y')
return YES;
}```

3. For question one you need to use else statement in order to only print one result.
Code:
```if(score >= 90)

else if(score >= 80)  //if is not >= than 90, but >= than 80..

else if(score >= 70) //If is not >= than 80, but >= than 70
There are also constants called "true" and "false" which can be used instead of your defined yes/no

4. [QUOTE=39ster;785323]For question one you need to use else statement in order to only print one result.
QUOTE]

It seems so obvious when someone tells you how to go about it. I tried similar things to that. i just didn't realize the above statements would bound the ones below. Thanks very much for the help.

Chip

5. Originally Posted by chip.litch
i just didn't realize the above statements would bound the ones below.
They are only bound with an else clause. The else if statement really is a shortcut for the following:
Code:
```if (grade >= 81) {
printf ("A");
} else {
if (grade >= 70) {
printf ("B");
} else {
if (grade >= 60) {
printf ("C");
}
}
}```
Which of course is ugly.

QuantumPete

6. An alternative solution is something like this:
Code:
```    char letter = 'F';
if (grade >= 60) letter = 'D';
if (grade >= 70) letter = 'C';
if (grade >= 80) letter = 'B';
if (grade >= 90) letter = 'A';```
I'm not suggesting you do this - it's pretty inefficient, but it is sometimes useful to know that you can do this sort of thing. It is of course important to have things in the right order, the following would obviously not work right:
Code:
```    char letter = 'F';
if (grade >= 60) letter = 'D';
if (grade >= 70) letter = 'C';
if (grade >= 90) letter = 'A';
if (grade >= 80) letter = 'B';```
--
Mats

7. Code:
```int main()  {

int a_score, process, again, intvar;

again = YES;
printf("This program will assign a letter grade to a numeric test grade");

while ( again == YES) {
/*prompt user and call function to get the test grade, assign returned value to grade in main */
printf("\n\nPlease enter the numeric test grade.  Grade must be between %d and %d:  ", LOW, HIGH);
scanf("%d%*c",&a_score);
process = grading (a_score);
/*prompt user and call function to determine whether another test grade will be entered*/
printf("\nWould you like to enter another test grade? y/n");
scanf("%d%*c", &again);
/* You do not break out of the loop. So you have an infinite loop.
* Probably because it's a work-in-progress. */
}

return 0;

}

int repeat (again) /* You do not have a type here */
{

while (again == 'y' || again == 'Y')
return YES;
}```

Popular pages Recent additions