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?