简体   繁体   English

使用指针将2个矩阵相乘

[英]Multiplying 2 matrices using pointers

so I am trying to figure out how to multiply 2 matrices using pointers. 所以我想弄清楚如何使用指针将2个矩阵相乘。 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");
      }
    }
  }
}

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. 变量matrixAmatrixBmatrixC是指针。 In C++ it's impossible to have an array for a function parameter. 在C ++中,不可能有一个用于函数参数的数组。 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. 现在您可以看到指向5个整数的数组的matrixAmatrixBmatrixC指针。 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++. 该链接讨论的是C,但是规则在C ++中是相同的。 Have a read it will probably help you understand better than I can. 阅读它可能会帮助您比我更好地理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM