Histogram function

This is a discussion on Histogram function within the C Programming forums, part of the General Programming Boards category; Hello all. I need assistance with making/printing the histogram function in my code. I have never done this before and ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    36

    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 */

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    Can someone please help me?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    I believe it has to be written as a function prototype.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    My question is: How do I write the print histogram function and where would I write this into the code?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I just showed you how to write it. What part can't you figure out?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    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'
    Last edited by davidjg; 11-23-2010 at 07:41 PM.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    36

    Post

    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("*");

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    I did. Please see the above in red.

  11. #11
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    Perhaps davidjg is in need of a pseudocode compiler
    Anchor | freshmeat.net

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    Maybe so. I am new to C programming. Can I get the exact syntax on how to write the histogram code?

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Good job showing up and doing it for him.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by quzah View Post
    Good job showing up and doing it for him.
    Quzah.
    That's not his program, it's one from a previous thread on the forum. He's posted code. This just saves him from searching for "histogram" in our forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 08:42 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21