
Simple loop
Hi, I need some help on the following problem:
An number of students have taken two exams, exam 1 and exam 2. (We don't know exactly how many) The marks obtained are to be typed into the program. (Using scanf() I presume)
The first two numbers typed in are the scores obtained by the first student. The next two numbers are scores for the second student and so on. Write a program that calculates and then print the average score for exam1 and for exam 2.
The scores entered must be within the 0100 range. Otherwise the marks should be ignored.
Here is what I have so far and it looks very messy:
Code:
#include <stdio.h>
int main()
{
int e1, e2, sum1, sum2;
sum1 = 0;
sum2 = 0;
scanf("%d %d", e1, e2);
if (e1 >= 0)
if (e1 <= 100)
sum1 = sum1 + e1;
scanf("%d %d", e1, e2);}
else
if (e2 >= 0)
if (e2 <= 100)
sum2 = sum2 + e2;
scanf("%d %d", e1, e2);
else
scanf("%d %d", e1, e2);
else
if (e2 >= 0)
if (e2 <= 100)
sum2 = sum2 + e2;
scanf("%d %d", e1, e2);
else
scanf("%d %d", e1, e2);
else
scanf("%d %d", e1, e2);
I know that we must somehow use the 'while' command but I don't exactly know how. How do I even start this problem?

Okay, how do you get the average of something?

For starters I would suggest spending time in coming up with a sound algorithm.

Start by entering the scores for exam 1 and 2 (s1 and s2). The average (a1 and a2) for exam1 will be the sum (sum1, sum2) of all the scores in exam1, likewise for exam 2. Then divide by the number (n) of students who had taken the test. Since n will increase by one everytime I enter a set of two scores, n++ could be used. But we start off with one student, so I initialized n=1. Same thing with sum1 and sum2
Code:
int s1, s2, sum1, sum2, n;
float average1, average2;
sum1 = 0;
sum2 = 0;
n= 1;
I will enter the two scores by scanf. Then we can start on the sum. Since for the first student, the initial sums are 0. I think I should put
Code:
scanf("%d %d", &s1, &s2);
sum1 += s1;
sum2 += s2;