1

so I am trying to figure out how to multiply 2 matrices using pointers. It successfully works the way it is now, but instead of using conventional array access methods, I would like to learn the use of pointers.

Here is my code:

#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
#include <iostream>

/* Routines called. */
int loadMatrixFromFile(char *filename, int *data);
void showMatrix(int *data, int len);
int makeIdent(int matrixB[5][5], int length);
int matrixA[5][5];
int matrixB[5][5];
int matrixC[5][5];
void multiplyMatrices(int matrixA[5][5], int matrixB[5][5],int matrixC[5][5]);

int main(){
    int len, data[1000];
    len = loadMatrixFromFile("Numbers.txt", data);
    showMatrix(data, len);
    makeIdent(matrixB,len);
    multiplyMatrices(matrixA, matrixB, matrixC);
}

int makeIdent(int matrixB[5][5], int len){
    int i,j;
    printf("Matrix B is: \n");
    for(i=0;i<5;i++){
           for(j=0;j<5;j++){
                 if(i==j){
                         matrixB[i][j]=1;
                         printf("%d ",matrixB[i][j]);
                 }
                 else{
                     matrixB[i][j]=0;
                     printf("%d ",matrixB[i][j]);
                 }
           }
           printf("\n");
     }
    return matrixB[i][j];
     printf("\n");
}
int loadMatrixFromFile(char *filename, int *data){
    FILE *in;
    int len;
    int j;
    in = fopen(filename, "r");
    if (in == NULL) {
        printf("Could not find file: %s \n", filename);
    }
    else {
        printf("Reading numbers...\n");
        fscanf(in, "%d", &len);
        printf("reading %d numbers from file %s ....\n", len, filename);
        for(j=0;j<len;j++) {
            fscanf(in, "%d", data + j);
        }
        fclose(in);
    }
    for(int i = 0; i<5; i++){
        for(int j = 0; j < 5; j++){
                matrixA[i][j] = *(data + i*5 + j);
        }
    }
    return len;
}
void showMatrix(int *data, int len){
    int j;
    int count = 0;
    printf("Showing %d numbers from data array....\n", len);
    printf("Matrix A is: \n");
    for(j=0;j<len;j++) {
        printf("%d ", *(data + j));
        count++;
        if(count % 5 == 0){
            printf("\n");
        }
    }
    printf("\n");
}

void multiplyMatrices(int matrixA[5][5], int matrixB[5][5],int matrixC[5][5]){
     int i, n, j;
     int count = 0;

     printf("\n");
     printf("Matrix A x Matrix B is: \n");
     for (i = 0; i<5; i++){
            for (j = 0; j<5; j++){
                matrixC[i][j] = 0;
                matrixC[i][j] += matrixA[i][j]*matrixB[i][j];
                printf("%d ",matrixC[i][j]);
                count++;
                if(count % 5 == 0){
                printf("\n");
      }
    }
  }
}
Shawn
  • 2,355
  • 14
  • 48
  • 98
  • 2
    `matrixC[i][j] += matrixA[i][j]*matrixB[i][j]` is *not* how matrix multiplication works! – Martin R Sep 17 '13 at 05:13
  • I do not see any benefit or use of pointers in this application. It will only complicate things. Following link explains how you can represent 2D matrix in terms of pointers. http://stackoverflow.com/questions/1052818/create-a-pointer-to-two-dimensional-array – Abhishek Bansal Sep 17 '13 at 05:15
  • @Martin R I had originally done matrixC[i][n] += matrixA[i][j]*matrixB[j][n], but it gave me a different matrix than the one I was looking for. Is this what you mean? – Shawn Sep 17 '13 at 05:17
  • `for(k = 0; k < 5; k++) { matrixC[i][j] += matrixA[i][k]*matrixB[k][j]; }` – Martin R Sep 17 '13 at 05:20
  • Decide which language you want to use. – n. m. could be an AI Sep 17 '13 at 05:28

1 Answers1

2

Well your algorithm for matrix multiplication is completely wrong.

You say 'I want to learn how to do it with pointers', but here's the thing, you're already are doing it with pointers.

In this code

void multiplyMatrices(int matrixA[5][5], int matrixB[5][5],int matrixC[5][5]){

the variables matrixA, matrixB and matrixC are pointers. In C++ it's impossible to have an array for a function parameter. It automatically gets converted to a pointer. It's also true that the syntax for accessing an array is identical to the syntax for accessing a pointer.

If you want to make it explicit that you are using pointers then rewrite your code like this

void multiplyMatrices(int (*matrixA)[5], int (*matrixB)[5],int (*matrixC)[5]){

Now you can see that matrixA, matrixB and matrixC pointers to arrays of 5 integers. You don't have to make any other changes. And in fact this change is exactly what the compiler does when you try to use an array as a function parameter.

Here's a good looking link that explains how pointers and arrays compare. The link talks about C, but the rules are the same in C++. Have a read it will probably help you understand better than I can.

john
  • 85,011
  • 4
  • 57
  • 81
  • Awesome link, it is much appreciated this is exactly what I needed to learn to use pointers. Best tutorial I have seen so far. – Shawn Sep 17 '13 at 06:24