# 2 dimensional array

can someone give me an example how I would load 10 integers from a user into a 2 dimensional array?
LVL 1
###### Who is Participating?

Commented:
Is this for a school assignment?  We cannot _give_ answers to school assigments.  But we can provide limited assistance.

You would declare an array of 2X5 array of 10 integers like

int IntArray[2][5];

I'm not sure if that is the right dimensions you want.  (although a 2D array of 10 integers can only be 1X10 or 2X5).

You can then read an integer from the user using something like

int I;

cout << "Please enter a number: ";
cint >> I;

In your case, you will probably need to use a loop to read a value for each integer in the array.

Does this get you started?
0

Author Commented:
this is from a book.  It's not very clear so I come here for clarification.

could you give me an example on the loop?
0

Author Commented:
so if I wanted a 12 integer array it would be int IntArray[2][6]
right?
0

Commented:
>> so if I wanted a 12 integer array it would be int IntArray[2][6]
That woudl be one way.  That would be a 2X6 array.  there are opther dimensions that would work.  Lik 3X4 and even 6X2.  There may be times when the order of the dimensions (3X4) verses 4X3) may be important too.

0

Commented:
int IntArray[2][5];

for (int x =0; x < 2; ++x)
{
for (int (y = 0; Y < 5; ++y)
{
cout << "Please enter number for position (" << x << "," << y << "): "
cin  >> IntArray[x,y];
}
}
0

Author Commented:
Adjusted points from 50 to 70
0

Author Commented:
I don't understand
0

Commented:
int IntArray[2][5];

for( int x =0; x < 2; ++x ){
for( int y = 0; y < 5; ++y ){
cout << "Please enter number for position (" << x << "," << y << "): ";
cin  >> IntArray[x][y];
}
}
0

Author Commented:
ozo that's the same thing neitod wrote.
0

Author Commented:
how do I get the program to ASK for 10 integers from a user?
0

Author Commented:
and then load it into an array?
0

Commented:
opps!   Thanks Ozo, I slipped into Pascal mode.

>> how do I get the program to ASK for 10 integers from a user?
The code I posted--with Ozo's correction.
0

Author Commented:
You should put a cin before your >> when you paste my message. Then i'll really think you're a guru.
0

Author Commented:
0

Author Commented:
what does this mean?
(int x =0; x < 2; ++x)
(int y = 0; y < 5; ++y)
0

Commented:
Its a for loop.  This is basic C/C++ stuff that you should fine exaplined in your text book.  There is probably an entire chapter on for () loops and while loops.

a for loop has 3 parts, inside the parenthesis,  a statement that is performed at the start, a statment that is tested to see if the loop should continue and a statement that is performed at the end of each loop.

so in

for (int x=0; x<2; ++x)

the compliler sets x to 0 before the loop.  Then it tests the execution condtion, x<2, if it is true (like when x is 0), it executes the loop (the statement following the for loop's parentheses).  Then after the loop it executes the it exectutes the final statemen ++x which increases the value of x by one.  Then the process repeats,  It tests the execution condition x<2, and if it is true it executes the loop....  This repeats until the execution condition is false, in this case when x becomes 2, then the loop ends and the program continues after the end of the for loop statement.

0

Author Commented:
I understand now.  You should write a book.

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.