Programming Tutorials

(追記) (追記ここまで)

Printing a simple histogram in C

By: Ignatius in C Tutorials on 2012年03月16日 [フレーム]

This simple C program prints a simple histogram.

#include <stdio.h>
#define SIZE 10
 
int main()
{
 // declare and initialize an array named n with size SIZE...
 int n[SIZE] = {19, 3, 15, 7, 11, 9, 13, 5, 17, 1};
 int i, j;
 // display the table header...
 printf("%s%13s%17s\n","Element/index", "Value", "Histogram");
 // do the iteration...
 // the outer for loop, read row by row...
 for(i=0; i <= (SIZE-1); i++)
 {
 printf("%9d%15d ", i, n[i]);
 // the inner for loop, for every row, read column by column and print the bar...
 for(j = 1; j<= n[i]; j++)
 // print the asterisk bar...repeat...
 printf("*");
 // go to new line for new row...repeats...
 printf("\n");
 }
 return 0;
}




(追記) (追記ここまで)


Add Comment

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!
(追記) (追記ここまで)
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /