C Source Code.

Cao Changyong changyoc at daisy.siue.edu
Mon Jan 17 17:56:34 EST 1994


/*
A comerade wrote:

>Hello comerades

>I am just developing a C program to create an input file, read some 
>part of data from the file and use them for computation. At this 
>time, i have been to create and read(and print to screen) values from 
>the input file created. The file looks like this:

1 1 1 45.98
1 1 2 34.09
1 2 1 44.09
1 2 2 65.98
2 1 1 34.99
2 1 2 74.07
2 2 1 87.09
2 2 2 66.90

>All attempts to use the fourth column of data for further computation 
>have failed. This column is a three dimensional array of the first 
>three column. For example, I obtain very funny numbers as I try to 
>multiply the fourth column of data by say 2. Has any c programmer 
>faced this problem. I am very sure I am not doing something right 
>this is the way my code looks like

    The problem was caused by mixed data types.  The following modified
code worked.
   


cao
XIS




*/
#include <stdio.h>
main ()
{
int c1,c2,c3,a,h,s;
FILE *infile;
float d[5][5][5],t[5][5][5];
float v1[5][5][5],v2[5][5][5],v3[5][5][5];

a=h=s=2;
if((infile = fopen("test.dat", "r")) == NULL)
{
  fprintf(stderr,"Error opening file");
  exit(1);
}
for(c1=0; c1<a; c1++)
{
  for(c2=0; c2<h; c2++)
   {
    for(c3=0; c3<h; c3++)
     {
    fscanf(infile,"%f %f %f %f", &v1[c1][c2][c3],&v2[c1][c2][c3],
    &v3[c1][c2][c3], &d[c1][c2][c3]);
    printf("%f %f %f %f", 
    v1[c1][c2][c3],v2[c1][c2][c3],v3[c1][c2][c3],d[c1][c2][c3]);
      
    t[c1][c2][c3]=2.0*d[c1][c2][c3];      
    printf("%10.4f\n",t[c1][c2][c3]);
    /*this is where the error sets in; t[c1][c2][c3] just comes as 
    gabbage */
     }
    }
  }
  fclose(infile);
  }
  
  















More information about the grass-user mailing list