乘以2二维数组 [英] Multiplying two 2D arrays

查看:195
本文介绍了乘以2二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个二维数组相乘的问题。我是pretty确保矩阵matrixA和matrixB被妥善保存,当程序运行它们被正确显示。当我尝试乘2阵列在一起我得到了很多的1和0的长串。上可能是什么问题的任何想法?

下面是我的code:

 的#include<&stdio.h中GT;
#包括LT&;&CONIO.H GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&iostream的GT;/ *过程调用。 * /
INT loadMatrixFromFile(字符*文件名,INT *数据);
无效showMatrix为(int *数据,INT LEN);
INT makeIdent(中间体matrixB [5] [5],INT长度);
INT矩阵matrixA [5] [5];
INT matrixB [5] [5];
INT matrixC [5] [5];
空隙multiplyMatrices(INT矩阵matrixA [5] [5],整数matrixB [5] [5],整数matrixC [5] [5]);诠释主(){
    INT LEN,数据[1000];
    LEN = loadMatrixFromFile(Numbers.txt数据);
    showMatrix(数据,LEN);
    makeIdent(matrixB,LEN);
    multiplyMatrices(矩阵matrixA,matrixB,matrixC);
}INT makeIdent(INT matrixB [5] [5],INT LEN)
{
    INT I,J;
    的printf(黑客帝国B = \\ n);
    对于(I = 0; I&小于5;我++){
           为(J = 0; J&小于5; J ++){
                 如果(我== j)条{
                         matrixB [I] [J] = 1;
                         的printf(%d个,matrixB [I] [J]);
                 }
                 其他{
                     matrixB [I] [J] = 0;
                     的printf(%d个,matrixB [I] [J]);
                 }
           }
           的printf(\\ n);
     }
    返回matrixB [I] [J]。
     的printf(\\ n);
}
INT loadMatrixFromFile(字符*文件名,INT *数据)
{
    FILE *的;
    INT LEN;
    诠释J;
    在FOPEN =(文件名,R);
    如果(在== NULL){
        的printf(找不到文件:%s \\ n,文件名);
    }
    其他{
        的printf(读数... \\ n);
        的fscanf(在%d个,&安培; LEN);
        的printf(读书%D编号从文件%s .... \\ n,LEN,文件名);
        为(J = 0; J< LEN; J ++){
            的fscanf(在%D,数据+ J);
        }
        fclose函数(在);
    }
    的for(int i = 0;我小于5;我++){
        为(中间体J = 0; J&小于5; J ++){
                矩阵matrixA [I] [J] = *的数据;
        }
    }
    返回LEN;
}
无效showMatrix为(int *数据,INT LEN)
{
    诠释J;
    诠释计数= 0;
    的printf(,LEN从数据数组.... \\ n显示%D编号);
    的printf(矩阵A = \\ n);
    为(J = 0; J< LEN; J ++){
        的printf(%d个*(数据+ J));
        算上++;
        如果(计数%5 == 0){
            的printf(\\ n);
        }
    }
    的printf(\\ n);
}空隙multiplyMatrices(INT矩阵matrixA [5] [5],整数matrixB [5] [5],整数matrixC [5] [5]){
     INT I,N,J;         的printf(\\ n);
 的printf(Matrix一个的X矩阵B = \\ n);
 对于(I = 0; I&小于5;我++){
    为(n = 0时; N小于5; N ++){
        为(J = 0; J&小于5; J ++){
            matrixC [I] [N] + =矩阵matrixA [I] [J] * matrixB [J] [N];
            的printf(%d个,matrixC [I] [N]);
      }
    }
  }
}

下面是该文本文件的外观,该数组A​​正在从拉:

  251 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7


解决方案

好像什么错在这里:

矩阵matrixA [I] [J] = *数据;

您应该使用

矩阵matrixA [I] [J] = *(数据+ I * 5 + J);

此外,尝试打印:

的printf(%d个,matrixC [I] [N]);

第二循环中 N (内部循环后Ĵ)。

so I am having an issue multiplying two 2d arrays together. I am pretty sure that matrixA and matrixB are being stored properly, they are displayed properly when the program is ran. When I try to multiply the 2 arrays together I am getting a long string of a lot of 1's and 0's. Any idea on what could be the problem?

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 = \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;
        }
    }
    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 = \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;

         printf("\n");
 printf("Matrix A x Matrix B = \n");
 for (i = 0; i<5; i++){
    for (n = 0; n<5; n++){
        for (j = 0; j<5; j++){
            matrixC[i][n] += matrixA[i][j]*matrixB[j][n];
            printf("%d ",matrixC[i][n]);
      }
    }
  }
}

Here is how the text file looks that the array A is being pulled from:

25 

1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7

解决方案

Seems something wrong here:

matrixA[i][j] = *data;

You should use

matrixA[i][j] = *(data + i*5 + j);

Also, try printing:

printf("%d ",matrixC[i][n]);

inside second loop n (after innermost loop j).

这篇关于乘以2二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆