1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
//Matrix Multiplication using dynamic memory
//
//Julien Cohen
#include <iostream>
#include <fstream>
using namespace std;
double mymult(double **s,double **a, double **b,int t,int r,int j);
main(){
ifstream inf;
ifstream inf1;
ofstream outf("matrix_out.txt");
double **x,**y;
int j,k,r,t;
inf.open("matrix1.txt");
if (!inf.is_open()){
cout << "Input file failed" << endl;
return 1;
}
inf1.open("matrix2.txt");
if (!inf.is_open()){
cout << "Input file failed" << endl;
return 1;
}
inf>>j;
inf>>k;
inf1>>r;
inf1>>t;
x=new (nothrow) double* [j];
y=new (nothrow) double* [r];
if(x==0||y==0) cout<<"Error: Nothrow exception.\n"<<endl;
for(int q=0; q<j; q++){
x[q]=new (nothrow) double[k];
}
for(int w=0; w<r; w++){
y[w]=new (nothrow) double[t];
}
for(int i=0; i<j; i++){
for(int n=0; n<k; n++){
inf>>x[i][n];
}
}
for(int i=0; i<j; i++){
for(int n=0; n<k; n++){
inf1>>y[i][n];
}
}
//Now declaring the output matrix to be used in mymult
int v,b;
double** c;
c=new (nothrow) double* [v];
if(c==0) cout<<"Error: Nothrow exception.\n"<<endl;
for(int q=0; q<v; q++){
c[q]=new (nothrow) double[b];
}
mymult(c,x,y,t,r,j);
for(int i=0; i<j; i++){
for(int n=0; n<t; n++){
outf<<c[i][n];
}
}
return 0;
}
double mymult(double **s,double **a, double **b,int t,int r,int j){
for(int d=0;d<j;d++){
for(int e=0;e<t;e++){
for(int g=0;g<r;g++){
double sum=0;
s[j][e]=((a[j][g])*(b[g][e]));
}
}
}
}
|