This array is used to represent and store data in a tabular
form. Such type of array specially used to represent data in a matrix form. It is an array having 2 subscript or indices.
Syntax
storage
class datatype arrayname[s1][s2];
Where ‘s1’ indicates number of rows and ‘s2’ indicates
number of column.
In C++ the subscript of the first array element is always zero. For instance, If a[5][5] is an array then the first element is always (by default) a[0][0].
In C++ the subscript of the first array element is always zero. For instance, If a[5][5] is an array then the first element is always (by default) a[0][0].
Example
int
a[4][3];
int
x[m][n];
Where ‘m’ and ‘n’ are constant numbers.
Initialisation:- Like
one dimensional array two dimensional array can be initialised with a list of
values while declaring the array variable.
Example
int a[3][2]={1,2,4,6,7,9};
where
a[0][0]=1 a[0][1]=2
a[1][0]=4 a[1][1]=6
a[2][0]=7 a[2][1]=9
alternate,
int
a[3][2]={{1,2},{4,6},{7,9}};
The row size is optional when array
is initialised with a set of values.
Lets have an example.
Q. Write a program to read a matrix having m X n elements
and display the sum of the main diagonal elements.
#include <iostream>
#include <stdlib.h>
void main()
{
int a[10][10], m, n;
cout<< “Enter row and column size ”;
cin>>m>>n;
if(m!=n)
{
cout<< “Diagonal elements don’t exist”;
exit(1);
}
cout<< “Enter”<< m << “X”
<< n << “elements”;
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
cin>>a[i][j];
int s=0;
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
if( i==j )
s+=a[i][j];
cout<< “Sum is ->”<<s;
}
Q. Write a program to determine product of diagonal element of m X n matrix.
Q. Write a program to determine sum and product of 2 matrices.
Q. Write a program to determine product of diagonal element of m X n matrix.
Q. Write a program to determine sum and product of 2 matrices.
No comments:
Post a Comment