The following program in C draws a Vertical , frequency distribution , histogram for the various characters as present in the input text .
CAUTION : As i have declared variables within the beginning for loop parentheses , hence , Please compile the following program in C99 mode.If you are on Linux/Unix then you may compile it in C99 mode by typin “ -std=gnu99” or “ -std=c99 “.
Program follows :
Sample Output
CAUTION : As i have declared variables within the beginning for loop parentheses , hence , Please compile the following program in C99 mode.If you are on Linux/Unix then you may compile it in C99 mode by typin “ -std=gnu99” or “ -std=c99 “.
Program follows :
/*Program in C to print a vertical histogram of the frequencies of different characters in its input*/ #include<stdio.h> int main(){ int i,j,t,q,a,flag; char text[1000]; char collector[128]; for(i=0;i<=127;++i){collector[i]=0;} printf("Enter the text hither : \n"); scanf("%[^\n]",text); i=0; while(text[i]!='\0'){ j=text[i]; collector[j]=collector[j]+1; ++i; } t=0; for(int q=0;q<=127;++q){if(collector[q]>0){++t;}} char cart[t]; char ascii[t]; i=0; q=0; for(;q<127;++q){if(collector[q]>0){ cart[i]=collector[q]; ascii[i]=q; ++i; } } printf("\n\nThe frequency distribution Histogram of characters :\n"); printf("\nfrequency\n"); for(int y=t+5;y>=1;--y){ if(y%2==0){ if(y>8){ printf(" %d|",y); } else{ printf(" %d |",y); } for(int r=0;r<t;++r){ if(cart[r]==y){ if(r==0){ printf("^"); }else{ printf(" ^"); } cart[r]=cart[r]-1; }else{ printf(" "); } }printf("\n"); } else{ printf(" |"); flag=0; for(int r=0;r<t;++r){ if(cart[r]==y){ if(r==0){ printf("^"); }else{ printf(" ^"); } cart[r]=cart[r]-1; }else{ printf(" "); } } printf("\n"); } } printf(" -------------------------------------------\n"); printf(" "); for(int a=0;a<t;++a){ printf(" %c",ascii[a]); } printf("\n"); return 0; }
Sample Output
No comments:
Post a Comment