Showing posts with label matrix. Show all posts
Showing posts with label matrix. Show all posts

Tuesday, 10 January 2012

C program to find the largest and smallest values of a matrix



#include<stdio.h>
main()
{
int a[10][10],l,s,i,j,p,q;
printf("Enter the order of the matrix\n");
scanf("%d%d",&p,&q);
printf("Enter the values\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&a[i][j]);
}
l=a[0][0];
s=a[0][0];
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
if(l<a[i][j])
l=a[i][j];
if(a[i][j]<s)
s=a[i][j];
}
}
printf("The matrix is\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("The biggest value = %d\n",l);
printf("The smallest value = %d\n",s);
}



Friday, 30 December 2011

C program to find the upper right and lower left triangles of a matrix



#include<stdio.h>
main()
{
int a[10][10],i,j,r,c;
printf("Enter the order of the matrix\n");
scanf("%d%d",&r,&c);
printf("Enter the values\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
if(r==c)
{
printf("\nUpper right triangle\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(j>=i)
printf("%d\t",a[i][j]);
else
printf("\t");
}
printf("\n");
}
printf("\nLower left triangle\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(j<=i)
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
else
printf("\nNot possible to print triangles");
printf("\n");
}




Thursday, 29 December 2011

C program to find the transpose of a matrix



#include<stdio.h>
main()
{
int a[10][10],b[10][10],i,j,r,c,t;
printf("Enter the order of the matrix\n");
scanf("%d%d",&r,&c);
printf("Enter the values\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
b[j][i]=a[i][j];
}
}
printf("Transpose of matrix is\n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
}




C program to find the sum of the diagonal elements(Trace) of a matrix



#include<stdio.h>
main()
{
int a[10][10],s,i,j,r,c;
printf("Enter the order of the matrix\n");
scanf("%d%d",&r,&c);
printf("Enter the values\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe matrix is\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
s=0;
if(r==c)
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
s=s+a[i][j];
}
}
printf("\nSum of diagonal elements = %d",s);
}
else
printf("\nNo diagonal elements");
printf("\n");
}



Tuesday, 27 December 2011

C program to find the product of two matrices



#include<stdio.h>
main()
{
int i,j,k,r1,c1,r2,c2,a[10][10],b[10][10],c[10][10];
printf("Enter the no. of rows and columns of matrix 1 : ");
scanf("%d%d",&r1,&c1);
printf("Enter the no. of rows and columns of matrix 2 : ");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("Enter the matrix 1\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the matrix 2\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<r1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("Product of matrices\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("Matrix multiplication not possible");
printf("\n");
}



C program to find the determinant and inverse of two matrices using recursion



#include<stdio.h>
#include<math.h>
float detrm(float[25][25],float);
void cofact(float[25][25],float);
void trans(float[25][25],float[25][25],float);
main()
{
float a[25][25],k,d;
int i,j;
printf("Enter the order of the matrix\n");
scanf("%f",&k);
printf("Enter the elements of the matrix\n");
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
scanf("%f",&a[i][j]);
}
}
d=detrm(a,k);
printf("Deteminant = %f",d);
if(d==0)
printf("\nMatrix is not inversible\n");
else
cofact(a,k);
}

float detrm(float a[25][25],float k)
{
float s=1,det=0,b[25][25];
int i,j,m,n,c;
if(k==1)
{
return(a[0][0]);
}
else
{
det=0;
for(c=0;c<k;c++)
{
m=0;
n=0;
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
b[i][j]=0;
if(i!=0&&j!=c)
{
b[m][n]=a[i][j];
if(n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det+s*(a[0][c]*detrm(b,k-1));
s=-1*s;
}
}
return(det);
}

void cofact(float num[25][25],float f)
{
float b[25][25],fac[25][25];
int p,q,m,n,i,j;
for(q=0;q<f;q++)
{
for(p=0;p<f;p++)
{
m=0;
n=0;
for(i=0;i<f;i++)
{
for(j=0;j<f;j++)
{
b[i][j]=0;
if(i!=q&&j!=p)
{
b[m][n]=num[i][j];
if(n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q+p)*detrm(b,f-1);
}
}
trans(num,fac,f);
}

void trans(float num[25][25],float fac[25][25],float r)
{
int i,j;
float b[25][25],inv[25][25],d;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=detrm(num,r);
inv[i][j]=0;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
inv[i][j]=b[i][j]/d;
}
}
printf("\nThe inverse of the matrix :\n");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
printf("\t%f",inv[i][j]);
}
printf("\n");
}
}