• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

2 dimensional array

can someone give me an example how I would load 10 integers from a user into a 2 dimensional array?
0
nationnon
Asked:
nationnon
  • 11
  • 5
1 Solution
 
nietodCommented:
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
 
nationnonAuthor 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
 
nationnonAuthor Commented:
so if I wanted a 12 integer array it would be int IntArray[2][6]
right?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
nietodCommented:
>> 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
 
nietodCommented:
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
 
nationnonAuthor Commented:
Adjusted points from 50 to 70
0
 
nationnonAuthor Commented:
I don't understand
0
 
ozoCommented:
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
 
nationnonAuthor Commented:
ozo that's the same thing neitod wrote.
0
 
nationnonAuthor Commented:
how do I get the program to ASK for 10 integers from a user?
0
 
nationnonAuthor Commented:
and then load it into an array?
0
 
nietodCommented:
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
 
nationnonAuthor Commented:
You should put a cin before your >> when you paste my message. Then i'll really think you're a guru.
0
 
nationnonAuthor Commented:
then with your answer put cout << "blah blah blah"
0
 
nationnonAuthor Commented:
what does this mean?
(int x =0; x < 2; ++x)
(int y = 0; y < 5; ++y)
0
 
nietodCommented:
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
 
nationnonAuthor Commented:
I understand now.  You should write a book.

Thanks
0

Featured Post

Industry Leaders: 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!

  • 11
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now