Hello all;
I have tried my best to see to the end of this "TROUBLE", but still I couldn't figure out what the problem is.
I want to multiply a row vector M1(1X3) to a column vector M2(3X1) and store the resultant in a memory location assigned to a float variable M1_M2(scalar).
the
M1 = [1, 4, 7]; while
M2 = [3, 5, 9]' and the expected result would be
M1_M2 = 86. But to my surprise my code below "amazingly" returns
2.70933e+259 in place of the actual value of
86.
Please help me out
CODE
#include <iostream>
#include <vector>
#include <math.h>
#include <cstdlib>
using namespace std;
// nRows and nCols stands for the number of rows and columns for the array
const int nRows1 = 1;
const int nCols1 = 3;
const int nCols2 = 1;
const int nRows2 = 3;
// Method to multiply the two arrays
double multiplyMatrix(double M1[nRows1][nCols1], double M2[nRows2][nCols2], double M1_M2) {
double X[10][10][10];
int row,col,m; // counters
for(row = 0; row < nRows1; row++) {
for(col=0;col<nCols2;col++) {
// initialise the resultant variable to 0.
M1_M2 = 0;
for(m=0;m<3;m++) {
X[row][col][m] += M1[row][m]*M2[m][col];
M1_M2 += X[row][col][m];
}
}
}
}
int main () {
double M1[nRows1][nCols1] = {1, 4, 7}; // declaration of row vector of order 1 by 3
double M2[nRows2][nCols2] = {{3},{5},{9}}; // declaration of column vector of order 3 by 1
double M1_M2; // declaration of the scalar to store the result of multiplying both vectors
multiplyMatrix(M1, M2, M1_M2); // function call
cout << M1_M2 << endl; // to display the result
return 0;
}