/* * Program 8.8 Grade.C * Purpose: Grade Assignment using Normal T Score. * Author : Wachara Rodsumrid. * Office : Dept of Physics. Rajamangala Institute of Technology. * Language: Turbo C v.2.0 * Date : OCT 1992. (in BASIC version) * Last update : 26 May 1996. * What 's new : 1. Data could be real numbers. * 2. More accuracy in calculating area under normal curve. 3. Decrease global variables, more structure programming. 4. Show output on screen before printing. **************************************************************/ #include
#include
#include
#include
#include
#include
/* maximum number of students = 1000 */ #define MAX_STUDENT 1000 /* Global variable */ char *str_time; char *filename; /* function declare */ int get_data_from_file(char *filename,double dat[],int* err_line); double average(int, double item[]); void quick_sort(double item[], int ,int ,int ); int find_frequency(int,double item[], int Frequency[],double MScore[]); void find_cumulate_frequency(int, int, int Frequency[],\ int cf[],double ZArea[]); void evaluate_Tscore( int,double item[],double Tresult[]); double Find_z(double); void assign_grade(int,double TScore[],char *grade[]); void output_to_printer(int,double ManagedScore[],\ int Frequency[],int cf[], double TScore[],double ZArea[],\ double mean, double sd,char* grade[]); int printer_ready(void); void output_to_screen(int,double ManagedScore[],int Frequency[] \ ,int cf[], double TScore[],double ZArea[], double mean,\ double sd,char *grade[]); void main(int argc, char *argv[]) { double RawScore[MAX_STUDENT],ManagedScore[MAX_STUDENT]; int Frequency[MAX_STUDENT],cf[MAX_STUDENT]; double TScore[MAX_STUDENT]; double ZArea[MAX_STUDENT]; char *grade[MAX_STUDENT]; long time_now; double mean,sd; int n, nf; int err_line,error; char c = 'S'; time(&time_now); str_time = ctime(&time_now); if (argc < 2) { puts ("Input your data filename"); puts (" grade [drive:][path] filename\n"); exit(1); } filename = argv[1]; puts("Normalized T-Score Calculation Version 1.6"); puts(" Dept of Physics."); puts ("Last update 31 MAY 1996"); puts("Rajamangala Institute of Technology."); printf("\nNow I am reading your file, %s\n",argv[1]); n = get_data_from_file(filename,RawScore,&err_line); if ( n == -1) { printf( "Detect some mistake(s) in your Data file,'%s'\n",argv[1]); printf( "Found error(s) in line %d\n",err_line); exit(1); } if (n == -2 ) { printf("Cannot open %s\n",argv[1]); exit(1); } puts("Find average and standard deviation....."); error = mean_deviation(n,RawScore,&mean,&sd); if (error == -1) { printf("The number of data = 0 \n"); exit(1); } puts("Sorting..."); quick_sort(RawScore,0,n-1,0); puts("Find cumulated frequency ..."); nf = find_frequency(n,RawScore,Frequency,ManagedScore); find_cumulate_frequency(nf,n,Frequency,cf,ZArea); puts("Calculating T-score..."); evaluate_Tscore(nf,ZArea,TScore); puts("Assigning grade..."); assign_grade(nf,TScore,grade); while ( c != 'Q') { printf("\nOutput to (S)creen, (P)rinter or (Q)uit ?"); c = toupper(getche()); if ( c == 'P') { puts("\nand printing...."); output_to_printer(nf,ManagedScore,Frequency,cf,\ TScore,ZArea,mean,sd,grade); } if ( c == 'S') { clrscr(); output_to_screen(nf,ManagedScore,Frequency,cf,\ TScore,ZArea,mean,sd,grade); } if ( c== 'Q') { puts("\n\nThank for using this Software..from Wachara .R.\n"); } } /* while */ } int get_data_from_file(char *filename,double dat[],int* err_line) { /* Function Description : get data (real number) from specified file. User's define Constant: none Input : *filename -- pointer which point to filename. data[] -- an array which real numbers are stored in. The first number begin at item[0] (not item[1]) and the last number is in item[n-1]. *err_line -- pointer to line number that error happened. Calling Function : none Return Value : the number of data -- if everything's ok. -1 -- there is some error in data. -2 -- cannot open file. */ FILE *fp; int line = 1; char ch,temp[20]= ""; int point=0, n=0,i=0, IN_DIGIT =0, IN_COMMENT = 0; int ERR_FLAG = 0; double t; if ((fp = fopen( filename,"r") ) == NULL) { /* printf("Cannot open %s\n",*filename); */ return(-2); } while ( (ch = fgetc(fp)) != EOF && !ERR_FLAG) { if (ch == '*' ){ IN_COMMENT = 1; } if (ch == '\n') { line++; IN_COMMENT = 0; } if (isalpha(ch) && !IN_COMMENT) { ERR_FLAG = 1; *err_line = line; } if (isdigit(ch)|| ch == '.' && !IN_COMMENT) { IN_DIGIT = 1; if ( ch == '.') point = point+1; temp[i] = ch; i = i +1; if (point > 1 ) { ERR_FLAG =1; *err_line = line; } } if (isspace(ch) && IN_DIGIT && !IN_COMMENT) { IN_DIGIT = 0; point = 0; i = 0; dat[n] = atof(temp); n++; } } fclose(fp); if (ERR_FLAG) return -1 ;else return(n); } int find_frequency(int number,double item[],\ int Frequency[],double Manageitem[]) /* Function Description : Collect frequency of each value. User's difine Constant: none Input : number -- number of data. item[] an array which numbers are stored in. The first number begin at item[0] (not item[1]) and the last number is in item[n-1]. Frequency[] an array that keeps the frequecy of each value of data. Manageditem[] an array which classified data are kept. Calling Function : none Output : Frequency of each value stored in Frequency[] Return Value : number of classified data. Remark : Lookout ! The data in item[] must be sorted before using this function */ { int n_freq = 0; int i,j; for (i = 0; i < number ; i++) { if (item[i] == -1 ) continue; Frequency[n_freq] = 1; Manageitem[n_freq] = item[i]; for (j = i ; item[j+1] == item[i]; j++) { Frequency[n_freq] +=1; item[j+1] = -1; } n_freq += 1; } return(n_freq); } int mean_deviation(int n_item, double item[],double *average,double* deviation) { /* Function Description : Calculate arithmatic mean and standard deviation. User's difine Constant: none Input : n_itemd -- number of data. item[] an array which numbers are stored in. The first number begin at item[0] (not item[1]) and the last number is in item[n-1]. *average -- mean of data. *deviation -- standard deviation. Calling Function : none Output : mean and standard deviation are stored in 'mean' and 'deviation'. Return Value : 0 -- No error. 1 -- number of data = 0 */ int i; double total; if (n_item == 0 ) return 1; total = 0; for ( i =0; i< n_item; i++) total = total+ item[i]; *average = total/ n_item; total = 0; for (i = 0; i < n_item; i++ ) total = total + (item[i] - *average)*(item[i]-*average); *deviation = sqrt(total/n_item); return(0); } void quick_sort(double item[], int left,int right,int ascending) { int i,j; double comparand,temp; i = left; j = right; comparand = item[(left+right)/2]; do { if (ascending) { while(item[i] < comparand && i < right) i++; while (comparand < item[j] && j > left) j--; } else { while(item[i] > comparand && i < right) i++; while (comparand > item[j] && j > left) j--; } if ( i <= j) { temp = item[i]; item[i] = item[j]; item[j] = temp; i++; j--; } } while (i <=j); if (left
< right) quick_sort(item,i,right,ascending); } void find_cumulate_frequency(int n_freq, int n_item,\ int Frequency[], int cf[],double ZArea[]) { int i; for ( i = 0; i <= n_freq;i++) { cf[i] = 0; ZArea[i] = 0; } Frequency[n_freq + 1] =0; for (i = n_freq-1; i >= 0; i--) { cf[i] = cf[i+1] +Frequency[i]; ZArea[i]=(cf[i+1]+0.5*Frequency[i])/n_item; } } double Find_z( double area) { /* Function Description: Finding z value at known area under normal curve. User's Define Variable : None Input : area -- area under normal cuvre Return : statistic z value. -99 -- if area >1 or < 0 Remark : The area should be between 0 to 1. *************************************************************************/ int i,flag,lowerlimit; double new_area,da,z,sum; if ( area < 0 || area > 1 ) return -99; flag = 0; if (area > 0.5) { new_area = area - 0.5; flag = 1; } else { new_area = 0.5 - area; } sum = 0.0; lowerlimit = 0; if (new_area >= 0.3412944) { lowerlimit = 1; sum = 0.3412944; } if (new_area >= 0.4771599) { lowerlimit = 2; sum = 0.4771599; } if (new_area >= 0.4986253) { lowerlimit = 3; sum = 0.4986253; } /* Integrate by Trapezoidal Rule */ z = lowerlimit; while (new_area - sum > 0.001) { da = (exp(-0.5*z*z)+exp(-0.5*(z+0.01)*(z+0.01)))*1.994712e-03; sum = sum + da; z += 0.01; } if (flag ==1) return(z); else return(-z); } void evaluate_Tscore( int n_freq,double item[],double TScore[]) { int i; double temp_z; for(i = 0 ; i < n_freq;i++) { temp_z = Find_z(item[i]); TScore[i] = (temp_z*10.0 + 50.0); } } void assign_grade(int n_freq,double TScore[],char *grade[]) { int i; double range; range = (TScore[0] - TScore[n_freq-1])/5.0 ; if (range < 0 ) range = -range; for (i = 0; i < n_freq; i++) { grade[i] = "F"; if (TScore[i] > (50.0 - 1.5*range)) grade[i] = "D"; if (TScore[i] > (50.0 - 0.5*range)) grade[i] = "C"; if (TScore[i] > (50.0 + 0.5*range)) grade[i] = "B"; if (TScore[i] > (50.0 + 1.5*range)) grade[i] = "A"; } } void output_to_printer(int n_freq,double ManagedScore[],int Frequency[]\ ,int cf[], double TScore[],double ZArea[],double mean,double sd,char* grade[]) { int i; if (printer_ready()) { fprintf(stdprn,"\t\t NORMALIZED T-SCORE CALCULATION. \n"); fprintf(stdprn,"\t\tDept of Physics., Rajamangala Institute of Technology.\n"); fprintf(stdprn," Data from file : %s \tDate of printing: %s \n\n",filename,str_time); fprintf(stdprn," Average Score = %6.2f. Std deviation = %f.2f\n",mean,sd); fprintf(stdprn,"Score Freq Cumulate Freq Percentile Normal-T \ Grade\n"); fprintf(stdprn,"==============================================================\ ========\n"); for(i=0; i < n_freq; i++ ) { fprintf(stdprn,"%6.3f %3i %3i %5.2f %5.2f %s\n",ManagedScore[i],\ Frequency[i],cf[i],ZArea[i]*100.0,TScore[i],grade[i]); } fprintf(stdprn,"\n\n\t\t---------------------------------------\n"); } else { puts( "\n\n printer not ready !!!!!!\n"); } } int printer_ready() { int p1; asm { mov ax,40h mov es,ax mov dx,es:[8] inc dx in al,dx mov bl,1 /* TRUE */ test al,1000b /* check out any printer ready */ jnz p1 /* jump if bit set */ mov bl,0 } p1: asm { xor ax,ax mov al,bl } return (_AX); } void output_to_screen(int n_freq,double ManagedScore[],int Frequency[] \ ,int cf[], double TScore[],double ZArea[], double mean,\ double sd,char *grade[]) { int i; fprintf(stdout,"\t\t NORMALIZED T-SCORE CALCULATION. \n"); fprintf(stdout,"\t\tDept of Physics., Rajamangala Institute of Technology.\n"); fprintf(stdout," Data from file : %s Date of printing: %s \n",filename,str_time); fprintf(stdout," Average Score = %6.3f. Std deviation = %6.3f\n",mean,sd); fprintf(stdout,"Score Freq Cumulate Freq Percentile Normal-T \ Grade\n"); fprintf(stdout,"==============================================================\ ========\n"); for(i=0; i < n_freq; i++ ) { fprintf(stdout,"%6.3f %3i %3i %5.2f %5.2f %s\n",ManagedScore[i],\ Frequency[i],cf[i],ZArea[i]*100.0,TScore[i],grade[i]); if (wherey() == 24) { printf("Press any key to continue ...");getch(); clrscr(); } }/* for loop */ fprintf(stdout,"\n\t\t---------------------------------------\n"); }
จบเพียงแค่นี้ก่อนครับ
ขอบคุณที่สนใจอ่านจนถึงจุดนี้