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)
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)
Post what you have done so far.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
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
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...
-Govtcheez
[email protected]
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;
}
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 05:07 PM.
> but(I forget the boolean function for but)
There is no "boolean function" for but. You want and, which is &&.
-Govtcheez
[email protected]
Thanks so much for helping!
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!
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;
thanks, but that might be a bit more advanced.. i'm only in the introductory class..