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

  • Home
  • SEARCH
  • 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 3288058
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:33:50+00:00 2026-05-17T20:33:50+00:00

This is a K&R exercise (1-13)… Write a program to print a histogram of

  • 0

This is a K&R exercise (1-13)…

“Write a program to print a histogram
of the length of words in its input.
It is easy to draw the histogram with
bars horizontal; a vertical
orientation is more challenging.”

The section was about arrays, and to be honest, I’m not sure I fully understood it. Everything up to this point was fairly easy to grasp, this was not.

Anyway I’m trying to do a histogram with horizontal bars first. Once I got that down I’ll try vertical, but right now I’m not even sure where to begin with the easy version. (I slept on it, woke up, and still couldn’t get it.)

I drew an example of what the program would output:

----------------------------------------------------------------
001|XX
002|XXXX
003|X
004|XXXXXXXXXX
005|XXXXXXXXXXXXXXXXXXXXXXXXX
006|XXXX
007|X
008|
009|XXXXXXXXX
010|XXX
>10|XXXX
----------------------------------------------------------------

And tried to break it (the program) down in sections. This is what I came up with:

  1. PRINT TOP BORDER
  2. PRINT CATEGORY, PRINT X EACH TIME CONDITION IS TRUE, PRINT NEWLINE,
    REPEAT.
  3. PRINT BOTTOM BORDER

But the more I think about it the less I think that’s how it would work (because getchar() goes through one character at a time, and it wouldn’t be able to go back up to put a X in the right category.) Or…

… I’m just really confused as to how I would solve this problem. Here’s as far as I’ve been able to get code wise:

#include <stdio.h>

#define MAXWORDLENGTH 10

// print a histogram of the length of words in input. horizontal bar version

int main(void)
{
  int c;
  while ((c = getchar()) != EOF) {

  }

  return 0;
}

Could someone help enlighten me? Not necessarily with the code, maybe just pseudo code, or with some “words from the wise” as to what I need to do, or think, or something. This has just been a really big stone in the road and I’d like to get past it :/.

(I’ll check back in 30 minutes)

  • 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-17T20:33:51+00:00Added an answer on May 17, 2026 at 8:33 pm

    I loved the pseudo-code! Some good thinking there, but you’re still not ordering your program right.

    As you said yourself, you can’t read the text, go back and print an X in a particular row. If we establish that it can’t be done, then there’s no choice but to know all the values of the histogram beforehand.

    So you should think your program as having two parts (and you’ll make this kind of division in practically every program you write): first, a part that will make calculations; and then a part that will output them in a certain format (the histogram).

    This tip should get you started! If you need further help, comment below.

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

Sidebar

Related Questions

I'm doing K&R's Exercise 1-10 Write a program to copy its input to its
I'm solving this K&R exercise: Write versions of the library functions strncpy , strncat
I'm on to K&R's Exercise 1-18 Write a program to remove trailing blanks and
This is my version of detab, from this K&R exercise: Modify detab to accept
I'm writing code for the exercise 1-24, K&R2, which asks to write a basic
I'm doing K&R Exercise 5-4 (p107). Write the function strend(s,t) , which returns 1
like this: Groovy: map = ['a':1,'b':2] doubler = this.&doubleMethod map.each(doubler) println map What's the
This pertains to C# & the .NET Framework specifically. It's best practice not to
This code function LoadContent(Id) { alert('Controls/Network/NetworkDetail.aspx?' + rnd() + '&CtlId=' + Id); $.get('Controls/Network/NetworkDetail.aspx?' +
Take this code: <?php if (isset($_POST['action']) && !empty($_POST['action'])) { $action = $_POST['action']; } if

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.