La lecture et l'Impression d'une Matrice C

J'ai une mission à lire les numéros à partir d'un fichier de forme d'une matrice.
Les deux premiers nombres entiers de chaque ligne sont en ligne et en colonne, et puis les autres entiers sont données dans la matrice.

2 2 1 2 3 4

ressemble

1 2
3 4

Je suis en mesure de charger avec succès dans une matrice à l'aide de:

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

Alors je peux imprimer avec

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");
}

Dans ma fonction principale, j'ai deux matrices A[MAXSIZE][MAXSIZE] et B[MAXSIZE][MAXSIZE], un rowA = 0, colA = 0; et rowB = 0, colB = 0;.
J'appelle RdMatrix(fpin, &A, &rowA, &columnA); RdMatrix(fpin, &B, &rowB, &columnB); PrMat(&A, rowA, columnA);

L'entrée ressemble à:

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

Puis il imprime

1 2 
9 8

9 8 
7 6

Alors qu'elle devrait être l'impression

1 2
3 4

9 8
7 6

Je ne suis pas autorisé à utiliser toutes les bibliothèques, ni de l'aide, depuis que j'ai ré-écrire cette assemblée plus tard.

EDIT: y Compris le Code

#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);
}

Ensuite le fichier a ouvrir est appelé INA1.txt

  • Pouvez-vous montrer le code complet (y compris la fonction principale)?
  • Affichage de code compilable nous aide à vous aider ;-).
InformationsquelleAutor Evan | 2013-09-23