# Thread: 12 hours later I am giving a c exam need help solving sample question

1. ## 12 hours later I am giving a c exam need help solving sample question

Question:

85  100 H
75  84 D
65  74 C
50  64 P
0  49 N (4 points) Draw a flowchart to:
· get one students mark · decide and display which grade the mark is in ( make a series of if / else decisions) - 1 pt · add 1 (one) to the corresponding counter for grade HD and grade N
· ask if this is the last student · if not the last student, return to the start to get the next students mark
· if no more student, find if the number of students who score H is bigger than the number of students who score N
o if yes, print Good Result o else, print Poor Result mY ANSWER NEDDS EDITING and has some error please correct:
Code:
```#include<stdio.h>
int main()
{
float mark;
int hd=0;
int l=0;
char decision;

do{
printf("Enter mark of student:");
scanf("%f",&mark);
if(mark<=100 && mark>=85)
{
printf("H\n");

}
if(mark<=84 && mark>=75)

{
printf("D\n");

}

if(mark<=74 && mark>=65)

{
printf("C\n");

}
if(mark<=64 && mark>=50)

{
printf("P\n");

}
if(mark<=49 && mark>= 0)
{
printf("N\n");

}

if(mark<=100 && mark>=85)
{
hd=0;
l=hd+l;
l++;

}

printf("Is this the last student?\n");
scanf("%ch",&decision);

}while(decision!='y');

if(hd>l)
{
printf("Good Result");
}
else
{
printf("Poor Result");
}

}``` 2. Add a space after the %f, on line 11

Add the int variable's of h and n. Assign zero to both of them. Remove l.

If the marks are either earning an H or an N, then h++ or n++.

and welcome to the forum! P.S. i guess "hd" could work for the "h" variable, just fine.

You'll need to replace the el variable, though. Looks too much like a 1 or an I. 3. What does "some error" mean?

Code:
```\$ gcc -Wall bar.c
bar.c: In function ‘main’:
bar.c:67:1: warning: control reaches end of non-void function [-Wreturn-type]```
Edit:
> Add a space after the %f, on line 11
Adding a space before the %c would be better.

Further, "%ch" really doesn't do what you think it should.

Unless you insist on typing
"yh"
or
"nh"
for every response to the yes/no question. 4. where should I write h++ and n++? 5. line 15 for h++.

line 44-50 should be deleted.

line 40 should be n++. 6. But in the sample question it is said to ask the user if it is the last student?. We are supposed to take the marks multiple students. If the total number of H and D exceeds the number of N then good result is displayed. Can you please show in proper codes where I am going wrong? 7. No I won't do that.

When you enter a high score, you add one to the number of students with a high score, h++ (or hd if you like)
When you enter a low score, you add one to the number of students with a low score: n++;

After you have entered all the students scores, you compare to see if (h > n) or not. If so, then print Good Result, if not, print Bad Result. 8. I finally got it right!
Code:
```#include<stdio.h>
int main()
{
float mark;
int h=0;
int n=0;
char decision;

do{
printf("Enter mark of student:");
scanf("%f",&mark);
if(mark<=100 && mark>=85)
{
printf("H\n");
h++;

}
if(mark<=84 && mark>=75)

{
printf("D\n");
h++;

}

if(mark<=74 && mark>=65)

{
printf("C\n");

}
if(mark<=64 && mark>=50)

{
printf("P\n");

}
if(mark<=49 && mark>= 0)
{
printf("N\n");
n++;

}

printf("Is this the last student?\n");
fflush(stdin);
scanf("%c",&decision);

}while(decision!='y');

if(h>n)
{
printf("Good Result");
}
else
{
printf("Poor Result");
}

}``` 9. Look for fflush(stdin) on the FAQ -> you shouldn't use it. 10. > I finally got it right!
Erm, no you didn't.

> fflush(stdin);
You put this in, instead of reading posts from myself and Adak regarding the use of space in format strings.
FAQ > Why fflush(stdin) is wrong - Cprogramming.com 11. Two suggestions Burns:

Code:
```          fflush(stdin);           //remove this fflush() works on OUTWARD streams, not inward ones
scanf(" %c",&decision);  //note the space added before the % - will help, here```
And well done!  Popular pages Recent additions 