0% found this document useful (0 votes)
3 views4 pages

6 Two

The document provides an overview of two-dimensional arrays in programming, illustrating their structure with examples. It includes code snippets for displaying elements, reading a matrix, and performing addition and multiplication of 3x3 matrices. The examples are written in C and demonstrate basic operations with two-dimensional arrays.

Uploaded by

Abadullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

6 Two

The document provides an overview of two-dimensional arrays in programming, illustrating their structure with examples. It includes code snippets for displaying elements, reading a matrix, and performing addition and multiplication of 3x3 matrices. The examples are written in C and demonstrate basic operations with two-dimensional arrays.

Uploaded by

Abadullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Two-dimensional Array

Two-dimensional array can be thought as a rectangular display of elements with rows and
columns.
For example elements of int x[3][3];

Array elements in matrix form

Col1 col2 col3

Row1 x[0][0] x[0][1] x[0][2]


Row2 x[1][0] x[1][1] x[1][2]
Row3 x[2][0] x[2][1] x[2][2]

Array Initialization

int a[3][3]={{1,2,3}, {2,3,4},{1,2,5}};

int a[][]={{1,1,2},{3,4,4},{5,4,3}};

1. WAP to display the elements of two-dimensional array.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
clrscr();
printf(“\n Elements of an array\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
getch();
}

2. Read the matrix of the order up to 10x10 elements and display the same in the
matrix form.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,row,col,a[10][10];
clrscr();
printf(“\n Enter order of matrix up to (10x10) :”);
scanf(“%d%d”,&row,&col);
printf(“\n Enter elements :”);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“\n The matrix is :\n”);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“%d\t”,a[i][j]);
}
}
getch();
}

3. WAP to calculate addition of two 3*3 matrix

#include<stdio.h>
#include<conio.h>
void main()
{
int mat1[3][3],mat2[3][3],add[3][3],i,j;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
print(“\nenter the number :”);
scanf(“%d”,&mat1[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
print(“\nenter the number :”);
scanf(“%d”,&mat2[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
add[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf(“\naddition of two matrix :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,add[i][j]);
}
}

Multiplication of two matrix


#include<stdio.h>
#include<conio.h>
void main()
{
int mat1[3][3],mat2[3][3],mul[3][3],i,j,k;
clrscr();
printf(“\nEnter the value of first matrix :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
print(“\nenter the number :”);
scanf(“%d”,&mat1[i][j]);
}
}
Printf(“\nEnter the value of second matrix :”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
print(“\nenter the number :”);
scanf(“%d”,&mat2[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mul[i][j]=0;
for(k=0;k<3;k++)
{
mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j];
}
}
}
Printf(“\n Multiplication of two matrix \n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
print(“%d\t”,mul[i][j]);
}
getch();
}

You might also like