Hello all. I need assistance with making/printing the histogram function in my code. I have never done this before and I'm very new to C programming. Any ideas on how to do this? All advice is welcome and appreciated!
Code:#include <stdio.h> #define NUMBERofSTUDENTS 3 #define EXAMSCORES 4 /* function prototypes */ void median ( int grades[] [ EXAMSCORES ] ); int maximum ( int grades[] [ EXAMSCORES ], int pupils, int tests ); double average ( int setOfgrades[], int tests ); void printArray( int grades[] [ EXAMSCORES ], int pupils, int tests); extern void bubbleSort(int[][], int); extern void histogram(int[][], int); int main() { /* begin main */ int student; /* student counter */ int iS, iE; /* variables declared by Jeff H. */ /* initialize student grades for the three students (rows) */ int studentGrades[ NUMBERofSTUDENTS ][ EXAMSCORES ]; for (iS = 0; iS < NUMBERofSTUDENTS; iS++) { /* start outer for loop */ for (iE = 0; iE < EXAMSCORES; iE++) { /* start inner for loop */ scanf("%d", &studentGrades[ iS ] [ iE ] ); } /* end inner for loop */ } /* end outer for loop */ /* output array studentGrades */ printf( "\nThe array is:\n" ); printArray( studentGrades, NUMBERofSTUDENTS, EXAMSCORES ); median( studentGrades ); /* determine smallest and largest grade values */ printf( "\n\nHighest grade: %d\nLowest grade: %d\n", maximum( studentGrades, NUMBERofSTUDENTS, EXAMSCORES ) ); /* calulate average grade for exach student */ for (student = 0; student < NUMBERofSTUDENTS; student++ ) { /* start for loop */ printf( "The avareage grade for student %d is %.2f\n", student, average( studentGrades[ student ], EXAMSCORES ) ); } /* end for loop */ return 0; /* indicates successfull termination */ } /* end main */ /* Find the median grade */ void median( int grades[][ EXAMSCORES ] ) { /* begin function median */ int i; /* student counter */ int j; /* exam counter */ int buf[NUMBERofSTUDENTS]; /* array buffer */ for (j = 0; j < EXAMSCORES; j++) { /* loops through examscores */ for ( i = 0; i < NUMBERofSTUDENTS; i++ ) { /* loads buf with 1 exam */ buf[ i ] = grades[ i ][ j ]; } bubbleSort (&buf, NUMBERofSTUDENTS); printf("Median for exam %d is %d\n", j, buf[(NUMBERofSTUDENTS-1)/2] ); /* best for odd num of students*/ } /* end outer for loop */ } /* end function median */ /* Find the maximum grade */ int maximum( int grades[][ EXAMSCORES ], int pupils, int tests ) { /* begin function maximum */ int i; /* student counter */ int j; /* exam counter */ int highGrade = 0; /* initialize to lowest possible grade */ /* loop through the rows of grades */ for ( i = 0; i < pupils; i++ ) { /* start outer for loop */ /* loop through the columns of grades */ for ( j = 0; j < tests; j++ ) { /* start inner for loop */ if ( grades[ i ] [ j ] > highGrade ) { /* start if */ highGrade = grades[ i ][ j ]; } /* end if */ } /* end inner for loop */ } /* end outer for loop */ return highGrade; /* return maximum grade */ } /* end function maximum */ /* Determine the average grade for a particular student */ double average( int setOfGrades[], int tests ) { /* begin function average */ int i; /* exam counter */ int total = 0; /* sum of test grades */ /* total all grades for one student */ for ( i = 0; i < tests; i++ ) { /* start for loop */ total += setOfGrades[ i ]; } /* end for loop */ return ( double ) total / tests; /* average */ } /* end function average */ /* Print the array */ void printArray( int grades[][ EXAMSCORES ], int pupils, int tests ) { /* begin function print array */ int i; /* student counter */ int j; /* exam counter */ /* output column heads */ printf( " [0] [1] [2] [3]" ); /* output grades in tabular format */ for ( i = 0; i < pupils; i++ ) { /* start outer for loop */ /* output label for row */ printf( "\nstudentGrades[%d] ", i); /* output grades for one student */ for ( j = 0; j < tests; j++ ) { /* start inner for loop */ printf( "%-5d", grades[ i ][ j ] ); } /* end inner for loop */ } /* end outer for loop */ printf("\n\n"); } /* end funtion print array */



LinkBack URL
About LinkBacks




