?
Solved

2 dimensional array

Posted on 2000-04-24
17
Medium Priority
?
211 Views
Last Modified: 2010-04-02
can someone give me an example how I would load 10 integers from a user into a 2 dimensional array?
0
Comment
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
  • Learn & ask questions
  • 11
  • 5
17 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 280 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

by:nationnon
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

by:nationnon
ID: 2743569
so if I wanted a 12 integer array it would be int IntArray[2][6]
right?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
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

by:nietod
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

by:nationnon
ID: 2750557
Adjusted points from 50 to 70
0
 
LVL 1

Author Comment

by:nationnon
ID: 2750558
I don't understand
0
 
LVL 84

Expert Comment

by:ozo
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

by:nationnon
ID: 2750694
ozo that's the same thing neitod wrote.
0
 
LVL 1

Author Comment

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

Author Comment

by:nationnon
ID: 2750729
and then load it into an array?
0
 
LVL 22

Expert Comment

by:nietod
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

by:nationnon
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

by:nationnon
ID: 2750862
then with your answer put cout << "blah blah blah"
0
 
LVL 1

Author Comment

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

Expert Comment

by:nietod
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

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

Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question