#define MAXLEN 10 /*Maximum Word length in Histogram*/
int main()
{
int word[MAXLEN + 1]; /*To increment values of words upto max length*/
int counter = 0;
int i, c, temp, max;
printf("Enter the text input for which you want to print a Histogram with word length frequencies!\nINPUT:\n");
for (i = 0; i < (MAXLEN + 1); ++i)
{
word[i] = 0;
}
while ( (c = getchar()) != EOF)
{
if (c == '\n' || c == '\t' || c == ' ')
{
temp = counter;
if (temp > MAXLEN)
{
++word[MAXLEN];
}
else
{
++word[temp - 1];
}
counter = 0;
}
else
{
++counter;
}
}
for (i = 0; i < (MAXLEN + 1); ++i)
{
if ( max < word[i])
{
max = word[i];
}
else
{
continue;
}
}
printf("\n");
while (max != 0)
{
printf("%4d| ", max);
for (i = 0; i < (MAXLEN + 1); ++i)
{
if (word[i] >= max)
{
printf("* ");
--word[i];
}
else
{
printf(" ");
}
}
printf("\n");
--max;
}
printf(" =");
for (i = 0; i < (MAXLEN + 1); ++i)
{
printf("====");
}
printf("\n");
printf(" ");
for (i = 0; i <(MAXLEN + 1); ++i)
{
if (i < 10)
{
printf("%4d", i+1);
}
else
{
printf("%4d<", 10);
}
}
printf("\n");\
return 0;
}```
This is a C program to Print a Vertical histogram for word frequencies of input text
#Pls give me suggestions on my C code
36 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
Can someone suggest me how to improve this code
I wrote this when I was absolute newb
int i, c, temp, max;
...
for (i = 0; i < (MAXLEN + 1); ++i)
just use
for(int i = 0; ...)
theres no reason to not do in inside the for scope
ohk
you only use the temp inside one while loop
hence you can also declare it there
int temp = counter;
max is not initialized
if ( max < word[i])
{
max = word[i];
}
here you check it before it gets assigned a value
while ((c = getchar()) != EOF) {
if (c == '\n' || c == '\t' || c == ' ') {
temp = counter;
if (temp > MAXLEN) {
++word[MAXLEN];
} else {
++word[temp - 1];
}
counter = 0;
} else {
++counter;
}
}
remove empty lines
and dont place opening brackets on a new line
it keeps it more concise and you have more code visible on your screen
but thats just my preference
I see
well that initialization of max fixed the issue of unnecessary lines being printed
thanks
Anything else?
for (i = 0; i < (MAXLEN + 1); ++i)
{
if ( max < word[i])
{
max = word[i];
}
else
{
continue;
}
}
the else case is redundant
for (i = 0; i < (MAXLEN + 1); ++i)
{
word[i] = 0;
}
can be replaced by
nt word[MAXLEN + 1] = {0};
could probably break some sections into a function, such as getting input. That way you also are fitting more code on your screen and its easier to understand yourself.
what is this ?
its basically memset
you should probably make more use of libraries and use strtok, etc
int arr[3] = {1, 2, 3}; // [0] = 1 [1] = 2 [2] = 3
int arr[3] = {1}; // [0] = 1 [1] = 0 [2] = 0
you will probably be dealing with strings often then so its good to learn to read the man pages for the standard library
@unborn jungle Has your question been resolved? If so, run !solved :)
!solved