You are on page 1of 3

# /* Name:faizan khan\

Roll No:09EE1026

Assignment No:6 */

#include<stdio.h>
#include<math.h>
#define S 128

void add(int X[][S], int Y[][S], int add[][S],int order, int *sum, ) // function definition for Addison of two matrice two matrices
{
int i=0, j=0;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
}
*sum=*sum+1;

void subtract(int X[][S], int Y[][S], int sub[][S],int order, int *diff) // function definition for subtraction of two matrices
{
int i=0, j=0;
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
sub[i][j]=X[i][j]-Y[i][j];
}
*diff=*diff+1;
}

void form(int X[][S], int Y[][S], int x, int y, int o) //taking four matrices and putting it in one matrix
{

int i,j;
for(i=x;i<x+o;i++) {
for(j=y;j<y+o;j++)
Y[i][j]=X[i-x][j-y];
}
}

void divide(int X[][S], int Y[][S], int x, int y, int order) //Dividing the function into four parts

{
int i,j;
for(i=x;i<(x+order);i++) {
for(j=y;j<y+order;j++)
Y[i-x][j-y]=X[i][j];
}
}

void multiply(int A[][S], int B[][S], int product[][S], int o, int *pro,int *sum, int *diff)
{
int A11[S][S], B11[S][S], C11[S][S], A12[S][S], B12[S][S], C12[S][S], A21[S][S], B21[S][S], C21[S][S], A22[S][S], B22[S][S], C22[S][S];
int M1[S][S],M2[S][S],M3[S][S],M4[S][S],M5[S][S],M6[S][S],M7[S][S];
int sum1[S][S], sum2[S][S], sum3[S][S], sum4[S][S], sum5[S][S], sum6[S][S], sum7[S][S], sum8[S][S], sum9[S][S], sum10[S][S],sum11[S][S],
sum12[S][S], sum13[S][S], sum14[S][S];
if (o<2)
{
product[0][0]=A[0][0]*B[0][0];
*pro=*pro+1;
}
else
{
divide(A,A11,0,0,o/2);
divide(B,B11,0,0,o/2);
divide(A,A12,0,o/2,o/2);
divide(B,B12,0,o/2,o/2); //calling function divide
divide(A,A21,o/2,0,o/2);
divide(B,B21,o/2,0,o/2);
divide(A,A22,o/2,o/2,o/2);
divide(B,B22,o/2,o/2,o/2);

subtract(B12,B22,sum4,o/2,diff);
subtract(B21,B11,sum5,o/2,diff); //calling function subtract
subtract(A21,A11,sum7,o/2,diff);
subtract(A12,A22,sum9,o/2,diff);

multiply(sum1,sum2,M1,o/2,pro,sum,diff);
multiply(sum3,B11,M2,o/2,pro,sum,diff);
multiply(A11,sum4,M3,o/2,pro,sum,diff); //calling function multiply
multiply(A22,sum5,M4,o/2,pro,sum,diff);
multiply(sum6,B22,M5,o/2,pro,sum,diff);
multiply(sum7,sum8,M6,o/2,pro,sum,diff);
multiply(sum9,sum10, M7,o/2,pro,sum,diff);

subtract(M5,M7,sum12,o/2,diff);
subtract(M1,M2,sum13,o/2,diff);
subtract(sum11,sum12,C11,o/2,diff);

form(C11,product,0,0,o/2);
form(C12,product,0,o/2,o/2); //calling form function
form(C21,product,o/2,0,o/2);
form(C22,product,o/2,o/2,o/2);

## void print(int X[][S], int order) //prints the matrix

{
int i,j;
for(i=0; i<order; i++)
{
for(j=0;j<order;j++)
{
printf("%d\t", X[i][j]);
}
printf("\n");
}
}

int main()
{
int order, i=0, j=0;
int order_new, A[S][S], B[S][S], product[S][S];
int *pro, *sum, *diff, p, s, d;
sum=&s;
pro=&p;
diff=&d;
p=s=d=0;
printf("Enter the order of the square matrices A and B:");
scanf("%d", &order);
order_new=order;

for(i=1;;i*=2)
{
if(order<=i)
{
order_new=i;
break;
}
}
printf("The padded order for A and B is %d \n", order_new);

## /*Input of Matrix A*/

printf("Enter the contents of Matrix A: \n");
for (i=0;i<order_new;i++)
{
for(j=0;j<order_new;j++)
{
if(i<(order) && j<(order))
scanf("%d", &A[i][j]);
else
A[i][j]=0;
}
}

## /*Input of Matrix B*/

printf("Enter the contents of Matrix B: \n");
for (i=0;i<order_new;i++)
{
for(j=0;j<order_new;j++)
{
if(i<(order) && j<(order))
scanf("%d", &B[i][j]);
else
B[i][j]=0;
}
}

## printf("The padded matrix A is: \n");

print(A, order_new);

## printf("The padded matrix B is: \n");

print(B, order_new);

multiply(A,B,product,order_new,pro,sum, diff);

## printf("The product of A and B is: \n" );

print(product,order_new);

## printf("No. of times addition or subtraction is done: %d \n", *sum+*diff);

printf("No. of times multiplication is done: %d \n", *pro);

getch();
}