85039

Matrix multiplication function C++

Question:

I seem to have an issue with my matrix multiplication function.

When I run the program I just get an n x n matrix with all of the values the same, as some wired double value, e.g 21312e-2 Here is my function code:

void Multiply(int i, int j, double mat1[10][10], double mat2[10][10]) { double mat3[10][10]; for (int r = 0; r < i; r++) { for (int c = 0; c < j; c++) { for (int in = 0; in < i; in++) { mat3[r][c] += mat1[r][in] * mat2[in][c]; } cout << mat3[r][c] << " "; } cout << "\n"; }

}

mat1 and mat 2 are read into the program in the main thread using the function read:

void read_matrix(int m, int n, double mat[10][10]) { int i, j; for (i = 0; i<m; ++i) for (j = 0; j<n; ++j) cin >> mat[i][j]; }

Edit:Main Code

int main() { int i1, i2, j1, j2; double mat1[10][10], mat2[10][10], mat3[10][10]; scanf_s("%d %d\n", &i1, &j1, mat1); read_matrix(i1, j1, mat1); scanf_s("%d %d\n", &i2, &j2, mat2); read_matrix(i2, j2, mat2); printf("%d x %d matrix\n", i1, j1); print_matrix(i1, j1, mat1); printf("\n%d x %d matrix\n", i2, j2); print_matrix(i2, j2, mat2); Multiply(i1, j2, mat1, mat2); system("pause"); return 0; }

Answer1:

You need to fill mat3 with a zero value before adding to it.

Simplest way is to use:

double mat3[10][10] = {};

Recommend

  • Getting a garbage value while printing matrix in C
  • Paste matrix in R
  • How does one test if a matrix in Python has only 1's and 0's?
  • Pass by Value and Pass by Reference
  • Difference between “&” and std::reference_wrapper?
  • overloading the extraction operator >> in C++ [duplicate]
  • iOS 8.3 - Metal, found nil while unwrapping an Optional Value
  • XSLT 1.0: copy everything except certain nodes according to value and variable
  • problem with calling libraries
  • Creating BackgroundSubtractorMOG2 in Android by OpenCV 3.0 RC1
  • In BASH convert a string with . in float
  • How to retrieve collisions of unordered map?
  • Automatically change the position of objects when using different devices
  • running median of constant size array
  • std::string::substr throws std::out_of_range but the arguments are in limit
  • Need help to stop program terminating without users consent
  • How to transform Byte[](decoded as PNG or JPG) to Tensorflows Tensor
  • Iterating over a container bidirectionally
  • What's the essential difference between these two variadic functions?
  • std::system Exception when instantiating a singleton object
  • Runtime error in UVA Online Judge [closed]
  • c++ regex_replace not doing intended substitution
  • Reduction and collapse clauses in OMP have some confusing points
  • Very simple C++ DLL that can be called from .net
  • output of program is not same as passed argument
  • Akka Routing: Reply's send to router ends up as dead letters
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • LevelDB C iterator
  • unknown Exception android
  • Can't mass-assign protected attributes when import data from csv file
  • Checking variable from a different class in C#
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal