Take this piece of code:
for(grade=0; grade<num; grade++)
{
inp1>>score1[grade];
inp2>>score2[grade];
inp3>>score3[grade];
avg=(score1[grade]+score2[grade]+score3[grade])/3;
}
cout<<avg<<endl;
Run it by handon paper for 3 students. What does avg display?
Be sure you execute each statement exactly as written, not as you think it should be.
And please learn now to format your code properly. It helps us help you, and you yourself can see what's happening easier.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Sorry, I am having trouble understanding what you are trying to say. Do you suggest that I insert
cout << avg << endl;
after the for loop? Or are you saying that there is a mistake within the for loop?
Really? OK:
Print out just that piece of code -- the loop.
Sit at your desk.
Put that pice of code in front of you.
Pick up a pencil.
Write down some numbers labeledFILE that will be the values of the 'file' you are going to read (based on your assignment). Use only 3 students for this.
Looking at those numbers, what should the answer be for the class average (based on your assignment). Write it down.
You can also calculate each student's average. Write them down, too.
Now, start with the FOR statement and write down
num = whatevernum is (from 'file' -- it's 3)
grade = 0 since that's what the FOR does
Go to the next line and "input" (write down) a value forscore1[grade] (the first number from your 'file')
Continue to the next statement and do what it says.
Keep doing this until you have finished the loop.
What's the value of avg? If avg is correct, you didn't follow your loop as written. If avg is wrong, why? If you don't know, do it again and really look at what you are doing.
As for "And please learn now to format your code properly. It helps us help you, and you yourself can see what's happening easier", just follow the link.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Good.
By the way, you can make these IFs simpler:
if(90 <= avg)
letter = 'A';
else if(80 <= avg && avg < 90) // for this if is it even possible for AVG
// to be >= 90?
letter = 'B';
else if(70 <= avg && avg < 80) // Same here with 80...
letter = 'C';
else if(60 <= avg && avg < 70)
letter = 'D';
else
letter = 'F';
Also if(90 <= avg) is confusing to most professionals. if(avg > 90) is the (unwritten) standard format. But this is not set in stone.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944