Solved

2 dimensional array

Posted on 2000-04-24
17
201 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
  • 11
  • 5
17 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 70 total points
Comment Utility
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
Comment Utility
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
Comment Utility
so if I wanted a 12 integer array it would be int IntArray[2][6]
right?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> 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
Comment Utility
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
Comment Utility
Adjusted points from 50 to 70
0
 
LVL 1

Author Comment

by:nationnon
Comment Utility
I don't understand
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

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

Author Comment

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

Author Comment

by:nationnon
Comment Utility
and then load it into an array?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
then with your answer put cout << "blah blah blah"
0
 
LVL 1

Author Comment

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

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
I understand now.  You should write a book.

Thanks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now