I recently got the complete idiots guide to c++,but i feel as if the author does not explain things simply or in detail sometimes.I am having trouble with arrays,one command has been bugging me specifically.
I don't understand when getting an array to list its elements you have to use this command.
cout %26lt;%26lt; number[index];
index sets up the for loop.I know this,but could someone in simple details explain whats going on in this command?I'll probably be feeling stupid once you tell me.
Trying to Learn C++?
Number is a array, number[index] is an element of the array. Simple.
Reply:To start you can store data in variables. Such as:
int x = 5;
cout %26lt;%26lt; x;
You can store one value in x. If you wanted to store more than 1 value in a variable you use an array. Think of it as a list or some container that you can put more than one item into.
int x[3] = {2,4,6}
Now 'x' is storing 3 values two, four, and six. To access them individually you have to specify which element in the array you want.
x[0] would be two
x[1] would be four
x[2] would be six
Notice how the first index is 0 and not 1. It is like this in most computer programming languages. KEY POINT x[0] is the first element! This also means that 3 would be fourth element. If you tried to cout %26lt;%26lt; x[3]; you would get an error because x does not have four elements.
To print out those values you use the variable index to access them:
int index = 1;
cout %26lt;%26lt; x[index];
in this case it would print out ... you guessed it! '4'
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment