Hi all... First post here, be gentle. I'm trying to get this to work, and there's no output. This is from the K&R book [v2] exercise 1-14, the prompt is in the header. I added comments for readability, and please comment on anything you believe needs improvement. Thank you for reading.
/*
* EX114.c
* this will create a histogram of the frequency of different characters
* in its input.
*
* what I'm trying to do: 2 arrays, 1 with the character and 1 with the #
* of time that character shows up...
*/
#include <stdio.h>
#define CHARCOUNT 32
// for the sake of simplicity, CHARCOUNT represents the # of unique characters
main(){
int c, nc, chcount[CHARCOUNT], lcv;
// chcount is the # of times a unique character shows up...
// so eg if I had 'a' show up 5 times then
// 'a' would be in charray[4], and chcount[4] would show 5.
// nc is the unique character count
nc = 0;
char charray[CHARCOUNT];
while ((c = getchar()) != EOF){
// so this loop checks to see if the current character is in the array
int inarray = 0;
for(lcv = 0; lcv < CHARCOUNT && inarray == 0; lcv++){
if(c == charray[lcv]){
chcount[lcv] = chcount[lcv]++;
inarray++;
}
// if the character is in the array then we increment the count of that character.
// we also increment inarray so the loop quits.
}
// so if this test passes, that means the character was not in the array.
if(lcv != CHARCOUNT){
// add the current char to the array.
charray[nc] = c;
chcount[nc] = 1;
// increment the # of unique characters.
nc++;
}
}
// this part is for drawing the histogram.
for(lcv = 0; lcv < nc; lcv++){
putchar(charray[lcv]);
int i = chcount[lcv];
while(i > 0){
printf("*");
i--;
}
printf("\n");
}
}
Now looking at this code I realize the part for drawing the histogram should probably be in the loop...