Assignment: Character count with Vertical Histogram Display. extra for GENERALIZ
ID: 3832021 • Letter: A
Question
Assignment: Character count with Vertical Histogram Display. extra for GENERALIZED vertical number routine. (Not tied to a given range of values.) Read in a stream of text UNTIL YOU ENCOUNTER END OF FILE (EOF). Read one character at a time. Keep a count of how many of each letter you see in an array of 26 ints called: int count[26] ; Example run: a.out < histo.data Counts: count[ a]: 320 count[ b]: 160 count[ c]: 80 count[ d]: 40 count[ e]: 20 count[ f]: 10 count[ g]: 5 count[ h]: 2 count[ i]: 1 count[ j]: 2 count[ k]: 17 count[ l]: 41 count[ m]: 67 count[ n]: 97 count[ o]: 127 count[ p]: 157 count[ q]: 191 count[ r]: 227 count[ s]: 257 count[ t]: 283 count[ u]: 331 count[ v]: 367 count[ w]: 373 count[ x]: 379 count[ y]: 383 count[ z]: 389 Graph: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * a b c d e f g h i j k l m n o p q r s t u v w x y z ==================================== How to design the program: Use the instructions in the previous Horizontal Histogram Assignment we just completed. Do not deviate from it regarding input and storage of counts. 0. Decide how many lines you want to use for your display, call it LINES. Number the lines highest to lowest, top to bottom of the screen. 1. Find the biggest number in the array, call it MAX 2. For each LINE on your display, calculate a minimum threshold value required for an element of count[] to get a '*' insread of a ' '. Call it THRESHOLD. LINE THRESHOLD ---- = ---------- LINES MAX So: THRESHOLD = (MAX * LINE) / LINES ; This also should be obvious to you, since it's 6th grade arithmetic. If you don't understand it easily and completely, you need to seek remediation. The screen will be processed by a for() loop which runs from LINES to 1, (a descending for() loop.) Run a for() loop which steps through the count[] array, and decides to print either a '*' or ' ' for each column by comparing count[LINE] with THRESHOLD. (Each column represents a letter in count[]). -------------------------- Discussion: For each line in, say, a 60-line screen, you start with the top line, (line 60). To "get a star" on the TOP LINE, the number in the count[] element must be EQUAL TO MAX, that is, the top line represents the largest count we've seen in the input. The next line down represents 59/60 ths of a 60 line screen. The bottom line represents 1/60 th of a 60 line screen. So for each line, you check each element in count, and if it holds that fraction of MAX or more, it gets a star, otherwise it gets a space. (Once a column gets a star, it will get stars for all the lines below that one, obviously.) Obviously, the "outer" loop will be a downward-counting loop, something like: for (line = LINES ; line > 0 ; line--) ====================================== Professional Note: Vertical numbers. To print out the numeric totals vertically beneath the columns they correspond to, come up with a general purpose function for formatting numbers of arbitrary size vertically using several lines. a.out < histo.data * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ---------------------------------------------------- a b c d e f g h i j k l m n o p q r s t u v w x y z 3 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 2 2 2 3 3 3 3 3 3 2 6 8 4 2 1 0 0 0 0 1 4 6 9 2 5 9 2 5 8 3 6 7 7 8 8 0 0 0 0 0 0 5 2 1 2 7 1 7 7 7 7 1 7 7 3 1 7 3 9 3 9 a.out < h.dat * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ---------------------------------------------------- a b c d e f g h i j k l m n o p q r s t u v w x y z 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 8 9 4 2 1 0 0 0 0 0 0 2 3 5 7 8 0 2 4 5 8 0 1 1 1 1 0 0 5 2 1 5 2 1 0 1 9 3 7 4 1 8 7 8 4 9 6 6 0 3 6 9 4 2 1 5 2 6 8 1 5 1 5 1 7 7 6 5 7 0 9 6 6 9 3 7 0 3 8 4 2 6 8 4 2 2 6 2 8 2 8 0 2 4 2 2 4 1 8 8 7 5 1 9 0 0 0 0 0 0 0 8 4 8 8 4 8 8 8 8 4 8 8 2 4 8 2 6 2 6 UNIX NOTE: If you're writing this so it senses the size, in columns and rows, of the user's terminal, you may need to explicitly do this at the command line: $ export LINES $ export COLUMNS If you encounter "segfault" errors, this may be the problem. (I set your .bashrc files to do this, it ought to be OK.)
Explanation / Answer
I have taken the max no of lines to be 10. You can change that in the program.
code:
#include <stdio.h>
int main()
{
int count[26];
int i,max=0,j;
for(i=0;i<26;i++)
count[i]=0;
FILE *f;
char c;
f=fopen("test.txt","rt");
while((c=fgetc(f))!=EOF){
//printf("%c",c);
count[c-97]++;
}
fclose(f);
for(i=0;i<26;i++){
if(count[i]>max)
max=count[i];
}
int lines=10;
int threshold=max/lines;
if(threshold==0)
threshold=1;
int buff[26];
for(i=0;i<26;i++)
buff[i]=count[i]/threshold;
/*
for(i=0;i<26;i++)
{
for(j=0;j<count[i]/threshold;j++){
printf("*");
}
printf(" ");
}
*/
printf(" ========================================= ");
for(i=0;i<lines;i++){
for(j=0;j<26;j++){
//printf("s=%d i=%d",buff[j],i);
if(buff[j]>=lines-i)
printf("*");
else
printf(" ");
}
printf(" ");
}
for(i=0;i<26;i++)
printf("%c",i+97);
printf(" ========================================= ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.