# Thread: Newbbie lookin' for help on two-dimensional arrays

1. ## help on two-dimensional arrays

im learning C++ by myself. (my brother and I)
yesterday a friend of us told us that we can make a multiple array..
and he gave us an example with a times table ( 2x2=4)
we tried to use it but it didn't work
we used the
int num[ ] [ ];

of couse if we hard code it will work but we cannot do it, by prompting the user..
can you help us solving it?

Thank you for all your feed back..
------------------------------------------------------------------------------------
but how can i make it interactive.. propting the user..
like..
cin>>num1;
cin>>num2;
and then display
cout<<num1<<*<<num2<<"="result<<endl; //displaying both numbers and then the result
==============================================

2. You need to supply the dimensions within brackets.

Code:
#include <iostream>

const int NUM_ROWS = 5; // dimensions need to be constants at compile time
const int NUM_COLS = 5;

int main() {
int timesTable[NUM_ROWS][NUM_COLS];  // create the matrix with sizes

for (int i = 0; i < NUM_ROWS; i++) {
for (int j = 0; j < NUM_COLS; j++) {
timesTable[i][j] = (i + 1) * (j + 1);  // fill the table
}
}

for (int r = 0; r < NUM_ROWS; r++) {
for (int c = 0; c <NUM_COLS; c++) {
std::cout << timesTable[r][c] << "\t"; // display the table
}
std::cout << std::endl;
}

return 0;
}
The catch is that the size of the matrix needs to be known at compile time, or else you need to use dynamic memory... but that stuff is a little rough for beginners.

3. Aside from the excellent help on the code, SilentStrike, may I add that MSVC++ 6 wouldn't compile the code as presented. Borland C++Builder5 did.

Borland:
Code:
As presented by SilentStrike
MSVC++:
Code:
#include <iostream.h> // Add the .h

const int NUM_ROWS = 5; // dimensions need to be constants at compile time
const int NUM_COLS = 5;

int main() {
int timesTable[NUM_ROWS][NUM_COLS];  // create the matrix with sizes

for (int i = 0; i < NUM_ROWS; i++) {
for (int j = 0; j < NUM_COLS; j++) {
timesTable[i][j] = (i + 1) * (j + 1);  // fill the table
}
}

for (int r = 0; r < NUM_ROWS; r++) {
for (int c = 0; c <NUM_COLS; c++) {
//std::cout << timesTable[r][c] << "\t"; // display the table
cout << timesTable[r][c] << "\t"; // display the table
}
//std::cout << std::endl;
cout << endl;

}
return 0;
}
Lots of opinions and they're all like...hmmmm.

Thanks, again.

4. I didn't test it with MSVC, but I am sure it supports the std namespace. iostream.h isn't standard C++, anymore.

http://www.cplusplus.com/doc/tutorial/tut5-2.html

One of the best examples that we can find about namespaces is the standard C++ library itself. According to ANSI C++ standard, the definition all the classes, objects and functions of the standard C++ library are defined within namespace std.

5. Quite right, and the error was mine.

In transferring the code from Borland, which requires #include <conio> for 'getch();', to MSVC, my fat fingers stopped paying attention to my brain.

Or, was it the other way around...?