• Around The HomeToggle Menu

    • Entertainment
    • Productivity
    • Smart Home
  • FamilyToggle Menu

    • Parenting
    • Toys
    • Pets
    • Travel
  • Product ReviewsToggle Menu

    • Phones
    • Tablets
    • Laptops
    • Desktops
    • Wearables
    • Audio
    • Cameras
    • Headphones
    • Printers
    • Smart Home
    • TVs
    • Gaming and Video
  • One Cool ThingToggle Menu

    • Frugal Tech
    • Kickstarters
    • Videos
Techwalla
  1. Home
  2. Around The Home
  3. Productivity
  4. How to Create a Histogram Using C Programming Code

How to Create a Histogram Using C Programming Code

March 31, 2015
By: Deborah Lee Soltesz
  • Share
  • Share on Facebook

Histograms are commonly found as a chart option in analyzing data in spreadsheet software, and in image editing software for showing the distribution of tones from black to white in an image. In the C programming language, using an array to hold the frequency count simplifies creating a histogram of your data set. While data sets used for creating histograms usually contain integer values, characters and even strings can be counted and graphed.

Video of the Day

Man making presentation to colleague
A histogram is a type of graph.
credit: Goodshoot/Goodshoot/Getty Images

Step

Populate an integer array called "values" with your data set, and set the "numvalues" variable to the number of values in your data set. These could be randomly generated, read in from a file, or interactively collected from the user. This example initializes these variables when they are declared:

int numvalues = 20; int values[numvalues] = { -3, 2, -2, 4, 5, 4, 2, 5, 4, 5, -1, 2, 3, 4, 7, 4, 2, 0, 7, -3 };

Step

Set up two integer variables (i and j) to use as iterators:

int i = 0, j = 0;

Step

Iterate through your data -- the values array -- and set the "maxval" variable to the maximum value of your data:

int maxval = 0; for (i=0; i maxval) maxval = values[i] }

Step

Step through your data and set the "minval" variable to the maximum value of your data:

int minval = maxval; for (i=0; i

Step

Declare a variable "freqsize" to hold the size of your frequency array:

int freqsize = maxval - minval + 1 ;

Step

Declare an array to hold the frequency counts and initialize each array element to zero:

int frequency[freqsize]; for (i=0; i

There is one array element for each possible value in your data set.

Step

Step through each value in your data set, adding one to the frequency array element corresponding to that value:

for (i = 0 ; i < numvalues ; i++) { int index = values[i] - minval; frequency[index]++ }

The index corresponding to the current value is generated by shifting the value by the minimum value.

Step

Step through each element in the frequency array. Print the current value (calculated by shifting the iterator "i" by the minimum value). Print the number of stars (*) corresponding to the frequency the current value by looping from one to the value stored in the frequency array, printing a single star each time:

for (i=1; i<=freqsize; i++) { printf("%2d\t|", i + minval); for(j=0; j

Show Comments

Related Articles

How Are Headphones Made?

How Are Headphones Made?

Around The Home
Entertainment
By: Contributing Writer
How to Use Excel for Correlation

How to Use Excel for Correlation

Around The Home
Productivity
By: Shane Hall
How to Split a String Into Two Variables in PowerShell

How to Split a String Into Two Variables in PowerShell

Around The Home
Productivity
By: Gareth Downes-Powell
How to Fix Syntax Errors

How to Fix Syntax Errors

Around The Home
Productivity
By: Dan Stone
How to Create a Quiz With Random Questions Using Java

How to Create a Quiz With Random Questions Using Java

Around The Home
Productivity
By: Micah McDunnigan
How to Make a Product Catalog in PHP

How to Make a Product Catalog in PHP

Around The Home
Productivity
By: Max Power
  • HOW WE SCORE
  • ABOUT US
  • CONTACT US
  • TERMS
  • PRIVACY POLICY
  • COPYRIGHT POLICY
  • Advertise

An error occurred. Try again later.

Thanks for signing up!
© 2018 Leaf Group Ltd. Leaf Group Media

Get great tech advice delivered to your inbox.

Keep your family productive, connected, entertained, and safe.

Please enter a valid email.