#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 matrices. Show all posts
Showing posts with label matrices. 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();
}
Subscribe to:
Posts (Atom)