Sunday, 7 April 2013

Matrix manipulation in c program with output

MATRIX MANIPULATION

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],sum,sum1,i,j,n,c=0,m=0,s=0,y=0,r,x,b[10],d[10];
clrscr();
printf("\nEnter the size of matrix:");
scanf("%d",&n);
printf("\nEnter the values of matirx");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\nThe matrix is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
while(c!=5)
{
printf("\n1.The trace of matrix");
printf("\n2.Column sum and row sum");
printf("\n3.print upper and lower triangular matrix");
printf("\n4.strict upper and lower triangular matrix");
printf("\n5.exit");
printf("\nEnter the selection:");
scanf("%d",&c);
switch(c)
{
case 1:
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
m+=a[i][j];
}
}
x=0;
r=n-1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((x==i)&&(r==j))
s+=a[i][j];
}
r--;
x++;
}
y=s+m;
printf("\nThe trace of matrix is:%d",y);
break;
case 2:
for(i=0;i<n;i++)
{  
sum=0;sum1=0;
for(j=0;j<n;j++)
{
sum+=a[i][j];
sum1+=a[j][i];
}
b[i]=sum;
d[i]=sum1;
}
printf("\nThe row sum of matrix:");
for(i=0;i<n;i++)
printf("%d ",b[i]);
printf("\nThe column sum of matrix:");
for(i=0;i<n;i++)
printf("%d ",d[i]);
break;
case 3:
printf("\nThe strict lower triangular matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i<=j)
printf("%d ",0);
else
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\nThe strict upper triangular matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i>=j)
printf("%d ",0);
else
printf("%d ",a[i][j]);
}
printf("\n");
}
break;
case 4:
printf("\nThe lower triangular matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i>=j)
printf("%d ",a[i][j]);
else
printf("%d ",0);
}
printf("\n");
}
printf("\nThe upper triangular matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i<=j)
printf("%d ",a[i][j]);
else
printf("%d ",0);
}
printf("\n");
}
break;
}
}
getch();
}
output

Enter the size of matrix:2

Enter the values of matirx1
2
3
4

The matrix is:
1 2
3 4

1.The trace of matrix
2.Column sum and row sum
3.print upper and lower triangular matrix
4.strict upper and lower triangular matrix
5.exit

Enter the selection:1

The trace of matrix is:10

1.The trace of matrix
2.Column sum and row sum
3.print upper and lower triangular matrix
4.strict upper and lower triangular matrix
5.exit

Enter the selection:2

The row sum of matrix:3 7
The column sum of matrix:4 6

1.The trace of matrix
2.Column sum and row sum
3.print upper and lower triangular matrix
4.strict upper and lower triangular matrix
5.exit

Enter the selection:3

The strict lower triangular matrix
0 0
3 0

The strict upper triangular matrix
0 2
0 0

1.The trace of matrix
2.Column sum and row sum
3.print upper and lower triangular matrix
4.strict upper and lower triangular matrix
5.exit

No comments:

Post a Comment