C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 28th, 2005, 01:05 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
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!!

Reply With Quote
  #2  
Old September 28th, 2005, 02:58 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 996 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 5 m 52 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
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:
Original - cpp Code
  1. int main(void)
  2. {
  3.     float testScore[6] = {0};
  4.     cout<<"Test Scores For The Semester\n";
  5.     for(int i = 0; i <= 5; i++)
  6.     {
  7.         cout << "Score " << i << ":";
  8.         cin >> testScore[i];
  9.     }
  10.     return 0;
  11. }

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:
Original - cpp Code
  1. int main(void)
  2. {
  3.     float sum = 0, testScore[6] = {0};
  4.     cout<<"Test Scores For The Semester\n";
  5.     for(int i = 0; i <= 5; i++)
  6.     {
  7.         cout << "Score " << i << ":";
  8.         cin >> testScore[i];
  9.         sum += testscore[i];
  10.     }
  11.     cout << "Average: " << (sum / 6.0) << "\n";
  12.     return 0;
  13. }


Hope this helps.
Comments on this post
MichaelSoft agrees: I'm sure he can grasp it now
__________________
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

Reply With Quote
  #3  
Old September 28th, 2005, 11:48 AM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
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:
Write a program that requests the user to enter two integers. The program should then calculate and report the sum of all the integers between and including the two integers. At this point, assume that the smaller integer is entered first. For Example, if the user entered 2 and 9, the program should report that the sum of all integers from 2 through 9 is 44.


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

Reply With Quote
  #4  
Old September 28th, 2005, 01:34 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 996 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2Folding Points: 808001 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 5 m 52 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
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:
Original - cpp Code
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5.     // declarations
  6.     int one, two, i, sum = 0;
  7.  
  8.     // read input
  9.     printf("Give first integer: ");
  10.     scanf("%d", &one);
  11.     printf("Give second integer: ");
  12.     scanf("%d", &two);
  13.  
  14.     // calculate sum
  15.     for(i = one; i <= two; i++)
  16.         sum = sum + i;
  17.  
  18.     // report sum
  19.     printf("the sum of all integers from %d to %d is %d\n", one, two, sum);
  20.  
  21.     // exit
  22.     return 0;
  23. }


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:
Original - cpp Code
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5.     // declarations
  6.     int one, two, sum;
  7.  
  8.     // read input
  9.     printf("Give first integer: ");
  10.     scanf("%d", &one);
  11.     printf("Give second integer: ");
  12.     scanf("%d", &two);
  13.  
  14.     // start output
  15.     printf("the sum of all integers from %d to %d is ", one, two);
  16.  
  17.     // calculate sum
  18.     for(sum = 0; one <= two; sum += one++);
  19.  
  20.     // finish output
  21.     printf("%d\n", sum);
  22.  
  23.     // exit
  24.     return 0;
  25. }


And finally one that doesn't use for-loops at all, but simple mathematics:
cpp Code:
Original - cpp Code
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5.     // declarations
  6.     int one, two;
  7.  
  8.     // read input
  9.     printf("Give first integer: ");
  10.     scanf("%d", &one);
  11.     printf("Give second integer: ");
  12.     scanf("%d", &two);
  13.  
  14.     // print output
  15.     printf("the sum of all integers from %d to %d is %d\n", one, two, (((1 + two - one) * (one + two)) / 2));
  16.  
  17.     // exit
  18.     return 0;
  19. }


I love this stuff

Reply With Quote
  #5  
Old September 28th, 2005, 05:01 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > FOR (LOOP) question!!!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |