hi guys,
I am writing a c program that generates a two dimensional array to make matrix and a vector of random numbers and perform multiplication. I can't figure out whats wrong with my code. It generates a matrix of random numbers but all the numbers in the vector array is same and so is the result array. I don't see anything wrong with my multiplication algorithm but could anyone check it, please?
here is my code. help me figure out whats wrong with this
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
const int mat_rows = 5;
const int mat_cols = 4;
const int vec_rows = mat_cols;
const int vec_cols = 0;
int matrix [mat_rows] [mat_cols];
int vector [vec_rows] [0];
int result [mat_rows] [0];
int res;
int main ()
{
srand (time(NULL));
/*Filling matrix with random value between 0 - 10 */
for (int i = 0; i < mat_rows; i++)
for (int j = 0; j < mat_cols; j++)
matrix [j] = rand () % 50;
/*Filling vector with random value between 0 - 10*/
for (int i = 0; i < vec_rows; i++)
vector [0]= rand () % 50;
cout << "Printing out the matrix" << "\n" << endl;
for (int i = 0; i < mat_rows; i++)
{
for (int j = 0; j < mat_cols; j++)
cout << matrix [j] << " ";
cout << endl;
}
cout << "Printing out the vector" << "\n" << endl;
for (int i = 0; i < vec_rows; i++)
cout << vector [0] << endl;
for (int i = 0; i < mat_rows; i++)
{
res = 0;
int vrows = 0;
for (int j = 0; j < mat_cols; j++)
{
res = res + ((matrix [j]) * (vector [vrows] [0]));
vrows++;
}
result [0] = res;
}
for (int i = 0; i < mat_rows; i++)
cout << result [0] << endl;
return 0;
}
thanks