Simple loop

This is a discussion on Simple loop within the C Programming forums, part of the General Programming Boards category; Hi, I need some help on the following problem: An number of students have taken two exams, exam 1 and ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    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 0-100 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?

  2. #2
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Okay, how do you get the average of something?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,050
    For starters I would suggest spending time in coming up with a sound algorithm.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    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;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  2. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 01:31 PM
  3. simple collision detection problems
    By Mr_Jack in forum Game Programming
    Replies: 0
    Last Post: 03-31-2004, 04:59 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 05:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21