Download the program histogramc and examine it This program

Download the program histogram.c and examine it. This program is partially complete. Your job is to finish the program so that it prints a histogram of the values entered into an array. A histogram counts the number of times a specific value occurs in an array and stores the counted value in a new array at the location of the number that was counted. After computing the histogram, the program prints the histogram to the screen using a horizontal bar chart and terminates. The following illustrates an example of an input array storing values between 0 and 9 and a calculated histogram with a bar chart illustration using * to represent a count of 1 for a recognized value.

Here is histogram.c :

#include <stdio.h>

/**
* Prints a histogram to the screen using horizontal bar chart.
* Parameters:
*   list - a list of integers
*   n - the number of values in the list
*/
void printHistogram ( int *hist, int n );

/**
* This program requests data from the user and then
* computes and prints a histogram. You may assume that
* the data values entered are in the range of 0 to 9.
*/
int main ( void )
{
   int i, n;

   // Data entry
   //
   printf (\"How many values in your data set? \");
   scanf (\"%d\", &n);

   int list[n];
   for (i=0; i < n; i++) {
       printf (\"Enter a value: \");
       scanf (\"%d\", &list[i]);
   }

   // Processing data to compute histogram
   //
   int hist[10];  


   // Printing histogram
   printHistogram ( hist, 10);

   return 0;
}

void printHistogram ( int *list, int n )
{
    int i, j;

    for (i=0; i < n; i++) {
       printf (\"[%d] \", i);
       for (j = 0; j < list[i]; j++)
           printf (\"*\");
       printf (\"\ \");
    }
}

Solution

#include <stdio.h>

/**
* Prints a histogram to the screen using horizontal bar chart.
* Parameters:
*   list - a list of integers
*   n - the number of values in the list
*/
void printHistogram ( int *hist, int n );

/**
* This program requests data from the user and then
* computes and prints a histogram. You may assume that
* the data values entered are in the range of 0 to 9.
*/
int main ( void )
{
    int i, n;

    // Data entry
    //
    printf (\"How many values in your data set? \");
    scanf (\"%d\", &n);

    int list[n];
    for (i=0; i < n; i++) {
        printf (\"Enter a value: \");
        scanf (\"%d\", &list[i]);
    }

  
    int hist[10]={0};
    for(int i=0;i<n;i++)
      {
   hist[list[i]]++;
      }


    // Printing histogram
    printHistogram ( hist, 10);

    return 0;
}

void printHistogram ( int *list, int n )
{
    int i, j;

    for (i=0; i < n; i++) {
        printf (\"[%d] \", i);
        for (j = 0; j < list[i]; j++)
            printf (\"*\");
        printf (\"\ \");
    }
}

Download the program histogram.c and examine it. This program is partially complete. Your job is to finish the program so that it prints a histogram of the valu
Download the program histogram.c and examine it. This program is partially complete. Your job is to finish the program so that it prints a histogram of the valu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site