tags:

views:

141

answers:

3

how do I plot a histogram in c from 2 arrays?

A: 

You can use ascii art for that

mamamia
And the ncurses library.
Raveline
+1  A: 

For a histogram layed out on its side...

I suggest using printf("*") for each increment, and printf("\n") to start outputting a new row. (Changing the orientation is an excercise to the reader).

Arafangion
+1  A: 

Thinking about the problem a bit I'm not convinced that the "duplicate" I identified in the comments is really responsive. So I'll say a few words.

If you've settled on a ASCII art approach, then you have only one more decision to make: vertical or horizontal bars. Horizontal is easy: just decide on the scaling and then print bin_contents*scale symbols for each bin. The code-golf link really is useful as a model of what to do, even if not a good example of how to write it.

However, many fields have an expectation of vertical bar in the presentation of histograms. That's a little harder, but consider the pseudocode

sacle = find_scale(input_array)
max_height = find_max(input_array) * scale
for (i=max_height; i>=0; i--) 
   if (some condition)
      print_in_N_digits(round(i/scale)) // to label the scale
   else
      print_in_N_digits()               // lines with no labels
   print " |"                           // set up the vertical axis
   for (j=first_bin to lat_bin)
      if (input[j]*scale >= i)
         print("#")
      else
         print(" ")
      print_new_line
print_in_N_digits(0)
print(" +")
for (j=first_bin to last_bin)
   print("-")
print_new_line
print_in_N_digits()
print(" 0")
for (j=first_bin to last_bin)
   if (some other condition)
      print_bin_label

This just walks across the page, using on column per bin and at each level prints either " " or "#" for each column. The histogram printing part is really very easy. All the complexity arises from managing the axis and labels.

dmckee