Arrays

Started by
12 comments, last by CoolMiker 23 years, 10 months ago
int numbers[10];for(int i=0; i<10; ++i){   cin >> numbers;} 



the ''int numbers[10]'' bit creates an array of 10 integers.
you access an element in the array by putting the number of the element you want into square brackets ''[]''... eg numbers[3] will give you the 4th integer in the array (since the indexes start at 0)

be careful though - C++ does no bounds checking, so ''numbers[54] = 3'' will compile fine, but will probalby crash when you run it.


simple as that.
Advertisement
that last post was me but since i forgot to give my details, i cant edit it... so heres a brand new post

#include int numbers[10];for(int x=0; x<10; ++x){   cin >> numbers[x];}//small usage examplesnumbers[0] = 2;  //finenumbers[53] = 3; //will compile ok, but will crash at run timeif(numbers[3] == 3)   cout << "the 4th element stores the number 3" << endl;  


Edited by - POINT on June 16, 2000 5:22:18 PM

Edited by - POINT on June 16, 2000 5:23:24 PM
I just made that post up as I went along.....
I had no intention for someone to figure it out.....
Did you really go through the trouble of seeing what happens ?
I guess you could just put it inside of a main loop, compile it and see what happens.


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Its pretty simple if you really look at it. By the way, its an infinite loop.
"The Key To Creation Is Destruction"

This topic is closed to new replies.

Advertisement