-2

I need a function that I could use to check if I could multiply two matrices. In it, I should check if the matrices are the same dimension, if not function returns -1, else returns 1. Later, function below multiplies matrices and returns **matrix previously declared.

//function prototype
double** matrixMultiply(double** M1, int r1, int c1, double** M2, int r2, int c2);

//I need to check if M1 and M2 have the same number of rows and cols, but I dont know how

double** matrixElementwiseMultiply(double** M1, double** M2, int rows, int columns)

Error message should be -1, else the function should carry on. I would use a flag.

Pers
  • 15
  • 8
  • They have the same dimensions if an only if `r1 == r2 && c1 == c2`. What could possibly be your problem in implementing this condition??? – goodvibration Jun 02 '19 at 09:10
  • My bad, I overlooked that. However, in other functions the dimensions are not defined, such as: double** matrixAdd(double** M1, double** M2, int rows, int columns); How do I check if the matrices are the same dimension here? – Pers Jun 02 '19 at 09:12
  • actually for multiplication should be r1 == c2 && r2 == c1 but remains simple :) – OznOg Jun 02 '19 at 09:13
  • @OznOg: I'm aware of that, but this dude wrote "same dimensions"... – goodvibration Jun 02 '19 at 09:14
  • @Pers: You should pass the dimensions from the calling function. – goodvibration Jun 02 '19 at 09:15
  • @Pers You'll _have_ to pass the size as a parameter. Otherwise, there is no way to know the size of the 2D array – Spikatrix Jun 02 '19 at 09:29

1 Answers1

0

Thanks, it works now. Checking wheather or not the matrices were same dimension (meaning that matrix 1 dimensions x*y is the same dimension of matrix 2) wasn't actually being tested in the program, and I all had to check is if the number of columns in matrix one is equal to number of rows in matrix two (if c1==r2).

Pers
  • 15
  • 8