*Really* basic Arrays question
Right now, I'm at the point in my C++ book where arrays are being taught. The following code gives the output followed underneath.
#include 'iostream.h'
int main()
{
int myArray[5];
int i;
for (i=0; i<5; i++) // o - 4
{
cout << "Value for myArray[" << i << "]: ";
cin >> myArray ;
}
for (i=0; i<5; i++)
cout << i << ": " << myArray << "\n";
return 0;
}
OutPut: Value for myArray[0]: n
Value for myArray[1]: n
Value for myArray[2]: n
Value for myArray[3]: n
Value for myArray[4]: n
0: n
1: n
2: n
3: n
4: n
This is cool, but what I don't understand is the for statement where if(i=0; i<5; i++). I equals zero, if it is bigger than five, increment it by one. This is true in every single case- it's proven in the output. However, How can myArray[0] exist if it is incremented by one? 0 is smaller than one, thus, it should've been incremented. This is a world of confusion for me, and the author is not detailed enough for my small brain! Hehe, so can anyone please help me out here? I'm really anxious to get started on more complicated arrays.
NOTE: For some reason, on line 11, where cin >> myrray, it won't display . Thanks
Edited by - Fredric on 2/25/00 7:39:59 PM
Edited by - Fredric on 2/25/00 7:40:32 PM
Edited by - Fredric on 2/25/00 7:41:39 PM </i>
3D Math- The type of mathematics that'll put hair on your chest!
Um... I think you''re misinterpreting the "for" loop.
First of all, the "for" statement isn''t an "if" statement. Where it says "(i=0; i<5; i++)" it means "upon first entering the loop, set i equal to zero. Then, do the loop. At the end of the loop, if i is less than 5, increment i by 1 (++) and loop again. If i is greater or equal to 5 (ie the statement i<5 is false) then stop looping." In essence it is just looping from 0 to 4, incrementing the variable i at the end of each loop.
Oh, also, on the line where you have
cout << i << ": " << myArray << "\n";
it should be
cout << i << ": " << myArray << "\n";
as otherwise it doesn''t access the value in myArray that you want. Same difference on the "cin" line.
Hope that helps.
First of all, the "for" statement isn''t an "if" statement. Where it says "(i=0; i<5; i++)" it means "upon first entering the loop, set i equal to zero. Then, do the loop. At the end of the loop, if i is less than 5, increment i by 1 (++) and loop again. If i is greater or equal to 5 (ie the statement i<5 is false) then stop looping." In essence it is just looping from 0 to 4, incrementing the variable i at the end of each loop.
Oh, also, on the line where you have
cout << i << ": " << myArray << "\n";
it should be
cout << i << ": " << myArray << "\n";
as otherwise it doesn''t access the value in myArray that you want. Same difference on the "cin" line.
Hope that helps.
I think you have several misunderstandings here. First, < is a lessthan sign, not a greater than sign. The for statement is equivelant to this:
int i = 0;
while (i < 5) //This is a LESS THAN sign
{
//Get input/output
i++;
}
I hope that helps you some.
int i = 0;
while (i < 5) //This is a LESS THAN sign
{
//Get input/output
i++;
}
I hope that helps you some.
Dragonskin- Thanks for the for loop info. I misunderstood how it worked. I''ve got it all clear now, and I thank you for it! 
Cloxs- sorry, when I said bigger, I meant smaller. I was rushing to type it, but it was a typing error. Sorry for causing even more confusion!

Cloxs- sorry, when I said bigger, I meant smaller. I was rushing to type it, but it was a typing error. Sorry for causing even more confusion!

3D Math- The type of mathematics that'll put hair on your chest!
Okay, I have another question about arrays. It''s on the same piece of code, so refer to my first message 
When the user is prompted to enter a value, the value that they entered is stored somewhere (1.- Where?). But, when the program prints all of the values that the user has given, I don''t understand how it does that. Right before it does, it has a for loop. The for loop sets the value of i to 0. So, how can the myArray know what the user has put in?
Thanks again- this syntax is just killing me!

When the user is prompted to enter a value, the value that they entered is stored somewhere (1.- Where?). But, when the program prints all of the values that the user has given, I don''t understand how it does that. Right before it does, it has a for loop. The for loop sets the value of i to 0. So, how can the myArray know what the user has put in?
Thanks again- this syntax is just killing me!
3D Math- The type of mathematics that'll put hair on your chest!
The "myArray" variable holds in fact 5 integers. "myArray" represents an adress in memory (a pointer, you'll surely learn more about that later). In memory at the "myArray" adress, there are 5 integers in a row. When you write "myArray[0]", it refers to the first. "myArray[1]" refers to the 2nd, and so on. So :
"cin >> myArray[y]" //not y but i, otherwise it doesn't display properly on this page
asks the user to enter 5 values, in each of the memory slots (remember "i" will take all values from 0 to 4 during the loop). Same explanation for cout, but there it displays the 5 values.
Edited by - Crousto on 2/26/00 8:44:13 AM
"cin >> myArray[y]" //not y but i, otherwise it doesn't display properly on this page

asks the user to enter 5 values, in each of the memory slots (remember "i" will take all values from 0 to 4 during the loop). Same explanation for cout, but there it displays the 5 values.
Edited by - Crousto on 2/26/00 8:44:13 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement