-3
#include <iostream>
#include <math.h>
#include <stdio.h>
#define column 3
#define row 3
#define share 3

int matrix_multiplication(int left_matrix[][column], int right_matrix[][column], int result_matrix[][column], int rows, int cols, int shared);

int A[][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
},
B[][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
}, C[3][3]; //initialize "hard coded" three matrices

int main() {
    matrix_multiplication(A, B, C, row, column, share); //passes address of each matrix to function
    return 0;
}

int matrix_multiplication(int left_matrix[][column], int right_matrix[][column], int result_matrix[][column], int rows, int cols, int shared) {
    int i, j, k;

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {//stays within first column and row through first iteration
            for (k = 0; k < 3; k++)//ensures inner dimensions match with variable k i.e. ixk * kxj =ixj or A*B=C
                result_matrix[i][j] += right_matrix[i][k] * left_matrix[k][j]; //C programming has ability to perform matrix multiplication
            //Adds result of each operation to C matrix i.e. +=
            printf("%d\t", result_matrix[i][j]); //matrix C is composed of rows(i) and columns(j)
        }//new tab after each column iteration
        printf("\n"); //new line for each row iteration
    }
    return 0;
}

This code is an good example of passing multidimensional arrays to a function using pointers and printing multidimensional arrays after mutliplication. There are multiple ways to indicate pointers to the compiler. I recommend seeing the "Correct way of passing a 2 dimensional array into a function." for example:

/*void display(int (*p)[numcols],int numRows,int numCols)//First method//
void dispaly(int *p,int numRows,int numCols) //Second Method//
void dispaly (int p[][numCols],int numRows,int numCols)  //Third Method*/
  • A two dimensional array is `int**`, not `int*`. What error are you getting on the multiplication line? – Jason Baker Nov 05 '14 at 19:24
  • 3
    @JasonBaker No, `int **` is not a **2d** array it's an array of pointers. – Marco Nov 05 '14 at 19:25
  • 1
    You cannot pass 2d arrays through as parameters in c or c++ as a simple `int arr[][]` parameter. – Jay Nov 05 '14 at 19:26
  • 1
    possible duplicate of [Correct way of passing 2 dimensional array into a function](http://stackoverflow.com/questions/9446707/correct-way-of-passing-2-dimensional-array-into-a-function) – Marco Nov 05 '14 at 19:27
  • Please post the full original error message, with the file names and line numbers. – pts Nov 06 '14 at 00:05
  • line 24 "expected primary-expression before ']' token" – Jeremy Johnson Nov 06 '14 at 00:08
  • http://stackoverflow.com/questions/16004668/c-allocating-a-matrix-in-a-function/27366086#27366086 – 42n4 Dec 08 '14 at 20:42

1 Answers1

1

Remove the column variable, and add this above the matrix_multiplication function declaration:

#define column 3

(You may also want to rename column to COLUMNS.)

In C++ you can also do this:

static const int column = 3;

or, in C++11:

constexpr int column = 3;

The idea behind all this is that all but the very first size of a multidimensional array must be known at compile time.


To fix the expected primary-expression before ']' token" error, change your inner assignment to something like this:

result_matrix[i][j] += right_matrix[i][k] * left_matrix[k][j];

Also you should initialize result_matrix with 0s first.


Also remove the * from int *result_matrix[][column].

Most modern compilers display warnings if you pass an int instead of an int*. Please enable all warnings in your compiler, recompile, fix them, and update your question stating that the example code compiles cleanly, without warnings.


To print an element of the matrix, you have to specify which element you want to print:

printf("%d\t", result_matrix[i][j]);

I can't believe that your compiler didn't display a warning when you omitted the [i][j]. Warnings are for your benefit: they indicate possible bugs in your code.

pts
  • 80,836
  • 20
  • 110
  • 183
  • Perhaps I'm not following, I have already removed the column variable and replaced it with the directive #define column 3 above the function prototype statement. – Jeremy Johnson Nov 06 '14 at 00:14
  • 1
    `int* result_matrix[][column]` is also wrong, and `printf("%d\t", result_matrix);` is brave, but doomed to failure. – Crowman Nov 06 '14 at 00:19
  • It was int* result_matrix[][column]. Can't believe I missed that. The 3X3 prints properly with the new tab printf line but the values are all 4223008 in the 3X3 array. – Jeremy Johnson Nov 06 '14 at 00:33
  • I've added a couple of more suggestion to my answers and fixed the indexes (`[i][j]` instead of `[i][k]`) in the `+=' formula. – pts Nov 06 '14 at 00:44
  • 1
    Thank you for your patience. This is my first programming class, and this forum has really helped! It was the print statement lacking the desired elements. – Jeremy Johnson Nov 06 '14 at 00:46
  • http://stackoverflow.com/questions/16004668/c-allocating-a-matrix-in-a-function/27366086#27366086 – 42n4 Dec 08 '14 at 20:42