Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7691933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:44:11+00:00 2026-05-31T20:44:11+00:00

So I’m making a program which is supposed to print a horizontal histogram of

  • 0

So I’m making a program which is supposed to print a horizontal histogram of the lengths of words in its input. So I changed the while into if so it now accepts more than 1 input. But what’s wrong with it this time? It won’t print the graph.

/*Write a program to print a histogram of the lengths of words in its input. 
It is easy to draw the histogram with the bars horizontal*/
#include <stdio.h>
#define MAX 30
#define IN 1
#define OUT 0
int main()
{
int a,c,i,k,state,word[MAX];
a=0;
k=0;
state=OUT;
for(i=0;i<MAX;i++)
    word[i]=0;
while((c=getchar())!=EOF)
{
    if(c==' '||c=='\t'||c=='\n')
        state=OUT;
    else
        state=IN;
    if(state==IN)
        a++;

    if(state==OUT)
        {
        word[i]=a;
        i++;
        a=0;
        }



}
/*This part is pissing me off, I don't know how to print X multiple times!*/
for(i=0;i<MAX;i++)
    if(word[i]>0)
        {
            for(k=0;k<=word[i];k++)
            putchar('-');
            putchar('\n');
        }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:44:12+00:00Added an answer on May 31, 2026 at 8:44 pm

    That’s your problem right there

    while(state==IN)
        a++;
    

    This is basically an endless loop, it will loop forever incrementing a

    Edit: Ok, part two then.

    First off what you are doing with a is not what you think you are doing.

    I think you think you are using it to count word length but you are not resetting it to 0 after each word so it will in fact keep on counting number of found chars. Your histogram will just show growing numbers.

    Second, in your print loop you are using i in both loops leading to a delicious mess of some sort.

    Edit: Part three

    I just spotted that you are printing the histogram inside the input-loop on the conditional

    if((c==getchar())&&c==EOF)
    

    This is probably not going to work, I suggest to move the printing outside the while loop and remove the condition

    while ((c=getchar())!=EOF)
    {
       ...
    }
    for(i=0;i<MAX;i++)
    {
        for(k=0;k<=word[i];k++)
            putchar('-'); 
        putchar('\n');
    }
    

    then it will get executed after you are finished taking input

    Edit: Part four

    You have reused i as index into the array without setting it to zero, the consequence being that the program assigns word lengths to indexes 30-59 which are all outside the array, that it does not crash is just pure luck. Setting i = 0 before entering the input loop makes the program work.

    Now all you have left to think about is why it prints word lengths one too long, I leave that as an exercise to the reader. Hint: It has to do with comparison operators

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.