Multidimensional Arrays
This next section focuses on Multidimensional arrays. What is a multidimensional array?
Well it’s mainly just an array of arrays. The arrays we have been working with thus far have only been one-dimensional. A one-dimensional array of one-dimensional arrays is called a two-dimensional array; a one-dimensional array of two-dimensional arrays is called a three-dimensional array; etc. This may sound confusing, but hopefully it will make sense as we go along.
Here is how to declare a multidimensional array:
This creates a three-dimesonal array, with dimensions 30,20, and 2. The statement
would assign the value 304 to the element identified by the multi-index (30,20,2).
A good way to think of a two-dimensional array is like a tic-tac-toe game.
|
0 |
1 |
2 |
| 0 |
| null |
null |
null |
| null |
null |
null |
| null |
null |
null |
| |
| 1 |
| 2 |
Here’s an example, which includes nested for loops, and a two-dimensional array. Notice that the first dimension is not specified, while all the remaining dimensions are specified. This is because when a multi-dimensional array is passed to a function the first dimension does not have to be included. The compiler does not need to know how many groups of these 5-element arrays are to be stored, but it does need to know that they are 5-element arrays.
This first part is the header file:
 | |  | | |
//array.h
int i,j;
void read(int a[][4])
{
cout <<"Enter 12 integers, 4 per row: n";
for(i = 0; i < 3; i++)
{
cout <<"row " << i << ": ";
for(j = 0; j < 4; j++)
{
cin >> a[i][j];
}
}
}
void print(int a[][4])
{
for(i = 0; i < 3; i++)
{
for(j = 0; j < 4; j++)
cout << " " << a[i][j];
cout << endl;
}
} |
|  |
|
In this file, all we did was create 2 different functions, one was to prompt the user for numbers, and the other function was to print the numbers in the two-dimensional array.
Here is the cpp file.
 | |  | | |
#include <iostream.h>
#include "array.h"
//prototypes
void read(int a[][4]);
void print(const int a[][4]);
int main()
{
int a[3][4]; //creats 3 groups of 4 elemental arrays
read(a);
print(a);
return 0;
} |
|  |
|
In the source file, all we did is create a two-dimensional array, which has 3 groups of 4 elemental arrays, meaning each group has 4 elements in it. Here is an example:
int a[3][4]; //imagine that each block is an element.
 | |  | | |
[] [] [] []
[] [] [] []
[] [] [] [] |
|  |
|
Got it? Good!
Well that about raps it up for arrays, we’ll be getting into more advanced topics later with arrays.Please Rate this Code:
Comments for: Multidimensional Arrays
C [c] Posted: 1 times. says:
It would be nice if you showed how to initialize multidimentional arrays. |
 |
 |
C [c] Posted: 1 times. says:
It would be nice if you showed how to initialize multidimentional arrays. says:
i;m looking for a way to dynamically create a two dimensional array at run time. |
 |
 |
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
The way to dynamically create multidimentional arrays at runtime is to have a double pointer to the type and use new or malloc, like so:
 | |  | | |
// This will create a multi dimensional int array
int ** myArray = 0; // in C++ 0==NULL
int Rows;
int Cols;
cout << \"Number of Rows?\" << endl;
cin >> Rows;
cin.ignore (); // Just to get rid of newline
cout << \"Columns?\" << endl;
cin >> Cols;
cin.ignore ();
myArray = new *int[Rows];
if (myArray != NULL) {
for (int i = 0; i < Rows; i++) {
myArray[i] = new int[Cols];
}
}
// to delete those:
for (int i = 0; i < Rows; i++) {
delete [] myArray[i];
myArray[i] = 0;
}
delete [] myArray;
myArray = 0;
|
|  |
|
Thats all folks! |
 |
 |
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
The way to dynamically create multidimentional arrays at runtime is to have a double pointer to the type and use new or malloc, like so:
 | |  | | |
// This will create a multi dimensional int array
int ** myArray = 0; // in C++ 0==NULL
int Rows;
int Cols;
cout << \"Number of Rows?\" << endl;
cin >> Rows;
cin.ignore (); // Just to get rid of newline
cout << \"Columns?\" << endl;
cin >> Cols;
cin.ignore ();
myArray = new *int[Rows];
if (myArray != NULL) {
for (int i = 0; i < Rows; i++) {
myArray[i] = new int[Cols];
}
}
// to delete those:
for (int i = 0; i < Rows; i++) {
delete [] myArray[i];
myArray[i] = 0;
}
delete [] myArray;
myArray = 0;
|
|  |
|
Thats all folks! says:
Hellow ! y didn't u tell how to add , multiply two Matries ....? plz add this tooooo , waiting for it ! |
 |
 |
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
The way to dynamically create multidimentional arrays at runtime is to have a double pointer to the type and use new or malloc, like so:
 | |  | | |
// This will create a multi dimensional int array
int ** myArray = 0; // in C++ 0==NULL
int Rows;
int Cols;
cout << \"Number of Rows?\" << endl;
cin >> Rows;
cin.ignore (); // Just to get rid of newline
cout << \"Columns?\" << endl;
cin >> Cols;
cin.ignore ();
myArray = new *int[Rows];
if (myArray != NULL) {
for (int i = 0; i < Rows; i++) {
myArray[i] = new int[Cols];
}
}
// to delete those:
for (int i = 0; i < Rows; i++) {
delete [] myArray[i];
myArray[i] = 0;
}
delete [] myArray;
myArray = 0;
|
|  |
|
Thats all folks! says:
Hellow ! y didn't u tell how to add , multiply two Matries ....? plz add this tooooo , waiting for it ! says:
Can you show me how to read items into an array from an input file and then print that array. |
 |
 |
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
The way to dynamically create multidimentional arrays at runtime is to have a double pointer to the type and use new or malloc, like so:
 | |  | | |
// This will create a multi dimensional int array
int ** myArray = 0; // in C++ 0==NULL
int Rows;
int Cols;
cout << \"Number of Rows?\" << endl;
cin >> Rows;
cin.ignore (); // Just to get rid of newline
cout << \"Columns?\" << endl;
cin >> Cols;
cin.ignore ();
myArray = new *int[Rows];
if (myArray != NULL) {
for (int i = 0; i < Rows; i++) {
myArray[i] = new int[Cols];
}
}
// to delete those:
for (int i = 0; i < Rows; i++) {
delete [] myArray[i];
myArray[i] = 0;
}
delete [] myArray;
myArray = 0;
|
|  |
|
Thats all folks! says:
Hellow ! y didn't u tell how to add , multiply two Matries ....? plz add this tooooo , waiting for it ! says:
Can you show me how to read items into an array from an input file and then print that array. says:
Just a small correction to the previous example on how to create and assign multi-dim. arrays:
Instead of myArray = new *int[Rows]; it should be myArray = new int*[Rows];
|
 |
 |
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
The way to dynamically create multidimentional arrays at runtime is to have a double pointer to the type and use new or malloc, like so:
 | |  | | |
// This will create a multi dimensional int array
int ** myArray = 0; // in C++ 0==NULL
int Rows;
int Cols;
cout << \"Number of Rows?\" << endl;
cin >> Rows;
cin.ignore (); // Just to get rid of newline
cout << \"Columns?\" << endl;
cin >> Cols;
cin.ignore ();
myArray = new *int[Rows];
if (myArray != NULL) {
for (int i = 0; i < Rows; i++) {
myArray[i] = new int[Cols];
}
}
// to delete those:
for (int i = 0; i < Rows; i++) {
delete [] myArray[i];
myArray[i] = 0;
}
delete [] myArray;
myArray = 0;
|
|  |
|
Thats all folks! says:
Hellow ! y didn't u tell how to add , multiply two Matries ....? plz add this tooooo , waiting for it ! says:
Can you show me how to read items into an array from an input file and then print that array. says:
Just a small correction to the previous example on how to create and assign multi-dim. arrays:
Instead of myArray = new *int[Rows]; it should be myArray = new int*[Rows];
says:
no its not compile
its giving syntex error at
myArray = new *int[Rows];
|
 |
 |
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
The way to dynamically create multidimentional arrays at runtime is to have a double pointer to the type and use new or malloc, like so:
 | |  | | |
// This will create a multi dimensional int array
int ** myArray = 0; // in C++ 0==NULL
int Rows;
int Cols;
cout << \"Number of Rows?\" << endl;
cin >> Rows;
cin.ignore (); // Just to get rid of newline
cout << \"Columns?\" << endl;
cin >> Cols;
cin.ignore ();
myArray = new *int[Rows];
if (myArray != NULL) {
for (int i = 0; i < Rows; i++) {
myArray[i] = new int[Cols];
}
}
// to delete those:
for (int i = 0; i < Rows; i++) {
delete [] myArray[i];
myArray[i] = 0;
}
delete [] myArray;
myArray = 0;
|
|  |
|
Thats all folks! says:
Hellow ! y didn't u tell how to add , multiply two Matries ....? plz add this tooooo , waiting for it ! says:
Can you show me how to read items into an array from an input file and then print that array. says:
Just a small correction to the previous example on how to create and assign multi-dim. arrays:
Instead of myArray = new *int[Rows]; it should be myArray = new int*[Rows];
says:
no its not compile
its giving syntex error at
myArray = new *int[Rows];
says:
hi,i use visual c++ 6.0 but
when i free memory space i have a problems in the execution of my program
for (int i = 0; i < Rows; i++) {
delete [] myArray[i];
myArray[i] = 0;
}
delete [] myArray;
myArray = 0;
please show me the way |
 |
 |
ledaker [ledaker@yahoo.com] Posted: 2 times. says:
hi,i use visual c++ 6.0 but
when i free memory space i have a problems in the execution of my program
for (int i = 0; i < Rows; i++) {
delete [] myArray[i];
myArray[i] = 0;
}
delete [] myArray;
myArray = 0;
please show me the way |
 |
 |
|
 |
|
 |