Hello I know I may receive some grief for not inserting code correctly it is my first post...

My question is this:
I am reading a huge list of values from a file and I need to store these elevations into an array so they can be used to calculate average values and what not later on in my program...but right now I am not able to store the correct values into the array how I would like for some reason. The first 192 values in my text file look like this:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 12 23 23
48 53 67 140 167 270 378 468 533 497 298 256 239 239 241 238
239 341 621 491 462 505 854 1092 885 1193 903 757 610 873 843 1303
1240 1391 1409 1178 1092 728 591 512 508 613 772 899 1165 1424 1446 1352
1544 1751 1973 2111 2190 2371 2473 2085 1707 1542 1276 1205 1184 1054 0 0

There are 16 columns and each column contains 5 spaces/numbers they are also right hand aligned in the text file.


Here is my code:

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

int main()
 {

//--local variable definitions--
  int i=0;
  int j=0;
  int k=0;
  float a[16][190];
  FILE *cfptr1;

//--open the file--
  cfptr1=fopen("topography.dat","r");

//--check the file read--
  if(cfptr1==NULL) 
   {
    printf("Error can't read file.\n");
    return 1;
   }

//--save the file to an array--
  else 
   {
    i=0;
    j=0;
    printf("File opened successfully.\n");
    while(!feof(cfptr1))
     {
      for (i=0; i<16; i++)
       {
        for (j=0; j<190; j++)
         {
          fscanf(cfptr1, "%5f", &a[i][j]);
         }
       }
      }
    }
 
//--print the array to check values--
  i=0;
  j=0;
  for (i=0; i<1; i++)
   {
    for (j=0; j<190; j++)
     {
      printf("%f\n", a[i][j]);
     }
   }


//--close the file--
  fclose(cfptr1);

return(0);
 }

and here is the output I am receiving:

File opened successfully.
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
3.000000
27.000000
57.000000
158.000000
179.000000
221.000000
294.000000
474.000000
708.000000
499.000000
826.000000
1231.000000
1312.000000
1071.000000
1106.000000
876.000000
561.000000
912.000000
1131.000000
983.000000
1074.000000
1066.000000
971.000000
1075.000000
1453.000000
950.000000
651.000000
972.000000
711.000000
821.000000
658.000000
737.000000
1026.000000
972.000000
688.000000
823.000000
1023.000000
845.000000
357.000000
411.000000
421.000000
388.000000
430.000000
357.000000
408.000000
475.000000
523.000000
565.000000
712.000000
947.000000
1295.000000
1590.000000
1681.000000
1497.000000
1609.000000
1761.000000
1948.000000
1636.000000
1209.000000
1267.000000
1509.000000
1598.000000
1715.000000
1757.000000
1777.000000
1866.000000
1887.000000
1998.000000
1945.000000
1849.000000
1811.000000
1839.000000
1842.000000
1665.000000
1680.000000
2021.000000
1913.000000
1785.000000
1666.000000
1762.000000
1765.000000
1643.000000
1747.000000
2062.000000
1877.000000
1695.000000
1653.000000
1751.000000
1761.000000
1757.000000
1695.000000
1468.000000
1483.000000
1766.000000
1892.000000
1841.000000
1738.000000
1323.000000
1212.000000
1264.000000
1630.000000
1680.000000
1300.000000
1180.000000
1201.000000
1233.000000
1309.000000
1440.000000
1506.000000
1362.000000
1486.000000
1508.000000
1388.000000
1599.000000
1471.000000
1530.000000
1511.000000
1688.000000
1744.000000
1592.000000
1477.000000
1344.000000
1278.000000
1261.000000
1368.000000
1312.000000
1278.000000
1272.000000
1286.000000
1360.000000
1428.000000
1524.000000
1764.000000
1484.000000
1351.000000
1357.000000
1499.000000
1907.000000
1813.000000
1500.000000
1607.000000
1695.000000
1589.000000
1521.000000
1514.000000
1644.000000
1853.000000
2066.000000
1613.000000
1412.000000
1366.000000
1365.000000
1373.000000
1421.000000
1384.000000
1408.000000
1478.000000
1596.000000
1722.000000
1599.000000
1411.000000
1482.000000
1548.000000
1703.000000
1832.000000
1809.000000
1724.000000
1610.000000
1795.000000
1927.000000
2011.000000
1908.000000


Can anyone help me to solve this issue of my printed values not matching the expected values?

Recommended Answers

All 10 Replies

Try

fscanf(cfptr1, "%f", &a[i][j]);

instead of

fscanf(cfptr1, "%5f", &a[i][j]);

It doesn't make much sense to use format specifiers in your case anyway.

Some general observations regarding the code:

NEVER use magic numbers like this in your code:

for (i=0; i<16; i++)
       {
        for (j=0; j<190; j++)
         {

or

float a[16][190];

Not only is it totally meaningless to any other person, in case the array size has to be modified later, its not an easy job. Use #defines or const's.

And do you need an else here?

//--check the file read--
  if(cfptr1==NULL) 
   {
    printf("Error can't read file.\n");
    return 1;
   }

//--save the file to an array--
  else 
   {
    i=0;
    j=0;
 .
 .

Because you are printing them in float format , they are printing default 6 digits after the point. To solve this change the printing line to :

printf("%.0f\n", a[i][j]);

then after the point it will print 0 digit.

Vicky:

If I'm not mistaken won't "#define" deny me the ability to store values into the array from the file? Also, if you could explain what you mean by magic numbers?

No no..... u are supposed to use #define only for a convenient way to set the dimensions of the 2D array. As you see the dimensions 16 and 192 are used in a lots of places. If you are to change any of them u will have to change the whole code, surely making mistakes.

Rather

#define dim1 16
#define dim2 190

int main
{

float a[dim1][dim2];

for(int i=0;i<dim1;i++)
     for(int j=0; j<dim2;  j++)
     {
           //do your stuff
     }

}

Now any time u change the dimension you just change the dim1 or dim2 value at the beginning. defining the dimensions has nothing to do with the value in the array.

No #define will not cause that problem... Thats more of a programming style issue.

Magic numbers are the constant numbers used in your programs, which give no clue regarding their origin. Wikipedia calls it "an unnamed or ill-documented numerical constant"

An example is the array size you specified: float a[16][190]; What are 16 and 190? You may know, but nobody else will know.
Its better to name the constants:

#define NUM_ROWS 16       
#define NUM_COLUMNS 190

Now you can use these constants everywhere in your program:

for (i=0; i<NUM_ROWS; i++)
       {
        for (j=0; j<NUM_COLUMNS; j++)
         {
          fscanf(cfptr1, "%f", &a[i][j]);
         }

That way, not only is it more readable, if the array size changes any time, you don't have to hunt throughout your code to make changes.

Okay I have made the suggested changes, also renamed my array to "elev" instead of "a." Although I am still seeing that the values I am printing to the screen are not the values I am expecting (these have remained the same values as I mentioned before).

Here is that section of my new code:

#define ncol 16
#define nrow 190

int main()
 {

//--local variable definitions--
  int i=0;
  int j=0;
  int k=0;
  float elev[ncol][nrow]={0};
  FILE *cfptr1;

//--open the file--
  cfptr1=fopen("topography.dat","r");

//--check the file read--
  if(cfptr1==NULL) 
   {
    printf("Error can't read file.\n");
    return 1;
   }

//--save the file to an array--
  i=0;
  j=0;
  printf("File opened successfully.\n");
  while(!feof(cfptr1))
   {
    for (i=0; i<ncol; i++)
     {
      for (j=0; j<nrow; j++)
       {
        fscanf(cfptr1, "%f", &elev[i][j]);
       }
     }
   }

//--print the array to check values--
  i=0;
  j=0;
  for (i=0; i<1; i++)
   {
    for (j=0; j<nrow; j++)
     {
      printf("%.1f\n", elev[i][j]);
     }
   }

Read the replies carefily:

//--print the array to check values--
  i=0;
  j=0;
  for (i=0; i<1; i++)
   {
    for (j=0; j<nrow; j++)
     {
      printf("%.1f\n", elev[i][j]);
     }
   }

Here is the line you use to print :

printf("%.1f\n", elev[j]);

change it to:

printf("%.0f\n", elev[j]);

the ".0" will eliminate all unnecessary 0's

Say if it helps

You seem to have got your rows and columns mixed up... If you're using an array as a matrix, then the first index usually denotes rows, the second one columns.

Here is the code that is working on my system, and should work on yours too. If its not, then there's something wrong with the input file, check that...

#define ncol 16
#define nrow 190

int main()
 {

//--local variable definitions--
  int i=0;
  int j=0;
  int k=0;
  float elev[nrow][ncol]={0};
  FILE *cfptr1;

//--open the file--
  cfptr1=fopen("topography.dat","r");

//--check the file read--
  if(cfptr1==NULL) 
   {
    printf("Error can't read file.\n");
    return 1;
   }

//--save the file to an array--
  i=0;
  j=0;
  printf("File opened successfully.\n");
  while(!feof(cfptr1))
   {
    for (i=0; i<nrow; i++)
     {
      for (j=0; j<ncol; j++)
       {
        fscanf(cfptr1, "%f", &elev[i][j]);
       }
     }
   }

//--print the array to check values--
  i=0;
  j=0;
  for (i=0; i<nrow; i++)
   {
    for (j=0; j<ncol; j++)
     {
      printf("%.1f\n", elev[i][j]);
     }
   }
}

I'm not really worried about the number of zeroes after the numbers since it's a matter of checking to see that my array was stored right, but that helps me get rid of unnecessary printed items.

Read the replies carefily:

//--print the array to check values--
  i=0;
  j=0;
  for (i=0; i<1; i++)
   {
    for (j=0; j<nrow; j++)
     {
      printf("%.1f\n", elev[i][j]);
     }
   }

Here is the line you use to print :

printf("%.1f\n", elev[j]);

change it to:

printf("%.0f\n", elev[j]);

the ".0" will eliminate all unnecessary 0's

Say if it helps

I figured it out but this also helped me out much more than I would have alone...I just realized that in reality it's a 190 by 190 array not a 16 by 190 array like the text file is.

You seem to have got your rows and columns mixed up... If you're using an array as a matrix, then the first index usually denotes rows, the second one columns.

Here is the code that is working on my system, and should work on yours too. If its not, then there's something wrong with the input file, check that...

#define ncol 16
#define nrow 190

int main()
 {

//--local variable definitions--
  int i=0;
  int j=0;
  int k=0;
  float elev[nrow][ncol]={0};
  FILE *cfptr1;

//--open the file--
  cfptr1=fopen("topography.dat","r");

//--check the file read--
  if(cfptr1==NULL) 
   {
    printf("Error can't read file.\n");
    return 1;
   }

//--save the file to an array--
  i=0;
  j=0;
  printf("File opened successfully.\n");
  while(!feof(cfptr1))
   {
    for (i=0; i<nrow; i++)
     {
      for (j=0; j<ncol; j++)
       {
        fscanf(cfptr1, "%f", &elev[i][j]);
       }
     }
   }

//--print the array to check values--
  i=0;
  j=0;
  for (i=0; i<nrow; i++)
   {
    for (j=0; j<ncol; j++)
     {
      printf("%.1f\n", elev[i][j]);
     }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.