edit: tried suggestions , didn't alter kind of works gives me incorrect data atleast aren't zeros. thier assistance.
i'm writing code reading grades text file , store them in array. full code rather large i'm posting part having issue in; while loop not changing values in array. sample data 65 99 87 76 89 37 -999 on separate lines. output shows me array values remained 0 after function should have changed values. other while loops failed attempts @ same task.
int readgrades(double grades[]){ file* gd; int count = 0; double ind; gd = fopen("sample.txt", "r"); while(count < max){ //max defined 100/array grades[max] fscanf(gd, "%lf", &ind); //should scan through items in file if(ind < 0) //end of sample data -999 breaks out of loop break; grades[count] = ind; //changes values in array (it doesn't change them 0) count++; } //increments index of array /*while(fscanf(gd, "%lf", &ind)>0){ grades[count] = ind; count++; }*/ /*do { fscanf(gd, "%lf", &grades[count]); printf("%.0lf", grades[count]); if(fscanf(gd, "%lf", &grades[count])== -999){ break; } count++; }while(fscanf(gd, "%lf", &grades[count])> 0);*/ fclose(gd); return count+1; } some info: grades array has initialized double filled w/ 0s. need replace zeros in grades[] data text file. i've been working on over day , still have had 0 progress. don't understand why doesn't change values in array.
edit: call readgrades array data.
int numgrades; numgrades = readgrades(grades); right inside main. #define max 100 , double grades[max] delcared outside main. return count+1 function needs return number of data items read.
someone asked full program:
#include <stdio.h> #include <stdlib.h> #include <math.h> int readgrades(double []); void frequency(double [], int); int maximum(double [], int); int minimum(double [], int); int deleteelement(double [], int, int); double mean(double [], int); double standarddeviation(double [], int); #define max 100 double grades[max]; int loc; int main(){ int numgrades; printf("%lf", grades); numgrades = readgrades(grades); loc = minimum(grades, numgrades); numgrades = deleteelement(grades, numgrades, loc); printf("the data has been adjusted removing minimum %.2lf\n", grades[loc]); loc = maximum(grades, numgrades); numgrades = deleteelement(grades, numgrades, loc); printf("the data has been adjusted removing maximum %.2lf\n", grades[loc]); printf("the adjusted mean %.2lf\n", mean(grades, numgrades)); printf("the adjusted standard deviation %.2lf\n", standarddeviation(grades, numgrades)); printf("here histogram of adjusted data:\n"); frequency(grades, numgrades); return 0; } int readgrades(double grades[]){ file* gd; int count = 0; double ind; gd = fopen("sample.txt", "r"); while(count < max){ fscanf(gd, "%lf", &ind); //double atod(ind); if(ind < 0) break; grades[count] = ind; count++; } /*while(fscanf(gd, "%lf", &ind)>0){ grades[count] = ind; count++; }*/ /*do { fscanf(gd, "%lf", &grades[count]); printf("%.0lf", grades[count]); if(fscanf(gd, "%lf", &grades[count])== -999){ break; } count++; }while(fscanf(gd, "%lf", &grades[count])> 0);*/ printf("%.0lf", grades[1]); fclose(gd); return count+1; } void frequency(double grades[], int numgrades){ int j, i, a=0, b=a+4; for(j=0; j<numgrades-1; j++){ printf("%d - %d|", a, b); for(i=0; i<numgrades; i++){ if(grades[i]<=b && grades[i]>=a){ printf("*"); } } printf("\n"); a+=5; if(a==100){ break; } } printf("%d|", a); for(j=0; j<numgrades; j++){ if(grades[i]==100) printf("*"); } printf("\n"); } int maximum(double grades[], int numgrades){ int i, h = 0; for(i=1; i<numgrades; i++){ /*if(grades[i] == grades[h]) continue;*/ if(grades[i] > grades[h]) h = i; } return h; } int minimum(double grades[], int numgrades){ int i, h = 0; for(i=1; i<numgrades; i++){ /*if(grades[i] == grades[h]) continue;*/ if(grades[i] < grades[h]) h = i; } return h; } int deleteelement(double grades[], int numgrades, int loc){ int i; for(i=loc; i<numgrades-1; i++){ grades[i] = grades[i+1]; } return numgrades-=1; } double mean(double grades[], int numgrades){ int i; double ans, sum; for(i = 0; i<numgrades; i++){ sum += grades[i]; } ans = sum/numgrades; return ans; } double standarddeviation(double grades[], int numgrades){ int i; double avg, sd, ans; avg = mean(grades, numgrades); for(i=0; i<numgrades; i++){ sd += pow((avg - grades[i]), 2); } ans = sqrt(sd/numgrades); return ans; } i appreciate helping me this.
check result of fopen , fscanf calls:
gd = fopen( "sample.txt", "r" ); if ( !gd ) { fprintf( stderr, "error opening input file\n" ); return 0; } while( count < max && fscanf( gd, "%lf", &ind ) == 1 ) { if ( ind < 0 ) break; grades[count++] = ind; } if ( feof( gd )) fprintf( stderr, "hit end of file\n" ); else if ( ferror( gd )) fprintf( stderr, "error during read\n" );
Comments
Post a Comment