Printing a simple histogram in C

By: Ignatius Emailed: 1706 times Printed: 2207 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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;
}


C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ignatius
Sorting an array of Strings in C++
Sorting an integer array in C
Printing a simple histogram in C
Sum of the elements of an array in C
Matrix using nested for loops in C++
Compute the square root of the sum of the squares of an array in C++
Calculate average using Two-Dimensional Array in C++
Two-Dimensional Array Manipulation in C++

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Find square and square root for a given number in C
goto and labels in C
Listing Files and Directories sample program in C
Using memset(), memcpy(), and memmove() in C
Using free() Function in C
Functions returning non-integer values in C
lseek() sample program in C
assert() Function Example program in C
Formatting with printf in C
Printing a simple histogram in C
Character Arrays in C
Type Conversions in C (String to Integer, isdigit() etc)
Precedence and Order of Evaluation in C
Using printf function in C
scanf and sscanf sample program in C
Most Emailed Articles (in C)
Word Counting sample program in C
Do while Loops in C
Bitwise Operators in C
Functions returning non-integer values in C
getch and ungetch in C
File Inclusion in C
Pointer Arrays and Pointers to Pointers in C
Multi-dimensional Arrays in C (Explained using date conversion program)
Using memset(), memcpy(), and memmove() in C
Using qsort() and bsearch() with strings - example program in C
Statements in C
Formatting with printf in C
while, do while and for loops in C
Infix to Prefix And Postfix in C
Printing a simple histogram in C