# Loop Function

This is a discussion on Loop Function within the C Programming forums, part of the General Programming Boards category; Originally Posted by blank Write a program to compute the grade for every student of a class. Use exactly the ...

1. ## Loop Function

Originally Posted by blank
Write a program to compute the grade for every student of a class.
Use exactly the same weights as in assignment #2 to determine the grade of each student. Then report the grade received by the student.
In addition, your program needs to output a summary report showing the total number of students, and the number of students receiving each letter grade.

To indicate that there is no more student, use a negative # as the first assignment score.
In other words, if assign. #1 score for the next student is negative; your program should terminate without the need to read in the rest of the scores of the student.

To allow mistakes in input, if any score (other than assign. #1) is out of the range of 0 – 100, do not count that student, and simply output an error statement, and move on to the next student.
In other words, ignore the rest of the scores for that student.
So basically as an assignment I must write a program that calculates the scores for students of a class and output a letter grade / etc, the rubric is attached. I am not asking for anyone to do the assignment, I have a program written to calculate the needed values for ONE student, but am wondering how to calculate it for multiple students while working with the rubric and through use of loop functions. I have attached the .cpp of the original program I wrote to calculate the average of one student.

The only real issue I'm having is taking the program I wrote for one student and making it follow the rubric, teacher speaks very little English and I'm having trouble self teaching myself.

2. Are you to assume there are possibly infinite students? Or is there a maximum class size? If the latter, use an array of whatever you used before for your single student. If the former, looks like a good time to learn about linked lists.

Quzah.

3. It's supposedly for infinite students I suppose. The real problem I'm having is the code itself, like. How do I take the program for one student and make it applicable to all students. The notes mention like a process similar to this:

i = 1; // loop initialization
while (i <= n) // loop test
{
output("current i = ",i);
processing;
i = i+1; // loop update
output("next i =",i);
system("pause")
}
statementA;

and an example for students.

printf("please input the number of scores,n:");
scanf("%d", &n);
sum = 0;
i = 1;
while (i <= n)
{
scanf("%d", &score);
sum = sum + score; i = i + 1;
}
printf("The total score=%f\n",sum);
avg = sum /n; printf("The average score=%f\n",avg);

the i represents the # of students, and which student is currently being worked on, n is the number of students. and sum represents the running total score. I'm just confused how to implement all of this into code!! It also mentions the use of a sentinel to keep the loop controlled, not sure how to do that either >:/.

Thanks

4. Code:
do
ask if they want to enter a student's grade, -1 to exit or whatever
if not -1
for each grade they want to enter
sum grades or whatever you're doing
note it some place (that they got an A or a C or what not)
while not -1 or whatever number you chose
Pretty much like that.

Quzah.

5. I have to make use of a "sentinel" to terminate the loop. He describes this as

"To indicate that there is no more student, use a negative # as the first assignment score.
In other words, if assign. #1 score for the next student is negative; your program should terminate without the need to read in the rest of the scores of the student."

So does this mean just a simple statement like.

if (assignment1 < 0);
{
return 0;
}

something like that?

6. So if your variable is called grade, maybe you should declare that instead of five separate variables that you never use.

7. yeah thats what I meant

but when

I get an error

8. I have another issue. These are the instructions

To allow mistakes in input, if any score (other than assign. #1) is out of the range of 0 – 100, do not count that student, and simply output an error statement, and move on to the next student.
In other words, ignore the rest of the scores for that student.
'
This is my code so far

But when I compile I get this error

parse error before `else'

9. Originally Posted by evangela
I have another issue. These are the instructions

To allow mistakes in input, if any score (other than assign. #1) is out of the range of 0 – 100, do not count that student, and simply output an error statement, and move on to the next student.
In other words, ignore the rest of the scores for that student.
'
This is my code so far

But when I compile I get this error

parse error before `else'
Well yes. An else must go with an if, not with a scanf.

10. Originally Posted by evangela
yeah thats what I meant

but when

I get an error
If that's how you declared it, then you don't get an error. If you get an error, then that's not what you did.

11. Ok, the assignment asks that if the first assignment grade is negative, it ends the loop or shuts down the program, which I think that part of my code is right. But it also says, if any other grade is less than 0 or more than 100 it skips that student and moves onto the next, but is there an easy way to do that in one code line? Since I have 4 scan Fs for assignments, and 2 scan Fs for tests and another for the final, is there someway I can have it see if the grade input is higher than 100 or less than 0 in one code line instead of having an if statement for every scanf?

Thanks

12. Code:
if grade is out of range
...don't actually do anything with this grade, and put an error message...
else
I'm not sure where you're hung up. I don't think you'd want to combine this with the -1 check, because it says to output an error message if it's out of range. You wouldn't put an error message if you were intentionally entering -1 to stop adding students.

Quzah.

13. Is it possible to do something like this?

else if(ass2, ass3, ass4 < 0 || ass2, ass3, ass4 > 100)
so that you don't have to have an else if statement for every single variable?

14. You can chain them together, but you need to do it differently. What you really should be doing is:
Code:
...
...bail out...
else
if( newgrade is out of range )
...error message...
else