开发者

print matrix from file

I use this code for loading matrix:

 FILE *inputMatrix=fopen("file","r");
 fscanf(inputMatrix,"%d",&num);
 for(i=0;i<num;++i){
    for(j=0;j<num+1;++j){
        fscanf(inputMatrix,"%f",&A[i][j]);
  开发者_如何转开发  }
 }
 fclose(inputMatrix);
 int numRow=num;
 int numColumn=num+1;

and then I want to print it in main by method printMatrix:

void printMatrix(int numColumn, int numRow ,int matrix[numRow][numColumn]){
   int i = 0, j = 0;
   printf("nc: %i, nr: %i\n",numColumn,numRow);
    for(i = 0 ; i < numRow ; i++)
    {
        for(j = 0 ; j < numColumn ; j++)
        {
            printf("%.2f  ", matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

Here is the question. When I use in main: printf("matrix[1][1] is %f",matrix[1][1]) => it is loaded successfully from file, but when I try to print it by my method, which prints as well, I get 0.00 for every piece in matrix. Why so? Am I missing some referrence?


May be you should use double or float type for matrix array.


You have an integer matrix, therefore you should use %d instead of %f / %.2f for scanning/printing


float A[10][11];

but...

void printMatrix(int numColumn, int numRow ,int matrix[numRow][numColumn]){

A float definition, but the method is expecting an int array. I'm stunned this compiled, but gcc threw off enough warnings that should give one pause:

$ gcc -o 5448492 5448492.c 
5448492.c:30: warning: conflicting types for ‘printMatrix’
5448492.c:24: note: previous implicit declaration of ‘printMatrix’ was here
5448492.c: In function ‘printMatrix’:
5448492.c:37: warning: format ‘%.2f’ expects type ‘double’, but argument 2 has type ‘int’

Because C requires all but the first array index to be known at compile time, you can make the program function by declaring a prototype for printMatrix() that includes the last array index size. (I got it wrong in my comment. Sorry.)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void printMatrix(int numColumn, int numRow ,float matrix[][11]);

int main (int argc,char *argv[]) {

  int num;
  int i,j;
  float A[10][11];

  FILE *inputMatrix=fopen("yourfile.in","r");
  fscanf(inputMatrix,"%d",&num);
  int numRow=num;
  int numColumn=num+1;

  for(i=0;i<numRow;++i){
     for(j=0;j<numColumn;++j){
         fscanf(inputMatrix,"%f",&A[i][j]);
     }
  }
  fclose(inputMatrix);

  printMatrix(numColumn,numRow,A);

  return 0;
}


void printMatrix(int numColumn, int numRow ,float matrix[][11]){
   int i = 0, j = 0;
   printf("nc: %i, nr: %i\n",numColumn,numRow);
     for(i = 0 ; i < numRow ; i++)
     {
       for(j = 0 ; j < numColumn ; j++)
       {
         printf("%f  ", matrix[i][j]);
       }
       printf("\n");
     }
     printf("\n");
}

And the output:

$ ./5448492 
nc: 3, nr: 2
1.000000  1.000000  3.000000  
2.000000  2.000000  5.000000  

I fiddled with other stuff while tracking this one down (and standardized the indents at two spaces; it's awfully small, but consistency on non-favorite beats inconsistency every time), but I'm pretty sure the only thing I changed of substance was the prototype of the function to include the last array dimension.


SInce you use an array 10x11, 11 should be hard coded in other places as well. When passing a multidimensional array like this, all dimensions except the highest must be hard coded. Otherwise, you will get erroneous results.

The reason is that your functions think that the array has numColumn columns but the array in memory has actually 11 columns.

I would make these changes:

include ...
...

-- note the *float** and the **[11]**
void printMatrix(int numColumn, int numRow ,float matrix[][11]);

int main (int argc,char *argv[]) {

  int num;
  int i,j;
  float A[10][11];  
...
}

-- note the *float** and the **[11]**
void printMatrix(int numColumn, int numRow ,float matrix[][11]){
...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜