C language Print horizontal histogram or vertical histogram

Source: Internet
Author: User

In the C programming language (version 2nd), there is an exercise that prints a horizontal or vertical histogram of the number of words, see page 17th. Now suppose we've got a count array of words.

The horizontal histogram is simple to print, only two loops are required, and the number of times the outer loop executes is the array length. The inner loop is the size of each element of the array. Here's how:

1/* 2 * Print Horizontal direction Histogram 3 * 4 */5 void horizontal (int array[],int len) {6   7   printf ("\t↑\n"); 8   printf ("\t| \ n "); 9   printf ("\t|\n"), ten   int i;11 for   (i=0;i<10;i++) {int k=0, printf ("\t|") ); (; k<array[i];k++) {printf ("*" }17 printf ("(%d)", Array[i]); printf ("\n\t|\n"  }20 printf ("\ t--------------------------------------------------->\n"); printf ("\ T-----------------------------------horizontal histogram-------------------------------------\ n ");  

      Printing a histogram in the vertical direction is a bit cumbersome, but it only requires a two-layer loop, first we need to find the largest element in the array, because that data determines the maximum height of the vertical statistic histogram, that is, the number of outer loops (here we have the largest data plus one, Because we want to show the data above the fat chart, the number of inner loops is the number of elements, as opposed to the horizontal histogram cycle. Inner loop each time the array with the data in the outer comparison, if equal, print the symbol at the same time the current data automatically minus one, or the next time will print the symbol is printed continuously, otherwise print empty, find out the largest data in the array is relatively simple. Here's how:

1/* 2 * Print Vertical direction histogram 3 * 4 */5 void vertical (int array[],int len) {6   int maxvalue=max (array,len);     7   int J; 8   int i;   9   printf ("\t↑\n"); ten for   (i=maxvalue+1;i>0;i--) {One-by-one printf ("\t|" ) (j=0;j<10;j++) {if (array[j]==i) {}else printf ("****\t"); Array[j]==i-1) {17//Display number printf ("(%d) \ t", Array[j]),}else{printf ("\ t" }23 printf ("\ n") }25 printf ("\ t--------------------------------------------------------------------------------------->\ ("\ t-----------------------------------vertical histogram-------------------------------------\ n \ n" ) );                 

The entire program is as follows

1 #include <stdio.h> 2/* 3 * Maximum number 4 * 5 */6 int max (int* array,intLen) {7 int maxvalue= 0; 8 int I=0; 9 for (i=0;i<len;i++) {Ten if (array[i]>MaxValue) {maxvalue=Array[i];12}13}14 returnMaxvalue;15}16/*17 * Print Horizontal directional histogram *19 */20 void horizontal (int array[],intLen) {("\t↑\n")); printf ("\t|\n")); printf ("\t|\n")); intI;26 for (i=0;i<10;i++) {k=0 int; printf ("\t|"); for (; k<array[i];k++) {printf ("*")); 31}32 printf ("(%d)", Array[i]); printf ("\n\t|\n")); 34}35 printf ("\ t--------------------------------------------------->\n"); printf ("\ t-----------------------------------horizontal histogram-------------------------------------\ n]); 37}38 39/*40 * Print vertical histogram *42 */43 void vertical (int array[],intLen) {maxvalue= intMax (Array,len); intj;46 intI printf ("\t↑\n"); for (i=maxvalue+1;i>0;i--) {"\t|") (j=0;j<10;j++ ) {array[j]==  i) {****\t ("array[j]--" ); 54} else if (array[j]==i-1 ) {55//Displays the number of printf ("(%d) \ t" , Array[j]);}else  ("\ t" ); 59 }60 }61 printf ("\ n" ); }63 printf ("\ T--------------------------------------------------------------------------------------->\n "); 64 printf ("\ t-----------------------------------vertical histogram-------------------------------------\ n" );  }66  Main () {WORDS[10 int. ];68 int temp[10 ];69 int i=0 ; (; i<10;i++ ) {words[i]= ( I-4) * (i-4) +1 ; }73 int Maxvalue=max (words,10 ), and the printf ("maxvalue=%d\n" , MaxValue); 75 Horizontal (words,10 ), Vertical (words,10 ), and the         

Program run:

The picture is too large, divided into two interception

C language Print horizontal histogram or vertical histogram

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.