Sie sind auf Seite 1von 3

//Implimented By Hamid Aslani

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
typedef double** Matrix_t; //points to vector of pointers to rows of dat
a
//functions which we should use in this program
//##############################################################################
#######################

void NoSpace()
{
cout<<"\nMemory exhausted!\n";
exit(-1);
}
//##############################################################################
#######################
Matrix_t Matrix(int N) /* allocate an NxN matrix */
{
int i; /* loop-index */
Matrix_t p = malloc(N*sizeof(double*));
if (p==NULL) NoSpace(); /* call user's exception-handler */
/* else, p -> vector of pointers to rows of doubles to be allocated */
for (i=0; i<N; i++)
{
p[i] = malloc(N*sizeof(double));
if (p[i]==NULL) NoSpace(); /* call exception-handler */
/* else, obtained N columns of data for row i */
}
return p;
}
//##############################################################################
#########################
void FreeMatrix(Matrix_t p, int N) /* de-allocates an NxN matrix */
{
int i; /* loop-index */
for (i=0; i<N; i++)
free(p[0]);
free(p);
}
//##############################################################################
#########################
void Quadrisect(Matrix_t p, int N,
Matrix_t p11, Matrix_t p12, Matrix_t p21, Matrix_t p22)
{
int N1 = N/2,
row,col; /* loop-indexes */
for (row=0; row<N1; row++) /* read data from p[0][0] ... p[N1-1][N1-1] */
for (col=0; col<N1; col++)
p11[row][col] = p[row][col];
for (row=0; row<N1; row++) /* read data from p[0][N1] ... p[N1-1][N-1] */
for (col=0; col<N1; col++)
p12[row][col] = p[row][col+N1];
for (row=0; row<N1; row++) /* read data from p[N1][0] ... p[N-1][N1-1] */
for (col=0; col<N1; col++)
p21[row][col] = p[row+N1][col];
for (row=0; row<N1; row++) /* read data from p[N1][N1] ... p[N-1][N-1] */
for (col=0; col<N1; col++)
p22[row][col] = p[row+N1][col+N1];
}
//##############################################################################
###########################
void AddMatrix(Matrix_t U, Matrix_t V, Matrix_t W, int N) /* W = U + V */
{
int row,col;
for (row=0; row<N; row++)
for (col=0; col<N; col++)
W[row][col] = U[row][col] + V[row][col];
}
//##############################################################################
############################
void SubMatrix(Matrix_t U, Matrix_t V, Matrix_t W, int N) /* W = U - V */
{
int row,col;
for (row=0; row<N; row++)
for (col=0; col<N; col++)
W[row][col] = U[row][col] - V[row][col];
}
//##############################################################################
#############################
// following function is invoked upon halting criterion: N=2
void MultMatrix(Matrix_t U, Matrix_t V, Matrix_t W,int rowU,int colU,int rowV,i
nt colV) // W = U*V,
{
if(colU != rowV)
{
cout<<"The column of first matrix should be equal to the row of
second matrix\n";
getch();
exit(-1);
}
int i,j,k;
double sum;
for (i=0; i< rowU; i++)
for (j=0; j < colV; j++)
{
W[i][j] = 0;
sum = U[i][0]*V[0][j];
for (k=0; k < colU; k++)
W[i][j] = W[i][j] + U[i][k] * V[k][j];
}
}
//##############################################################################
###############################
/* when C11 ... C22 are computed, merge these quadrants to form matrix C */
void QuadMerge(Matrix_t p11, Matrix_t p12, Matrix_t p21, Matrix_t p22,
int N, Matrix_t p)
{
int N1 = N/2, /* order of the block-matrices p11 ... p22 */
row,col; /* loop-indexes */
for (row=0; row<N1; row++)
for (col=0; col<N1; col++)
p[row][col] = p11[row][col];
for (row=0; row<N1; row++)
for (col=0; col<N1; col++)
p[row][col+N1] = p12[row][col];
for (row=0; row<N1; row++)
for (col=0; col<N1; col++)
p[row+N1][col] = p21[row][col];
for (row=0; row<N1; row++)
for (col=0; col<N1; col++)
p[row+N1][col+N1] = p22[row][col];
}
//#############################################################################
#################################
int main()
{
int choice;
cout<<"Enter one of the below options\n";
cout<<"1-Multiply two nxn matrices which n is power of 2\n";
cout<<"2-multiply two nxn matices which n is not power of 2\n";
cout<<"3- multiply two mxn matrices\n";
cin>>choice
}