#include <stdio.h>
void main()
{
int m1[10][10],i,j,k,m2[10][10],prod[10][10],r1,c1,r2,c2;
printf("Enter number of rows and columns of first matrix:\n");
scanf("%d%d",&r1,&c1);
printf("Enter number of rows and columns of second matrix:\n");
scanf("%d%d",&r2,&c2);
if(r2==c1)
{
printf("Enter elements of First matrix (row wise):\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
printf("Matrix1 is :\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d ",m1[i][j]);
printf("\n");
}
printf("Enter elements of Second matrix (row wise):\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
printf("Matrix2 is:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d ",m2[i][j]);
printf("\n");
}
printf("Product of the Matrices (M1 x M2):\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
prod[i][j]=0;
for(k=0;k<r1;k++)
prod[i][j]+=m1[i][k]*m2[k][j];
printf("%d\ ",prod[i][j]);
}
printf("\n");
}
}
else
{
printf("Matrices can't be multiplied.\n");
printf("No. of columns of first matrix and no of rows of second are different.");
}
}
Search This Blog
Showing posts with label matrix. Show all posts
Showing posts with label matrix. Show all posts
C Program for Matrix Multiplication
This is a c program for Matrix Multiplication. To understand how it works, you should first know how matrix multiplication is done mathematically.
C Program To Represent Directed or Undirected Graph Using Adjacency Matrix
C program to represent directed or undirected graph using Adjacency matrix.
#include<stdio.h>
#include<conio.h>
#define max 20
int adj[max][max]; //Adjacency matrix
int n; //Denotes number of nodes in the graph
void main()
{
int max_edges,i,j,origin,destin;
char graph_type;
printf("Enter number of nodes : ");
scanf("%d",&n);
printf("Enter type of graph, directed or undirected.\n");
printf("Enter d for directed and u for undirected:\n ");
fflush(stdin);
scanf("%c",&graph_type);
if(graph_type=='u')
max_edges=n*(n-1)/2;
else
max_edges=n*(n-1);
for(i=1;i<=max_edges;i++)
{
printf("Enter edge %d( 0 0 to quit ) : ",i);
scanf("%d %d",&origin,&destin);
if( (origin==0) && (destin==0) )
break;
if( origin > n || destin > n || origin<=0 || destin<=0)
{
printf("Invalid edge!\n");
i--;
}
else
{
adj[origin][destin]=1;
if( graph_type=='u')
adj[destin][origin]=1;
}
}
printf("The adjacency matrix is :\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%4d",adj[i][j]);
printf("\n");
}
getch();
}
C Program to Sort a Matrix Column wise using Pointers
This is a c program to sort a mxn matrix column wise in ascending order using pointers. To sort column wise in descending order, just replace the '>' symbol in the comparing line of code into '<'. That part of the code is marked with a comment 'comparison'.
#include<stdio.h>
main()
{
int a[7][7],i,j,k,m,n,temp;
//reading
printf("enter number of rows of matrix\n");
scanf("%i",&m);
printf("enter number of columns of matrix\n");
scanf("%i",&n);
printf("enter the elements\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
//displaying
printf("the matrix you entered:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%4d",*(*(a+i)+j));
}
}
//sorting
for(i=0;i<n;i++)
{
for(k=0;k<m-1;k++)
{
for(j=0;j<m-k-1;j++)
{
//comparison
if( *(*(a+j)+i)> *(*(a+(j+1))+i))
{
temp=*(*(a+j)+i);
*(*(a+j)+i)=*(*(a+(j+1))+i);
*(*(a+(j+1))+i)=temp;
}
}
}
}
//displaying
printf("\nSorted Matrix:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%4d",*(*(a+i)+j));
}
}
}
Determinant of Matrix Program - Program to Find Determinant of Matrix
In this post i am sharing a C program to calculate determinant of matrices. Determinant exists only for square matrix. Here i have wrote a c program to find determinant of matrices of order less than or equal to 3.
#include<stdio.h>
void main() { int a[10][10],i,m,j; int det=0; printf("\nEnter the order of matrix:\n "); scanf("%i",&m); printf("\nenter elements\n"); for(i=0;i<m;i++) { for(j=0;j<m;j++) scanf("%d",&a[i][j]); } printf("\nThe matrix is\n"); for(i=0;i<m;i++) { printf("\n"); for(j=0;j<m;j++) printf("%3d",a[i][j]); } printf("\nthe transpose is\n"); for(i=0;i<m;i++) { for(j=0;j<m;j++) printf("%3i",a[j][i]); printf("\n"); } if(m==1) { det=a[0][0]; printf("det=%i",det); } else if(m==2) { int k=1,l=1; for(i=0;i<2;i++) for(j=0;j<2;j++) { if(i==j) k*=a[i][j]; else l*=a[i][j]; } printf("\ndeterminant=%i",k-l); } else if(m==3) { for(i=0;i<3;i++) det=det+(a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3]-a[1][(i+2)%3]*a[2][(i+1)%3])); printf("\nDeterminant of matrix is: %d",det); } else printf("This program can't calculate determinant of matrix of order greater than 3"); }
Subscribe to:
Posts (Atom)