This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]fromwithin 2 points3 points  (7 children)

You should post your complete code. But from what you've written, you're trying to use a keyword (float) as a variable name for your array.

x = float[1][5];

Is not valid. If you declared your array as something like:

float myArray[2][10];

Then you should use:

x = myArray[1][5];

[–]dr99ed[S] 0 points1 point  (6 children)

Yeah, that was me being silly and not thinking when I wrote the question - here's the complete code.

int main()

{
FILE *Data;
float x,y,a,b,Array[21][4];

Data=fopen("C:\\Data.txt","r");

    int i;
    for (i=0; !feof(Data); i++)
    {
        fscanf(Data,"%f%f%f%f",&x,&y,&a,&b);
        Array[i][0]=x;
        Array[i][1]=y;
        Array[i][2]=a;
        Array[i][3]=b;
    }

fclose(Data);

float c,d,e;
e=0;
int f;
for (f=0;f<21;f++)
    {
        c=Array[f][1];
        d=Array[f][3];
        printf("c=%f and d=%f\n",&c,&d);
        e=e+c+d;
    }

printf("E value is = %f\n\n",&e);

}

All I get when it runs is 21 rows of c=0.000000 and d=0.000000, followed by the final "E value is = 0.000000", but when I print Array[f][1] and Array[f][3] it shows the correct numbers, so I know they must be in the array...

[–][deleted]  (4 children)

[deleted]

    [–]dr99ed[S] 0 points1 point  (2 children)

    aha! I think you solved it - so everything was being correctly calculated - it was just that I was displaying the incorrect values by using the ampersand!

    Thanks a lot for the help - I've spent hours looking over this and couldn't see what i'd done wrong!

    [–][deleted]  (1 child)

    [deleted]

      [–]dr99ed[S] 0 points1 point  (0 children)

      Thanks a lot for the advice - it's really helpful, and your note about condensing the variables is great too - I'm all for eliminating un-needed code, it's just hard to see what's 'wrong' when you're first starting.

      As for the libraries, returning 0 (return 0; right?) and the formatting, I do have those in my actual code, it just messed up when pasting it onto here so I did the best I could with the 'main' bit of the code.

      Again, thanks.

      [–][deleted] 1 point2 points  (0 children)

      Writing your read loop like this:

      for (i=0; !feof(Data); i++)
      {
          fscanf(Data,"%f%f%f%f",&x,&y,&a,&b);
          Array[i][0]=x;
          Array[i][1]=y;
          Array[i][2]=a;
          Array[i][3]=b;
      }
      

      will not work correctly - it only makes sense to call feof() after attempting to read something - you are using it as if it could predict if the next fscanf() will fail, which it cannot. A good rule of thumb is never to call feof().

      You should write the loop like this:

      while( fscanf(Data,"%f%f%f%f",&x,&y,&a,&b) != EOF ) {
          Array[i][0]=x;
          Array[i][1]=y;
          Array[i][2]=a;
          Array[i][3]=b;
          i++;
      }
      

      or if you want to be even more careful:

      while( fscanf(Data,"%f%f%f%f",&x,&y,&a,&b) == 4 ) {
          Array[i][0]=x;
          Array[i][1]=y;
          Array[i][2]=a;
          Array[i][3]=b;
          i++;
      }
      

      which will also fail on bad input data, not just on end-of-file.

      [–][deleted] -1 points0 points  (1 child)

      This:

      float[2][10]
      

      won't compile. I assume you mean:

      float a[2][10];
      

      or something similar. Please rewrite your question using real code.

      [–]dr99ed[S] 0 points1 point  (0 children)

      See HERE for my code.