Just your standard C primer program. I’m trying to print out a histogram of all the different characters that a user inputs as a string.
#include <stdio.h>
#define LIMIT 255
main(){
int asciiArray[LIMIT], input, outer, inner;
while((input = getchar()) != EOF){
asciiArray[input] = ++asciiArray[input];
//printf("%d\n", asciiArray[input]);
}
for(outer = 0; outer <= LIMIT; outer++){
if(asciiArray[outer] < 0){
putchar(outer);
printf("\t");
for(inner = asciiArray[outer]; inner > 1; inner--)
printf("*");
printf("\n");
}
else
;
}
}
These are the results when the user inputs “h”:
h0
2
9
?
@
C
G
J
K
U
Z
a
b
h
j
l
n
q
}
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
�
For starters, you are not initializing
asciiArray, thus it may contain everything.Try
memset(asciiArray, 0, sizeof(asciiArray));orint asciiArray[LIMIT] = {0}.Also, you can simplify that increment in the while to:
asciiArray[input]++.The
forlooks fishy too (especially how it accessesasciiArray[255]). What’s more, I don’t see howasciiArray[outer]can be less than0Perhaps you meant>?.