Thread: question on multiplying 2D arrays

1. question on multiplying 2D arrays

I am having trouble writing a function to multiply two matricies together. Here is what I have so far, what needs to be done? The fxn is called matrixMultiply. Another question on my printArray fxn is how can I make it so that there are spaces between the numbers?

Code:
```//Name: Joseph Valenzuela
//Date: April 22, 2003
//Purpose: Matrix multiplication

#include <iostream.h>

const int MAX_ROWS = 10;
const int MAX_COLS = 10;

// Precondition: None
// Postcondition: Cells of the matrix have been filled with
// data entered at the keyboard.  Rows and Cols return the
// actual number of rows and columns.
void readArray(int nums[][MAX_ROWS], int& rows, int& cols);
//Postcondition: Prints the array by rows
void printArray(int array[][MAX_COLS], int rows, int cols);

void main()
{
int array2d[MAX_ROWS][MAX_COLS];
int num_rows=0,num_cols=0;

printArray(array2d,num_rows,num_cols);
printArray(array2d,num_rows,num_cols);

}

void readArray(int nums[][MAX_ROWS], int& rows, int& cols)
{
// Accept # of rows, must be from 1-MAX_ROWS
do
{
cout << "Enter the number of rows: ";
cin >> rows;
if (rows <= 0 || rows > MAX_ROWS)
cout << "Must be between 1 and " << MAX_ROWS << endl;
} while (rows <= 0 || rows > MAX_ROWS);

// Accept # of columns, must be between 1 and MAX_COLS
do
{
cout << "Enter the number of columns: ";
cin >> cols;
if (cols <= 0 || cols > MAX_ROWS)
cout << "Must be between 1 and " << MAX_COLS << endl;
} while (cols <= 0 || cols > MAX_COLS);

// Prompt user and accept all matrix values
for (int i=0; i<rows; i++)
for (int j=0; j<cols; j++)
{
cout << "[" << i << "," << j << "]: ";
cin >> nums[i][j];
}
}

void printArray(int array[][MAX_COLS], int rows, int cols)
{
int x,y;
for(x=0; x<rows; x++)
{
for(y=0; y<cols; y++)
cout << array[x][y];
cout << endl;
}
}

/*int matrixMultiply(int array[][MAX_ROWS], int& rows, int& cols)
{
for(int i; i<rows; i++) //for each row i in matrix1 do...
for(int j; j<cols; j++) //for each column j in matrix2 do...
forint(k; k<cols; k++)//for each column k in matrix1 do...
//increase matrix3[i,j] by matrix1[i,k]*matrix2[k,j]*/```

2. This code stores the results of the multiplication in one of the matrices:
Code:
```#include <iostream>

using namespace std;

void multiply(int a[][1], int b[][1])
{
for(int i=0; i<2; i++)
{
for(int j=0; j<1; j++)
{
a[i][j] *= b[i][j];
}
}
}

int main()
{
int matrix[2][1]={5, 6};
int multX2[2][1]={2, 2};

cout<<matrix[0][0]<<" "<<matrix[1][0]<<endl;

multiply(matrix, multX2);

cout<<matrix[0][0]<<" "<<matrix[1][0]<<endl;

return 0;
}```

3. If you want to store the results of multiplying two arrays together in a third array, then it's more complicated:
Code:
```#include <iostream>
using namespace std;

int** multiply(int a[][1], int b[][1]);

int main()
{
int matrix[2][1]={5, 6};
int multX2[2][1]={2, 2};

cout<<matrix[0][0]<<" "<<matrix[1][0]<<endl;

int** result= multiply(matrix, multX2);

cout<<result[0][0]<<" "<<result[1][0]<<endl;

//Free up memory:
for(int i=0; i<2; i++)
{
delete [] result[i];
//Deletes each individual pointer in the array
//but since each pointer is an array itself
//the brackets preceding the name of the array
//are required.
}
delete [] result;
//Deletes the array of pointers

return 0;
}

int** multiply(int a[][1], int b[][1])
{
//You can't return a type int c[][1] with the result because
//such an array
//declared within the function body would go out of scope
//when the function ended, so you have to dynamically create
//a two dimensional array for the results and return a pointer
//to the array, which is a bit tricky.

int** c;
//Declares a pointer to an array of pointers.
//You can think of c as the name of the array of pointers.
//An int* can point to an array of int's, so
//a pointer to an array of int pointers is of type pointer
//to int* or int**.

c = new int*[2];
//Creates an array of pointers(corresponding
//to the two rows of the matrix in main())
//and assigns the address to c.

c[0] = new int[1];
c[1] = new int[1];
//Creates two int arrays with 1 column each
//(corresponding to the columns of the matrix in main())
//and assigns address to pointers c[0] and c[1] from
//the array of pointers c

for(int i=0; i<2; i++)
{
for(int j=0; j<1; j++)
{
c[i][j] = a[i][j] * b[i][j];
}
}

return c;
}```

4. Parameters

Here is what I have for my function to multiply the matricies, but it doesnt compile and I am haveing trouble knowing what parameters I need for the fxn call...
Code:
```int matrixMultiply(int matrix1[][MAX_COLS],int matrix2[][MAX_COLS] int rows_a, cols_a, cols_b)
{
int matrix3[MAX_ROWS][MAX_COLS];
int sum=0;
for(int i=0; i<rows_a; i++){
for(int j=0; j<cols_b; j++){
for(int k=0; k<cols_a; k++){
sum+=(matrix1[i][k]*matrix2[k][j]);
matrix3[i][j]=sum;

}
}
}
}
return(matrix3[i][j]);
}```

5. Well, I posted example code for two different ways to do it. I suggest you actually take a look at them. The first post shows you exactly what parameters are required, what the return type is, and how to multiply the matrices together. Read the second one, and once again it shows you exactly what the parameters are, what the return type has to be, and how to multiply the matrices together.

Where do you need to store the results of multiplying the two matrices together? If you put them in one of the matrices you sent to the function, then it's simple--that's demonstrated in the first program. If you need to put them in a third matrix, then it's more complicated, and I showed you how to do that in the second program.

Currently, your return type is int. Are you returning a number like 8? Looking at your return statement,

return(matrix3[i][j]);

you're trying to return a 2d array, which is very different than an int like 8. Arrays are addresses like pointers. However, as I explained in the comments in the second program, you CANNOT return an array you created in a function. Why? Because when the function ends, the array is destroyed, so it won't exist anymore, and you end up returning something that points to a blank spot in memory. To solve that problem, the return type has to be int** and you have to dynamically create the array in the function.

If none of this makes any sense, then the program you're attempting is probably beyond your understanding at this time.