0% found this document useful (0 votes)
17 views9 pages

Programs Related To 2D Arrays

Uploaded by

Niraj Nillawar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
17 views9 pages

Programs Related To 2D Arrays

Uploaded by

Niraj Nillawar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

2D Array

Basics of 2D array (Declaration and Initialization)


#include<stdio.h>
int main()
{
int a[2][2]={{2,3},{4,5}};
int a1[2][2]={2,3,4,5};
int a2[2][2]={{2},{1,5}};
int a3[2][2]={1,2};
int a4[2][2]={0};
int a5[][2]={1,2,3};
int i,j;
printf("\n Elements of first array are:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n Elements of second array are:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d ",a1[i][j]);
}
printf("\n");
}
printf("\n Elements of third array are:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d ",a2[i][j]);
}
printf("\n");
}
printf("\n Elements of fourth array are:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d ",a3[i][j]);
}
printf("\n");
}

Write a program to read and display elements of 2D array


#include<stdio.h>
int main()
{
int a[3][3], i, j;
for(i=0; i<3; i++)
{ // for loop for rows
for(j=0; j<3;j++)
{ // for loop for columns
printf("enter the value of a[%d][%d]:", i, j);
scanf("%d", &a[i][j]);
} //end for columns
} //end for rows
printf("elements of 2D matrix are");
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
printf("%d\t", a[i][j]);
} //end for
printf("\n");
} //end for
return 0;
}
Write a program to find the sum of all elements of 1D array
#include<stdio.h>
int main()
{
int a[3][3], i, j,sum=0;
for(i=0; i<3; i++)
{ // for loop for rows
for(j=0; j<3;j++)
{ // for loop for columns
printf("enter the value of a[%d][%d]:", i, j);
scanf("%d", &a[i][j]);
} //end for columns
} //end for rows
printf("elements of 2D matrix are");
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
sum=sum+a[i][j];
} //end for
printf("\n");
} //end for
printf("\n Sum is:%d",sum);
return 0;
}
Matrix Operations
WAP to find the sum of two matrices
//Write a program to add 2 matrices (3X3)
#include <stdio.h>
int main()
{
float a[3][3], b[3][3], c[3][3];
int i, j;
printf("Enter elements of 1st matrix\n");
for(i=0; i<3; i++)
{
for(j=0; j<3 ;j++)
{
printf("Enter a%d%d: ", i, j);
scanf("%f", &a[i][j]);
}
}
// Taking input using nested for loop
printf("Enter elements of 2nd matrix\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("Enter b%d%d: ", i, j);
scanf("%f", &b[i][j]);
}
}
// adding corresponding elements of two arrays
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
// Displaying the sum
printf("\nSum Of Matrix:\n");

for(i=0; i<3; i++)


{
for(j=0; j<3; j++)
{
printf("%.1f\t", c[i][j]);
}
printf("\n");
}
return 0;
}
WAP to display the transpose of a matrix
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);

// Storing elements of the matrix


printf("\nEnter elements of matrix:\n");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("Enter element a%d%d: ",i, j);
scanf("%d", &a[i][j]);
}
}
// Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("%d ", a[i][j]);
}
printf("\n\n");
}

// Finding the transpose of matrix a


for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
transpose[i][j] = a[j][i];
}
}
// Displaying the transpose of matrix a
printf("\nTranspose of Matrix:\n");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("%d ",transpose[i][j]);
}

printf("\n\n");
}
return 0;
}
WAP to find the sum of diagonal elements of a matrix
#include<stdio.h>
int main()
{
int a[10][10],sum=0;
int i,j,m,n;
printf("Enter number of rows and column:");
scanf("%d%d",&m,&n);
printf("Enter Elements : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
sum=sum+a[i][j];
}
}
}
printf("Sum of Diagonal Elements = %d ",sum);

}
WAP to perform multiplication of 2 matrices and display the result
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;

printf("Enter rows and column for first matrix: ");


scanf("%d %d", &r1, &c1);

printf("Enter rows and column for second matrix: ");


scanf("%d %d",&r2, &c2);

// Column of first matrix should be equal to column of second matrix and


while (c1 != r2)
{
printf("Error! No. of columns of first matrix not equal to no.of row of second.\n\n");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}

// Storing elements of first matrix.


printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
{
printf("Enter elements a%d%d: ",i,j);
scanf("%d", &a[i][j]);
}
}

// Storing elements of second matrix.


printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; i++)
{
for(j=0; j<c2; j++)
{
printf("Enter elements b%d%d: ",i, j);
scanf("%d",&b[i][j]);
}
}

// Initializing all elements of result matrix to 0


for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
result[i][j] = 0;
}
}
// Multiplying matrices a and b and
// storing result in result matrix

for(i=0; i<r1; i++)


{
for(j=0; j<c2; j++)
{
for(k=0; k<c1; k++)
{
result[i][j]+=a[i][k]*b[k][j];
}
}
}
// Displaying the result
printf("\nOutput Matrix:\n");
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
printf("%d ", result[i][j]);
}
printf("\n\n");
}
return 0;
}

Practice questions to do:


WAP to display the count of odd array elements in 2D array
WAP to find the sum of all prime array elements in 2D array
WAP to find the sum of those array elements which are multiple of 5 of 2D array
WAP to subtract, divide, modulo two matrices (-, /, %)

You might also like