| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
FOR (LOOP) question!!!
when using a for loop how is it possible to use the data collected. for example in this short program how would it be possible to access the entered amounts.
Code:
int main(void)
{
float testScore = 0;
cout<<"Test Scores For The Semester\n";
for(int i = 0; i <= 5; i++)
{
cout<<"Score "<<i<<":";
cin>>testScore;
}
return 0;
}
how would I access the five test scores after they are entered. Like, if I wanted to average them all up. I know this is really a question but for some reason I cant quite grasp it!! |
|
#2
|
||||||||
|
||||||||
|
Well, first of all, you'd have to read them all in seperately, right now, each one is overwriting the previous one. Try using an array, like this:
cpp Code:
Now, if you want to calculate the average, you have to calculate the total too. You can do this after reading all the variables, (another for loop) but the faster way is to do it in the same loop: cpp Code:
Hope this helps.
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 275
![]() |
|
#3
|
||||
|
||||
|
Ty
Thanks for that example, that's a BIG help that's for sure. I guess I don't really understand them mostly because there's so many statements in a small space and although its easy to read through an example in a book and see how that guy would use it, but once its time to do one of your own design that's a little different, ya know, or maybe its just me
But anyways Thx again ![]() Hey but before I quit picking your brain, I got an example that for some reason, stumped the crap out of me, So, here's the question. NOT homework, its a test question out of the book C++ Primer Plus(5th Chapter); Quote:
So here's what I got for the answer, and I know its not right, but !! Code:
int main (void)
{
int one = 0;
int two = 0;
cout<<" Enter The First Number \n";
cin>>one;
cin>>two;
int sum = one + 1;
for (int i = 1; i <= one%two;i++)
{
cout<<" Value "<<i<<" : "<<one++ + sum++<<endl;
}
system("PAUSE");
return 0;
}
But Im going to try it again with my newly acquired knowledge ![]() |
|
#4
|
|||||||||||
|
|||||||||||
|
Hmm, you've been here long enough to be more than someone who doesn't want to do his homework. Either that or you get a LOT of homework :-)
anyway, here are some solutions to the problem you gave. The first one, which is clearest: cpp Code:
And the second, which is a lot nastier, and only uses one variable less, but looks cooler because the for-loop is weird :-): cpp Code:
And finally one that doesn't use for-loops at all, but simple mathematics: cpp Code:
I love this stuff ![]() |
|
#5
|
||||
|
||||
|
TY Itsacon
I appreciate you taking the time to go through that in detail for me, and I was only saying the homework thing because there seemed to be a lot of homework questions floating around lol. Thx Again, this little session helped me tremendously.
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > FOR (LOOP) question!!! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|