KEMBAR78
Two Dimensional Arrays | PDF | Computer Programming | Computer Data
0% found this document useful (0 votes)
23 views10 pages

Two Dimensional Arrays

The document provides an overview of two-dimensional arrays, including their declaration, initialization, indexing, and memory allocation. It includes examples of C++ programs for various operations such as reading matrices, calculating sums of rows and columns, finding transposes, and representing sparse matrices in three-tuple form. Additionally, it explains how to read and manipulate two-dimensional arrays using nested loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views10 pages

Two Dimensional Arrays

The document provides an overview of two-dimensional arrays, including their declaration, initialization, indexing, and memory allocation. It includes examples of C++ programs for various operations such as reading matrices, calculating sums of rows and columns, finding transposes, and representing sparse matrices in three-tuple form. Additionally, it explains how to read and manipulate two-dimensional arrays using nested loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Two Dimensional arrays

A two dimensional array can be considered as a table consisting of rows and


columns.
1. Declaring a two dimensional array:-

The general form of declaring a two dimensional array is

Datatype arrayname [size1][size2];

Where datatype can be any of the basic data type or use defined data
type.
Arrayname refers to the name of the array.
Size1 indicates the number of rows in array and
size2 indicates no of columns in array

2. Examples of declaring two dimensional arrays


int mat[5][5];
char page[24][80];
float marks[50][7];

3. Initialising two dimensional array


int A[2][2] ={2,3,5,4};
char myset[3][3] ={‘e’,’r’,’s’,’a’,’q’,’t’,’f’,’l’,’c’};
float marks[2][7] ={1,30,35,38,34,38,30,2,20,25,28,22,27,29};
if extra values are provided then extra values are ignored , and if less
values are provided then remaining array elements are not assigned any
values.

4. Array indexing in two dimensional arrays.


Each element in a two dimensional array is identified by two indices
namely the row number and the column number. The row number starts
from 0 and column number also starts from 0.
Example :- Consider the following two dimensional array A[3][3] in the
form of a matrix. Then Row number and column number of each element
is as given in table.

Element Row No. Column No.


3 5 7 3 0 0
4 9 1 5 0 1
2 6 8 7 0 2
4 1 0
9 1 1
1 1 2
2 2 0
6 2 1
8 2 2
5. Memory allocation for two dimensional array
Continuous memory blocks are allotted for the two dimensional array. In
the above example int A[3][3] ; total 9X2=18 bytes of continuous blocks
of memory will be allotted to A.
6. Reading the elements in a two dimensional numerical arrays:-
Usually a two dimensional array can be read using two loops as given
below.
Consider the array declaration
int A[10][10], m,n; m and n indicates Rows and columns
respectively.
Then we can read elements in A as follows
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>A[i][j];
}
}
7. Displaying the elements of two dimensional array.
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<A[i][j]<< “ “; //each element is separated by
space
}
cout<<”\n”; // each row is displayed on new
line.
}
8. Programs on two dimensional arrays
i. Write a complete C++ program to read a matrix and find the sum
of each row of the matrix.

#include<iostream.h>
#include<conio.h>
main()
{
int A[10][10],i,j,m,n, sum=0;
clrscr();
cout<<"\n Enter Rows and column ";
cin>>m>>n;
cout<<"\n Enter matrix\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>A[i][j];
}
}
cout<<"\nSum of each row is";
for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
{
sum=sum+A[i][j];
}
cout<<"\nSum of Row "<<i+1<<" = "<<sum;
}
getch();
}
OUTPUT OF THE PROGRAM

Enter Rows and column 3 3


Enter matrix
222
123
432
Sum of each row is
Sum of Row 1 = 6
Sum of Row 2 = 6
Sum of Row 3 = 9

ii. Write a complete C++ program to read a matrix and find the sum
of each column of the matrix
#include<iostream.h>
#include<conio.h>
main()
{
int A[10][10],i,j,m,n, sum=0;
clrscr();
cout<<"\n Enter Rows and column ";
cin>>m>>n;
cout<<"\n Enter matrix\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>A[i][j];
}
}
cout<<"\nSum of each column is";
for(i=0;i<n;i++)
{
sum=0;
for(j=0;j<m;j++)
{
sum=sum+A[j][i];
}
cout<<"\nSum of column "<<i+1<<" = "<<sum;
}
getch();
}

Enter Rows and column 3 4


Enter matrix
1234
2222
4567
Sum of each column is
Sum of column 1 = 7
Sum of column 2 = 9
Sum of column 3 = 11
Sum of column 4 = 13

iii. Write a complete C++ program to read a matrix and find the sum
of each diagonal of the matrix
#include<iostream.h>
#include<conio.h>
main()
{
int A[10][10],i,j,m,n, sumd1=0,sumd2=0;
clrscr();
cout<<"\n Enter Rows and column ";
cin>>m>>n;
cout<<"\n Enter matrix\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>A[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
if(i==j)
sumd1=sumd1+A[i][j];
sumd2=sumd2+A[i][n-i-1];
}
cout<<"\n Sum of diagonal 1= "<<sumd1;
cout<<"\n Sum of diagonal 2= "<<sumd2;
getch();
}

Enter Rows and column 4 4

Enter matrix
1234
2222
5656
5555

Sum of diagonal 1= 13
Sum of diagonal 2= 17

iv. Write a complete C++ program to read a matrix and find the
transpose of the given matrix
#include<iostream.h>
#include<conio.h>
main()
{
int A[10][10],B[10][10],i,j,m,n, sumd1=0,sumd2=0;
clrscr();
cout<<"\n Enter Rows and column ";
cin>>m>>n;
cout<<"\n Enter matrix\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>A[i][j];
}
}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
B[j][i]=A[i][j];
}
}
cout<<"\n Transpose of the given matrix is\n";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<B[i][j]<<" ";
}
cout<<"\n";
}
getch();
}

Enter Rows and column 2 3

Enter matrix
123
456

Transpose of the given matrix is


1 4
2 5
3 6

v. Write a complete c++ program to read and add two matrices.


#include<iostream.h>
#include<conio.h>
main()
{
int A[10][10],B[10][10],C[10][10],i,j,m,n, ;
clrscr();
cout<<"\n Enter Rows and column ";
cin>>m>>n;
cout<<"\n Enter matrix1\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>A[i][j];
}
}
cout<<"\n Enter matrix2\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>B[i][j];
}
}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
C[i][j]=A[i][j]+B[i][j];
}
}
cout<<"\n Result of addition of the two matrices is\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<C[i][j]<<" ";
}
cout<<"\n";
}

getch();
}

Enter Rows and column 2 4

Enter matrix1
1234
2244

Enter matrix2
2233
5566

Result of addition of the two matrices is


3 4 6 7
7 7 10 10
vi. Write a complete c++ program to multiply two matrices.
#include<iostream.h>
#include<conio.h>
main()
{
int A[10][10],B[10][10],C[10][10],i,j,m,n,p;
clrscr();
cout<<"\n Enter Rows and columns of matrix1 ";
cin>>m>>n;
cout<<"\n Enter matrix1\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>A[i][j];
}
}
cout<<"\n Enter no of columns of matrix2 ";
cin>>p;
cout<<"\n Enter matrix2\n";
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
{
cin>>B[i][j];
}
}
for(i=0; i<m;i++)
{
for(int j=0; j<p;j++)
{
C[i][j]=0;
for(int k=0;k<n;k++)
{
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}
}
}
cout<<"\n Result of product of matrix multiplication is\n";
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
cout<<C[i][j]<<" ";
}
cout<<"\n";
}

getch();
}

Enter Rows and columns of matrix1 2 4

Enter matrix1
1234
2233

Enter no of columns of matrix2 3

Enter matrix2
123
222
333
111

Result of product of matrix multiplication is


18 19 20
18 20 22

vii. Write a complete c++ program to represent a sparse matrix in three


tuple form
#include<iostream.h>
#include<conio.h>
main()
{
int A[10][10],B[20][3],i,j,m,n,count=0;
clrscr();
cout<<"\n Enter Rows and columns ofsparse matrix ";
cin>>m>>n;
cout<<"\n Enter sparse matrix\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>A[i][j];
}
}
for(i=0; i<m;i++)
{
for(int j=0; j<n;j++)
{
if(A[i][j]!=0)
{
B[count+1][0]=i;
B[count+1][1]=j;
B[count+1][2]=A[i][j];
count++;
}
}
}
B[0][0]=m;
B[0][1]=n;
B[0][2]=count;
cout<<"\n The matrix in three tuple form is \n";
for(i=0;i<=count;i++)
{
for(j=0;j<3;j++)
{
cout<<B[i][j]<<" ";
}
cout<<"\n";
}

getch();
}

Enter Rows and columns ofsparse matrix 5 6

Enter sparse matrix


000001
200000
000000
050003
004000
The matrix in three tuple form is
5 6 5
0 5 1
1 0 2
3 1 5
3 5 3
4 2 4

You might also like