Histogram function

• 11-23-2010
davidjg
Histogram function
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 */```
• 11-23-2010
davidjg
• 11-23-2010
quzah
Code:

```for each student (column)     for each score (row)         if grade > fun math with dividing grade across possible range of rows             print symbol         else             print empty```
What part can't you figure out?

Quzah.
• 11-23-2010
davidjg
I believe it has to be written as a function prototype.
• 11-23-2010
davidjg
My question is: How do I write the print histogram function and where would I write this into the code?
• 11-23-2010
quzah
I just showed you how to write it. What part can't you figure out?

Quzah.
• 11-23-2010
davidjg
The code you have provided is returning parse errors.

Code:

``` 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 */ for each student (column)     for each score (row)         if grade > fun math with dividing grade across possible range of rows             print symbol         else             print empty 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 */```

gcc test.c histo.c bubble.c
test.c:69: parse error before `for'
test.c:84: parse error before `for'
• 11-23-2010
davidjg
I believe it has to be written as a function prototype and the array needs to be initialized in the loop. It should be something like:

Code:

```void PrintHistogram (int grades[]  [ EXAMSCORES ] ) int i; int j; for (i = 0; i < EXAMSCORES; i++) printf("*");```
• 11-23-2010
quzah
So do it. Did you write any of that code? Because if you know how to do all that, then why can't you do what I have shown?

Quzah.
• 11-23-2010
davidjg
I did. Please see the above in red.
• 11-23-2010
hellork
Perhaps davidjg is in need of a pseudocode compiler :D
Anchor | freshmeat.net
• 11-23-2010
davidjg
Maybe so. I am new to C programming. Can I get the exact syntax on how to write the histogram code?
• 11-23-2010
An easy way to do it is to make a char array[R][C] and populate it with exactly the char's you want to see in your histogram.

That will be one pair of nested for loops

Then in the second pair of nested for loops, with the rows beginning at the R value and cols beginning at the 0 value like normal, print out the array[r][c]. Rows decrement, but columns increment.

That way the highest rows, go at the top of the screen, matching the needs for the histogram.
Code:

``` #include<stdio.h> #define PI 3.14159 #define ROWS 10 #define COLS 30 void printGraph(void); int main(){         int min, inches, c=0, i, flength;     double totalRev=0, totalGas=0, rev, gas;     char file[20];     FILE *fp;     printGraph();     printf("\n\n\t\t\t    press enter when ready");     (void) getchar();     return 0; } void printGraph(void) {   int i,r,c,n, width=5, twidth=35, theight=10;   char dat[ROWS][COLS];   for(r=0,n=0;r<ROWS;r++) { //assigns the char values to the array     for(c=0;c<COLS;c++) {      //making up the histogram       dat[r][c]=' ';       ++n;       if(n-127<r)         dat[r][c]='*';     }   }   //basic print out block   for(r=ROWS-1,n=45;r>-1;r--) {  //rows decrement     printf(" %2d| ", n);     for(c=0;c<COLS;c++) {          //cols increment       if(c % width == 0 && c)         putchar(' ');       printf("%c",dat[r][c]);     }     putchar('\n');     n-=5;   }   printf("    -----------------------------------\n");   printf("    00-05 05-10 10-15 15-20 20-25 25-30\n"); } /* Should look like this, with his scaling 45 40 35 30 ***** 25 ***** ***** 20 ***** ***** 15 ***** ***** 10 ***** ***** ***** 5  ***** ***** ***** 0  ***** ***** *****     -----------------------------------     00-05 05-10 10-15 15-20 20-25 25-30 */```
This was altered from the interactive version, pardon the schizophrenic bits
• 11-23-2010
quzah
Good job showing up and doing it for him.

Quzah.
• 11-24-2010