读取和打印矩阵C

读取和打印矩阵C,c,matrix,C,Matrix,我的任务是从文件中读取数字,形成一个矩阵。 每行的前两个整数是行和列,其余整数是矩阵中的数据 2 2 1 2 3 4 看起来像 1 2 3 4 我能够使用以下方法成功加载到一个矩阵中: void RdMatrix(FILE *file, int (*matrix)[MAXSIZE][MAXSIZE], int *row, int *column) { int data; int matRow = RdRowSize(file); int matCol = RdCol

我的任务是从文件中读取数字,形成一个矩阵。 每行的前两个整数是行和列,其余整数是矩阵中的数据

2 2 1 2 3 4
看起来像

1 2
3 4
我能够使用以下方法成功加载到一个矩阵中:

void RdMatrix(FILE *file, int (*matrix)[MAXSIZE][MAXSIZE], int *row, int *column)
{
    int data;

    int matRow = RdRowSize(file);
    int matCol = RdColumnSize(file);
    *row = matRow;
    *column = matCol;
    int i, j;

    for (i = 0; i < matRow; i++)
    {
        for (j = 0; j < matCol; j++)
        {
            fscanf(file, "%d", &data);
            *matrix[i][j] = data;
        }
}
然后打印出来

1 2 
9 8

9 8 
7 6
什么时候应该打印

1 2
3 4

9 8
7 6
我不允许使用任何库,也不会有帮助,因为我以后必须在汇编中重新编写它

编辑:包括代码

#include <stdio.h>
#define MAXSIZE 10
FILE *fpin;

int RdRowSize(FILE *file)
{   
    int row;
    fscanf(file, "%d", &row);
    return row;
}

int RdColumnSize(FILE *file)
{
    int col;
    fscanf(file, "%d", &col);
    return col;
}

void RdMatrix(FILE *file, int (*matrix)[MAXSIZE][MAXSIZE], int *row, int *column)
{
    int data;

    int matRow = RdRowSize(file);
    int matCol = RdColumnSize(file);
    *row = matRow;
    *column = matCol;
    int i, j;

    printf("\n=====================\nLoading Matrix\n=====================\n");
    for (i = 0; i < matRow; i++)
    {
        for (j = 0; j < matCol; j++)
        {
            fscanf(file, "%d", &data);
            *matrix[i][j] = data;
        }
    }
}

void PrMat(int(*matrix)[MAXSIZE][MAXSIZE], int row, int col)
{   
    int i, j;
    printf("\n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            printf("%d ", *matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}


int main(void) 
{

    int RsizeM, CsizeM;                                 /*matrix row size and column size*/
    int A[MAXSIZE][MAXSIZE], B[MAXSIZE][MAXSIZE];       /*the two matrices*/
    int rowA=0, columnA=0, rowB=0, columnB=0;           /* the row and column sizes of A and B */




    /*open input file - file name is hardcoded*/
    fpin = fopen("INA1.txt", "r");                      /* open the file for reading */
    if (fpin == NULL) 
    {
        fprintf(stdout, "Cannot open input file  - Bye\n");
        return(-1);                                 /* if problem, exit program*/
    }
    /*ASSUMPTIONS: the file is not empty and contains has at least 1 set of matrices*/


    /* Add while loop after testing a single iteration */ 


    RdMatrix(fpin, &A, &rowA, &columnA);
    RdMatrix(fpin, &B, &rowB, &columnB);

    PrMat(&A, rowA, columnA);
    PrMat(&B, rowA, columnB);



    fclose(fpin);  /* close the file */

    return (0);
}
#包括
#定义最大尺寸10
文件*fpin;
int RdRowSize(文件*文件)
{   
int行;
fscanf(文件“%d”行和行);
返回行;
}
int RdColumnSize(文件*文件)
{
int col;
fscanf(文件“%d”和列(&col);
返回列;
}
无效RdMatrix(文件*文件,int(*矩阵)[MAXSIZE][MAXSIZE],int*行,int*列)
{
int数据;
int matRow=RdRowSize(文件);
int matCol=RdColumnSize(文件);
*row=matRow;
*列=matCol;
int i,j;
printf(“\n============================\n加载矩阵\n============================\n”);
对于(i=0;i

然后它必须打开的文件名为INA1.txt

当引用元素时,应该使用
(*matrix)[i][j]
而不是
*matrix[i][j]

另外,当打印B矩阵时,它应该是
rowB
,而不是
rowA

您能显示完整的代码(包括主功能)吗?发布可编译代码有助于我们帮助您;-)。我相信你是对的,但这里真正的问题只是打字错误…;)*矩阵周围的括号修复了它。那只小罗后来会咬我的。非常感谢。
1 2
3 4

9 8
7 6
#include <stdio.h>
#define MAXSIZE 10
FILE *fpin;

int RdRowSize(FILE *file)
{   
    int row;
    fscanf(file, "%d", &row);
    return row;
}

int RdColumnSize(FILE *file)
{
    int col;
    fscanf(file, "%d", &col);
    return col;
}

void RdMatrix(FILE *file, int (*matrix)[MAXSIZE][MAXSIZE], int *row, int *column)
{
    int data;

    int matRow = RdRowSize(file);
    int matCol = RdColumnSize(file);
    *row = matRow;
    *column = matCol;
    int i, j;

    printf("\n=====================\nLoading Matrix\n=====================\n");
    for (i = 0; i < matRow; i++)
    {
        for (j = 0; j < matCol; j++)
        {
            fscanf(file, "%d", &data);
            *matrix[i][j] = data;
        }
    }
}

void PrMat(int(*matrix)[MAXSIZE][MAXSIZE], int row, int col)
{   
    int i, j;
    printf("\n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            printf("%d ", *matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}


int main(void) 
{

    int RsizeM, CsizeM;                                 /*matrix row size and column size*/
    int A[MAXSIZE][MAXSIZE], B[MAXSIZE][MAXSIZE];       /*the two matrices*/
    int rowA=0, columnA=0, rowB=0, columnB=0;           /* the row and column sizes of A and B */




    /*open input file - file name is hardcoded*/
    fpin = fopen("INA1.txt", "r");                      /* open the file for reading */
    if (fpin == NULL) 
    {
        fprintf(stdout, "Cannot open input file  - Bye\n");
        return(-1);                                 /* if problem, exit program*/
    }
    /*ASSUMPTIONS: the file is not empty and contains has at least 1 set of matrices*/


    /* Add while loop after testing a single iteration */ 


    RdMatrix(fpin, &A, &rowA, &columnA);
    RdMatrix(fpin, &B, &rowB, &columnB);

    PrMat(&A, rowA, columnA);
    PrMat(&B, rowA, columnB);



    fclose(fpin);  /* close the file */

    return (0);
}