Solved

# 2 dimensional array

Posted on 2000-04-24
206 Views
can someone give me an example how I would load 10 integers from a user into a 2 dimensional array?
0
Question by:nationnon
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 11
• 5

LVL 22

Accepted Solution

nietod earned 70 total points
ID: 2743457
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

LVL 1

Author Comment

ID: 2743557
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

LVL 1

Author Comment

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

LVL 22

Expert Comment

ID: 2743652
>> 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

LVL 22

Expert Comment

ID: 2743661
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

LVL 1

Author Comment

ID: 2750557
Adjusted points from 50 to 70
0

LVL 1

Author Comment

ID: 2750558
I don't understand
0

LVL 84

Expert Comment

ID: 2750675
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

LVL 1

Author Comment

ID: 2750694
ozo that's the same thing neitod wrote.
0

LVL 1

Author Comment

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

LVL 1

Author Comment

ID: 2750729
and then load it into an array?
0

LVL 22

Expert Comment

ID: 2750810
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

LVL 1

Author Comment

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

LVL 1

Author Comment

ID: 2750862
0

LVL 1

Author Comment

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

LVL 22

Expert Comment

ID: 2751013
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

LVL 1

Author Comment

ID: 2751250
I understand now.  You should write a book.

Thanks
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

returning a dereferenced pts in C++ 10 175
I could not build boost code, 10 116
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 312
parameter pack in c++11 2 22
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, whâ€¦
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the bâ€¦
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Botâ€¦
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month6 days, 2 hours left to enroll