RSS Forums RSS
Please support our C++ advertiser: Programming Forums

Please help me with arrays

Join Date: Sep 2004
Posts: 6,576
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 499
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Please help me with arrays

  #2  
Jul 26th, 2005
An array is just about the simplest and most useful thing you can think of. Imagine a line of boxes numbered 0 to N, where N is some arbitrary number. To put something in the fifth box, you walk over to the one with the number 4 (because they start at 0), and drop the thing in it. Then you walk back to the beginning of the row. To get the thing back, you walk back to the box with 4 and grab the thing.

Let's say that thing is a number, 123. The C++ code to create a line of ten boxes numbered 0 to 9 is:
int box[10];
You can then put the thing in the fifth box like this:
box[4] = 123;
Then to get the thing back, you do the same thing, except with a spare box on the left, which we'll call thing:
int thing = box[4];
If you want every thing in the line of boxes, you go from one end to the other and grab the things in each box:
for ( int i = 0; i < 10; i++ )
  std::cout<< box[i] <<'\n';
Or you could go from the other end:
for ( int i = 9; i >= 0; i-- )
  std::cout<< box[i] <<'\n';
Just about any use of an array is some variation of the above.
I'm here to prove you wrong.
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:44 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC