0% found this document useful (0 votes)
20 views8 pages

Prasit Ass2

The document contains 4 C program examples: 1) storing elements in a 2D array in row major order and displaying it, 2) checking if a matrix is sparse and representing it in 3-tuple format if so, 3) finding the transpose of a sparse matrix, 4) multiplying two matrices.

Uploaded by

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

Prasit Ass2

The document contains 4 C program examples: 1) storing elements in a 2D array in row major order and displaying it, 2) checking if a matrix is sparse and representing it in 3-tuple format if so, 3) finding the transpose of a sparse matrix, 4) multiplying two matrices.

Uploaded by

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

1) Write a C program to store the elements in row major order in 2D array

and display it.

 #include <stdio.h>
void main()
{
int i,j,m,n;
printf("ENTER NUMBER OF COLUMNS AND ROWS:: ");
scanf("%d %d",&m,&n);
int a[m][n];
printf("ENTER ARRAY ELEMENTS:: ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}}
printf("ELEMENTS STORED IN ROW MAJOR ORDER ARE \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}}}
2) Write a C program to test a given matrix is sparse or not. If
sparse then represent it as 3 tuple format.

 #include <stdio.h>
void main()
{
int i,j,m,n,count=0;
printf("ENTER NUMBER OF COLUMNS AND ROWS:: ");
scanf("%d %d",&m,&n);
int a[m][n];
printf("ENTER ARRAY ELEMENTS:: ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==0)
{
count++;
}
}
}
int avg=(m*n)/2;
if(count>=avg)
{
int nn=(m*n)-count,e=0;
int tuple[3][nn];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]!=0)
{
tuple[0][e]=i;
tuple[1][e]=j;
tuple[2][e]=a[i][j];
e++;
}
}
}
printf("THE 3 TUPLE FORMAT OF THE SPARSE MATRIX IS ::\n");
for(i=0;i<3;i++)
{
for(j=0;j<nn;j++)
{
printf("%d ",tuple[i][j]);
}
printf("\n");
}
}
else
{
printf("NOT A SPARSE MATRIX.");
}
}
3) Write a C program to fine the transpose of a sparse matrix.

#include<stdio.h>
int main()
{
int i,j,m,n,a[10][10],non_zero_ele = 0;
float count = 0;
printf("Enter the size of the row :: ");
scanf("%d",&n);
printf("Enter the size of the collumn :: ");
scanf("%d",&m);
for(i=0;i<n;i++) {
for(j=0;j<m;j++)
{
printf("Enter the value of the array a[%d][%d] :: ",i,j);
scanf("%d",&a[i][j]); if(a[i][i]==0)
{
count+=1;
}
if(a[i][j]!=0)
{
non_zero_ele+=1;
}
}
}
printf("2-D array is :: \n");
for(i=0;i<n;i++) {
for(j=0;j<m;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
if(count>((n*m)/2))
printf("The matrix is sparse matrix \n");
else
printf("The matrix is not a sparse matrix ");
int newmatrix[10][10];
int k=0;
newmatrix[k][0]=i;
newmatrix[k][1]=j;
newmatrix[k][2]=non_zero_ele;
k++; for(i=0;i<n;i++) {
for(j=0;j<m;j++)
{
if(a[i][j]!=0)
{
newmatrix[k][0]=i+1;
newmatrix[k][1]=j+1;
newmatrix[k][2]=a[i][j]; k++; }
}
} int tm[10][10],temp; printf("After 3 tupple ne matrix is :: \n");
for(i=0;i<=non_zero_ele;i++)
{
for(j=0;j<3;j++)
{
printf("\t%d",newmatrix[i][j]);
tm[i][j]=newmatrix[i][j];
}
printf("\n");
}
for(i=0;i<=non_zero_ele;i++)
{
temp=tm[i][0]; tm[i][0]=tm[i][1]; tm[i][1]=temp; }
printf("\nTranspose matrix :\n");
for(i=0;i<=non_zero_ele;i++)
{ for(j=0;j<3;j++)
{
printf("\t%d",tm[i][j]);
} printf("\n");
}
return 0;
}

4) Write a C program to find the matrix multiplication of two


matrices.

 #include <stdio.h>
void main() {
int i,j,m1,m2,n1,n2,count=0;
printf("ENTER NUMBER OF ROWS AND COLUMNS OF FIRST 2D
ARRAY:: \n");
scanf("%d %d",&m1,&n1); int a[m1][n1];
printf("ENTER ARRAY ELEMENTS OF FIRST 2D ARRAY:: \n");
for(i=0;i<m1;i++)
{
for(j=0;j<n1;j++)
scanf("%d",&a[i][j]);
}
printf("ENTER NUMBER OF ROWS AND COLUMNS OF SECOND
2D ARRAY:: \n");
scanf("%d %d",&m2,&n2);
int b[m2][n2];
printf("ENTER ARRAY ELEMENTS OF SECOND 2D ARRAY:: \n");
for(i=0;i<m2;i++)
{
for(j=0;j<n2;j++)
{
scanf("%d",&b[i][j]);
}
}
if(n1!=m2)
{
printf("MATRIX MULTIPLICATION NOT POSSIBLE!");
}
else
{
printf("THE MULTIPLIED MATRIX IS :: \n");
int result[m1][n2]; for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
result[i][j]=0; for(int k=0;k<m2;k++) result[i][j] += a[i][k]*b[k][j];
}
}
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
printf("%d ",result[i][j]);
}
printf("\n");
}
}
}

You might also like