c - I want position, but I get ncols and nrows of array -
i'm student, , i'm trying last piece of code working on assignment. has been asked million times, haven't been able find solution works me. can last position of array print when run program. can't use shortcuts (vectors, maxelement(), etc). can help?
#define inputfile "c:\\c_txt_files\\wind.txt" int i, j, nrows, ncols; double wind_max, wind_min, wind_array[12][5], index_minmonth=0, index_minyear=0, index_maxmonth=0, index_maxyear=0; fscanf(wind, "%d %d", &nrows, &ncols); /*find min , max, , compute average each row. locate min , max*/ wind_min = wind_array[0][0]; wind_max = wind_array[0][0]; for(i=0; i<nrows; i++) { for(j=0; j<ncols; j++) { if(wind_array[i][j]<wind_min) wind_min = wind_array[i][j]; index_minmonth = i; index_minyear = j; if(wind_array[i][j]>wind_max) wind_max = wind_array[i][j]; index_maxmonth = i; index_maxyear = j; } } index_minmonth += 1; index_minyear += 1; index_maxmonth += 1; index_maxyear += 1; printf("minimum speed %2.0f mph in month %2.0f, year %1.0f.\n", wind_min, index_minmonth, index_minyear); printf("maximum speed %2.0f mph in month %2.0f, year %1.0f.", wind_max, index_maxmonth, index_maxyear);
this prints: minimum speed 5 mph in month 12, year 5. maximum speed 12 mph in month 12, year 5.
you have initialiaze variables nrows
, ncols
. otherwise working garbage values.
also need {}
in loop body.
otherwise changes value every single time , arrive @ last element's index.
*later op mentioned reading file. initialization part not needed reads file itself.
Comments
Post a Comment