Need Help on Program Using For Loop

This is a discussion on Need Help on Program Using For Loop within the C++ Programming forums, part of the General Programming Boards category; How do I write a program using for loop to average 3 integer test grades? Please help!!!!! I/P : 3 ...

  1. #1
    Unregistered
    Guest

    Need Help on Program Using For Loop

    How do I write a program using for loop to average 3 integer test grades? Please help!!!!!

    I/P : 3 integer test grades
    O/P: Exam average (float or double)

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Post what you have done so far.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Unregistered
    Guest
    Is this even close? I'm not sure I quite understand, but I'm trying! Please help!

    #include <iostream.h>
    #include <iomanip.h>

    int main ()

    {

    int total; //sum of grades
    int counter;
    int grade;
    double average;

    total=0;
    counter=0;

    for (counter=1;
    counter<=3;
    counter++)

    {
    total=total+grade;
    counter= counter+1
    cout<<"Enter grade, -1 to end:";
    cin>>grade;
    }

    average= total/counter;
    cout<<\n "Your exam average is:" <<average;

    cout<<"\n End of lab";

    return 0;
    }
    //end of main function

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Close.

    > counter=0;

    This is unecessary, since you're just giving counter another value on the next line anyways.

    > counter= counter+1

    This is also unecessary, since the "counter++" in your for loop will do this. The way you have it now, counter's value is increased by 2 on every pass.

    > -1 to end

    There's really no reason to have this in, since you don't deal with what happens if they put in -1.

    Looks fine, otherwise...

  5. #5
    Unregistered
    Guest
    for (counter=1;
    counter<=3;
    counter++)
    ==============================
    with above instructions the loop will stop when counter == 4. Work it out by hand to prove it to yourself. If you start counter at 0 and increment until counter < 3, then the value of counter will (often) equal the number of grades entered when loop is terminated. This is one reason why loop counters frequently start at 0 rather than one.
    ===============================

    {
    total=total+grade;
    ================================
    ask yourself what value grade has the first time through the loop. It is undefined right? The user hasn't had the chance to enter the grade at this point yet, right? Therefore you will be assigning garbage to total the first time through, ang building on garbage each additional loop through. Also, ask yourself what happens to user input the third time through the loop. Will user input be added to total? Nope. Write out a description for yourself as to why that is so. This line is correct in syntax but inappropriate in location. I'll leave it to you to figure out where it should go.
    =================================

    counter= counter+1;
    =================================
    this line should be eliminated as per previous post
    ==================================

    cout<<"Enter grade, -1 to end:";
    ==================================
    I actually like the idea here. You are allowing user to terminate the loop early if they have less tha three grades to enter. However, you have to deal with the input of -1 somehow. Here's one way:

    cin>>grade;
    if(grade == -1)
    {
    break;
    }

    I would actually prefer a slightly different (although not necessarily better) approach to this aspect of the program, but the above sequence should allow flexibility for the user.
    ==================================

    cin >> grade;

    }

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    33
    would it be easyer to just have:

    double a, b, c;
    for(;;)//just add this if you want this to loop forever, I know unorthodox, but works.
    {
    cout <<"please enter your grades";
    cin>>a>>b>>c;
    a = (a+b+c)/3
    cout <<"you're GPA is "<<a<<endl;
    }

    or you could even add something like
    if (a > 3.3)
    cout <<"you're average is an a";
    else if (a < 3.3 && > 2.7)
    cout <<"you have a b";
    you get the jist.

    or do you really have your heart set on a for loop? anyways, hope this helps.
    Last edited by Dummies102; 02-25-2002 at 04:07 PM.

  7. #7
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > but(I forget the boolean function for but)

    There is no "boolean function" for but. You want and, which is &&.

  8. #8
    Unregistered
    Guest

    Smile

    Thanks so much for helping!

  9. #9
    Unregistered
    Guest

    Unhappy

    I have to use a for loop. I know it would be much easier the other way.
    ---------------------------------------------------------------------------------
    Once I think I know what I'm doing, I'm confused again. I really don't know what I'm doing or why. I'm just trying to write the program.
    ---------------------------------------------------------------------------------
    for (counter=1;
    counter<=3;
    counter++)

    So I need to change counter=1 and counter<3 ? I'll try it, but my teacher had that up for an example in class.
    ---------------------------------------------------------------------------------
    {
    total=total+grade;

    I'm sorry I don't know what I'm doing here. And I still don't quite understand.

    Any more help on this will greatly be appreciated. Thanks!

  10. #10
    Unregistered
    Guest

    Talking

    const int MAX_INPUT = 3;

    int grade[ MAX_INPUT ];
    int total;

    for( counter = 1; counter <= MAX_INPUT; counter++ )
    {
    cout << "Enter number " << counter << ":\n"
    cin >> grade[ i ];
    total += grade[ i ]
    }

    cout << "Average = " << total / MAX_INPUT << endl;

  11. #11
    Unregistered
    Guest
    thanks, but that might be a bit more advanced.. i'm only in the introductory class..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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