I have been trying to create a program to read a 4 column tab-delineated file into a variably sized array and then print the array to the screen, but when I try the code below, it gives me a diagonally symmetrical array, ie [0][1] is the same as [1][0], which is wrong.
This is my code:
And the data set I am working with isCode:int main () { FILE *file_ptr = 0; int nrows; nrows=1; float a,b; int i; int j; char data[100]; float e,f,c,d; float myarray[10][nrows]; printf("Enter location of input file.\n"); scanf("%s", data); file_ptr = fopen(data, "r"); for (i=0; !feof(file_ptr); i++) { fscanf(file_ptr,"%f%f%f%f",&e,&f,&c,&d); myarray[0][i]=e; myarray[1][i]=f; myarray[2][i]=c; myarray[3][i]=d; if (e =!NULL){nrows++;} } fclose(file_ptr); printf("\n%d data sets retrieved.\n\n",i); for (j=0;j<i;j++) { printf("%f\t%f\t%f\t%f\n",myarray[0][j],myarray[1][j],myarray[2][j],myarray[3][j]); } return 0; }
13.079 0.051658 0.77359 0.00052980
14.162 0.051750 0.0097982 0.0019695
15.888 0.054027 0.80159 0.0019485
16.191 0.054512 0.62303 8.9929e-05
17.339 0.055797 0.99587 0.00041386
18.784 0.056146 0.25421 0.0016562
19.967 0.056846 0.60045 0.00038078
20.437 0.058294 0.61663 0.00014638
21.397 0.058011 0.026190 0.00032348
22.998 0.059799 0.26090 0.00092712
23.456 0.059883 0.64564 0.0016402
24.070 0.060772 0.83006 0.0017486
25.109 0.060415 0.80091 0.00093706
26.887 0.061940 0.80918 6.3588e-05
27.527 0.063505 0.86446 0.0012275
28.860 0.064270 0.31924 0.0014887
29.616 0.063735 0.63400 0.0016803
30.430 0.065634 0.64624 0.00025035
31.863 0.067435 0.20427 0.00051878
32.326 0.067050 0.0066200 0.00017732
33.043 0.068377 0.71793 0.0017101
What am I doing wrong? I am fairly new to coding, so simple answers would be much appreciated!



LinkBack URL
About LinkBacks



