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: 5
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: 1,030 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: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4
Time spent in forums: 1 Week 9 h 13 m 59 sec
Reputation Power: 7
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: 564

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: 5
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: 1,030 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: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4
Time spent in forums: 1 Week 9 h 13 m 59 sec
Reputation Power: 7
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: 5
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




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 9 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek