C
code to multiply two matrix by recursion:
#include <stdio.h>
#define MAX 10
void multiplyMatrix( int [MAX][MAX], int [MAX][MAX]);
int m,n,o,p;
int c[MAX][MAX];
int main(){
int a[MAX][MAX],b[MAX][MAX],i,j,k;
printf( "Enter
the row and column of first matrix: " );
scanf( "%d
%d" ,&m,&n);
printf( "Enter
the row and column of second matrix: " );
scanf( "%d
%d" ,&o,&p);
if (n!=o){
printf( "Matrix multiplication is not possible" );
printf( "\nColumn
of first matrix must be same as row of second matrix" );
}
else {
printf( "Enter the First matrix:
" );
for (i=0;i<m;i++)
for (j=0;j<n;j++)
scanf( "%d" ,&a[i][j]);
printf( "Enter the Second matrix:
" );
for (i=0;i<o;i++)
for (j=0;j<p;j++)
scanf( "%d" ,&b[i][j]);
printf( "\nThe First matrix is:
\n" );
for (i=0;i<m;i++){
printf( "\n" );
for (j=0;j<n;j++){
printf( "%d\t" ,a[i][j]);
}
}
printf( "\nThe Second matrix is:
\n" );
for (i=0;i<o;i++){
printf( "\n" );
for (j=0;j<p;j++){
printf( "%d\t" ,b[i][j]);
}
}
multiplyMatrix(a,b);
}
printf( "\nThe multiplication of two matrix is: \n" );
for (i=0;i<m;i++){
printf( "\n" );
for (j=0;j<p;j++){
printf( "%d\t" ,c[i][j]);
}
}
return 0;
}
void multiplyMatrix( int a[MAX][MAX], int b[MAX][MAX]){
static int sum,i=0,j=0,k=0;
if (i<m){ //row of first
matrix
if (j<p){ //column of
second matrix
if (k<n){
sum=sum+a[i][k]*b[k][j];
k++;
multiplyMatrix(a,b);
}
c[i][j]=sum;
sum=0;
k=0;
j++;
multiplyMatrix(a,b);
}
j=0;
i++;
multiplyMatrix(a,b);
}
}
Sample
output:
Enter the row and column of first
matrix: 2 2
Enter the row and column of
second matrix: 2 2
Enter the First matrix: 1 2 3 4
Enter the Second matrix: 2 3 4 5
The First matrix is:
1 2
3 4
The Second matrix is:
2 3
4 5
The multiplication of two
matrix is:
10 13
22 29
C
code to multiply two matrix without using recursion:
#include <stdio.h>
#define MAX 10
void multiplyMatrix( int [MAX][MAX], int [MAX][MAX]);
int m,n,o,p;
int c[MAX][MAX];
int main(){
int a[MAX][MAX],b[MAX][MAX],i,j,k;
printf( "Enter
the row and column of first matrix: " );
scanf( "%d
%d" ,&m,&n);
printf( "Enter
the row and column of second matrix: " );
scanf( "%d
%d" ,&o,&p);
if (n!=o){
printf( "Matrix
multiplication is not possible" );
printf( "\column
of first matrix must be same as row of second matrix" );
}
else {
printf( "Enter
the First matrix: " );
for (i=0;i<m;i++)
for (j=0;j<n;j++)
scanf( "%d" ,&a[i][j]);
printf( "Enter
the Second matrix: " );
for (i=0;i<o;i++)
for (j=0;j<p;j++)
scanf( "%d" ,&b[i][j]);
printf( "\nThe
First matrix is: \n" );
for (i=0;i<m;i++){
printf( "\n" );
for (j=0;j<n;j++){
printf( "%d\t" ,a[i][j]);
}
}
printf( "\nThe
Second matrix is: \n" );
for (i=0;i<o;i++){
printf( "\n" );
for (j=0;j<p;j++){
printf( "%d\t" ,b[i][j]);
}
}
multiplyMatrix(a,b);
}
printf( "\nThe
multiplication of two matrix is: \n" );
for (i=0;i<m;i++){
printf( "\n" );
for (j=0;j<p;j++){
printf( "%d\t" ,c[i][j]);
}
}
return 0;
}
void multiplyMatrix( int a[MAX][MAX], int b[MAX][MAX]){
int sum,i=0,j=0,k=0;
while (i<m){ //row of first matrix
j=0;
while (j<p){ //column of second matrix
k=0;
sum=0;
while (k<n){
sum=sum+a[i][k]*b[k][j];
k++;
}
c[i][j]=sum;
j++;
}
i++;
}
}
6 comments:
Give the C++/Java code and time complexity for checking whether two indices i and j exist in
a sorted array a of integers such that a[i] + a[j] = x for some value x
Can some one pliz help with his!!
#include
#define MAX 10
int n=3;
int c[MAX][MAX]={0};
void mult(int a[][3],int b[][3],int i,int j,int k)
{
printf("\n%d %d %d",i,j,k);
if(i>n-1)
return;
else if(k>n-1)
{
mult(a,b,i,j+1,0);
}
else if(j>n-1)
{
mult(a,b,i+1,0,0);
}
else
{
c[i][j]+=a[i][k]*b[k][j];
mult(a,b,i,j,k+1);
}
}
int main(){
int a[][3]={1,2,3,1,2,3,1,2,3};
int b[][3]={1,2,3,1,2,3,1,2,3};
int i,j,k;
printf("\nThe First matrix is: \n");
for(i=0;i<n;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",a[i][j]);
}
}
printf("\nThe Second matrix is: \n");
for(i=0;i<n;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",b[i][j]);
}
}
mult(a,b,0,0,0);
printf("\nThe multiplication of two matrix is: \n");
for(i=0;i<n;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",c[i][j]);
}
}
getch();
return 0;
}
can anybody help? the code with recursion does not work can u tell me why? or what might be problem?
can anybody help? the code with recursion does not work can u tell me why? or what might be problem?
post in your solution or personally mail me
can any one tell me one example which is possible by recursion but not by loop and vice versa
Post a Comment