MULTIDIMENSIONAL
ARRAYS
Multidimensional arrays are defined in the same manner as
one dimensional arrays, except that a separate pair of square brackets are
required for each subscript. Thus, a two dimensional array will require two
pairs of square brackets; a three dimensional array will require three pairs of
square brackets and so on.
The general format of the multidimensional array is,
storage_class data_type
arrayname [expression_1][expression_2]...
[expression_n];
where the storage_class refers to the scope of the
array variable such as external static, automatic or register; the data_type refers to the nature of the
data elements in the array such as character type, integer point or floating
point etc; and the array name is the name of the multidimensional array. Expression_1, expression_2... expression_n refers
to the maximum size of the each array locations.
For example,
float coordinate x[10] [10];
int value [50] [10] [5];
char line [10] [80];
static double records [100] [100]
[10];
In the above
illustration, the first line defines the coordinate as a floating point array
having 10 rows, 10 columns with a total of 100 elements. The second one is a
three dimensional integer array whose maximum size is 2500 elements.
Multidimensional array initialization Similar to one
dimensional array, multidimensional arrays can also be initialized, if one
intend assign some values to these elements. It should be noted that only
external or static arrays can be initialized.
For example,
consider the following two dimensional array declaration:
(1)
int x[2][2] = {1,2,3};
where x is a two dimensional array of
integer numbers whosw maximum size is 4 and the assignments would be
x[0] [0] = 1;
x[0] [1] = 2;
x[1] [0] = 3;
x[1] [1] = 4;
(2)
static
float sum [3] [4 = { 0.1,0.2,0.3,0.4,0.6,0.7,0.8,0.9,1,1,1,1 };
where sum is a static two dimensional array of
floating point numbers and the maximum size of the array is 3 rows and 4 columns having a total of 12
elements.
The assignments
would be
sum [0] [0] =0.1 sum [0] [1] =0.2 sum [0] [2] =0.3 sum [0] [3] =0.4
sum [1] [0] =0.6 sum [1] [1] =0.7 sum [1] [2] =0.8 sum [1] [3] =0.9
sum [2] [0] =1 sum [2] [1] =1 sum [2] [2] =1 sum [1] [3] =1
The natural order in
which the initial values are assigned can be altered by forcing groups of
initial values enclosed within braces, i.e., {...}.
For example, in the
following a two dimensional array can be declared.
int A[3] [3]
= {
{ 1,2,3 },
{ 4,5,6 },
{ 7,8,9 }
};
In the above array
declaration, the three values in the first inner pair of braces are assigned to
the array element in the first row; the values in the second pair of braces are
assigned to the array element in the second row and so on.
The elements in
the above array A will be assigned as
A[0] [0] =
1 A[0] [1] = 2 A[0] [2] = 3
A[1] [0] = 4 A[1] [1] = 5 A[1] [2] = 6
A[2] [0] = 6 A[2] [1] = 8 A[2] [2] = 9
Now consider
the following two dimensional array definition,
int matrixa [3] [3] = {
{ 1,2 },
{ 4,5 },
{ 7,8 }
};
This definition assigns values only
to the first two elements in each row and hance, the array elements will have
the following initial values,
matrixa[0]
[0] =1 matrixa[0] [1] =2 matrixa[0] [2] =0
matrixa[1]
[0] =4 matrixa[1] [1] =5 matrixa[1] [2] =0
matrixa[2]
[0] =7 matrixa[2] [1] =8 matrixa[2] [2] =0
Note that the last element in each
row is assigned a value zero.
If we declared the
two dimensional array as
int matrix [3] [3] = { 1,2,3,4,5,6,7 }
then following assignment will be
carried out:
matrixa[0]
[0] =1 matrixa[0] [1] =2 matrixa[0] [2] =3
matrixa[1]
[0] =4 matrixa[1] [1] =5 matrixa[1] [2] =6
matrixa[2]
[0] =7 matrixa[2] [1] =0 matrixa[2] [2] =0
Two of the array
elements will be again assigned zero, though the order of the assignment will
be different.
If one declares the
array definition as
int a [3] [3] = {
{ 1,2,3,4 },
{ 5,6,7,8 },
{ 9,10,11,12 }
};
then, the maximum array allocation for the above two
dimensional declaration will be 3x3=9 elements. But the initial definition of
the array elements have exceeded the declared size and hence, normally, the
compiler will produce the error massage. To avoid the above problem, the array
may be declared and written as,
int a [3] [3] = {
{ 1,2,3,4 },
{ 5,6,7,8 },
{ 9,10,11,12 }
};
PROGRAM
A program to initialized a set of numbers in a two
dimensional array and to display the content of the array on the careen.
//example
5.7
#define N 3
#define M 4
#include <iostream.h>
void main (void)
{
int I,j;
float a[N] M] = {
{ 1,2,3,4
},
{ 5,6,7,8 },
{
9,10,11,12 }
};
cout << “Contents of the
array “ << endl;
for (i = 0; i <=N-1; ++i) {
for (j = 0; j <= M-1; ++j)
cout << a[i] [j]
<< ‘\t’;
cout << endl;
}
}
Output of the above
program
Contents of the array
1
2 3 4
5
6 7 8
9
10 11 12
PROGRAM
A program initialize only a few elements of a two
dimensional array and to display the content of the array on the screen.
//example 5.8
#define N 3
#define M 4
#include <iostream.h>
void main (void)
{
int i, j;
float a [N] [M] = {
{ 1,2,3 },
{ 5,6,7 },
{ 9,10,11 }
};
Cout << “ Contents of the array
“ << endl;
for (i = 0; i <= N-1; ++i) {
for (j = 0; j <= M-1; ++j)
cout << a[i] [j]
<< ‘\t’;
cout << endl;
}
}
Output of the above
program
Contents of the array
1
2 3 0
5
6 7 0
9
10 11 0
PROGRAM
A program to read the elements of the given two matrices of
order n x n and to perform the matrix
multiplication.
// example
5.9
#define MAX 100
#include <iostream.h>
void main (void)
{
void output (float a [MAX] [MAX], int
n);
// function declaration
void mul (float a[MAX] [MAX], float b
[MAX] [MAX], int n);
float a[MAX] [MAX], b[MAX] [MAX;
int i,j,n;
cout << “order of A matrix “
<<endl;
cin >> n;
cout << “Enter the elements of A
Matrix “ << endl;
for (i= 0; i <= n-1; ++i) {
for (j =0; j <= n-1; ++i)
cin >> a[i] [j];
}
cout << “ order of B matrix “
<< endl;
cin >> n;
cout << “Enter the elements of B
Matrix “ << endl;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
cin >> b[i] [i];
}
cout << “output A[i] [j] “
<< endl;
output (a,n);
cout << endl;
cout << “output B[i] [j] “
<< endl;
output (b,n);
mul (a,b,c) ;
}
void mul (float a [MAX] [MAX], float b[MAX]
[MAX], int n)
{
void mul (float c[MAX] [MAX], int n);
// function declaration
float c[MAX] [MAX];
int i,j,k;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j) {
c[i] [j] = 0.0;
for (k =0; k <= n-1; ++k)
c[i] [j] = c[i] [j] +
a[i] [k] * b[k] [j];
}
}
cout << endl;
cout << “output of c[i] [j]
matrix” << endl;
output (c,n) ;
}
void output (float x[MAX] [MAX], int n)
{
int i,j;
for (i = 0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j)
cout << x[i] [j] <<
‘\t’;
cout << endl;
}
}
Output of the above
matrix
Order of A matrix
3
Enter the elements of A Matrix
3
3 3
3
3 3
3
3 3
Order of B matrix
3
Enter the elements of B Matrix
3
3 3
3
3 3
3
3 3
Output A[i] [j]
3
3 3
3
3 3
3
3 3
Output of c[i] [j] matrix
27
27 27
27
27 27
27
27 27
The following function performs the
matrix subtraction of the two given matrices of order n*n.
//function on matrix subtraction
void sub (float a[MAX] [MAX], float b[MAX]
[MAX], int n)
{
void output (float c[MAX][MAX]; int
n);
// function declaration
float c [MAX [MAX];
int i,j,k;
for (i =0; i <= n-1; ++i) {
for (j = 0; j <= n-1; ++j) {
c[i] [j] = a[i] [j] - b[i]
[j];
}
}
cout << endl;
cout << “output of c[i] [j] matrix”
<< endl;
output (c,n);
}
cplusplus code examples for novices
ReplyDeletec++ sample - Create a file comparision utility