Thread: help with histogram exercise in K&R book

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    12

    help with histogram exercise in K&R book

    Hello, I am currently working on a exercise in K&R book which by the is an excellent book, but the solutions on the net really do not have any comments what so ever. Any help in clearing up my confusion would be greatly helpful. Before I ask questions, let me show you the code found on the interent
    Code:
    #include<stdio.h>
    
    #defineMAXWORDLEN10
    
    
    intmain(void)
    {
    intc;
    intinspace = 0;
    longlengtharr[MAXWORDLEN+ 1];
    intwordlen = 0;
    
    
    intfirstletter = 1;
    longthisval = 0;
    longmaxval = 0;
    intthisidx = 0;
    intdone = 0;
    
    
    for(thisidx= 0; thisidx <= MAXWORDLEN;thisidx++)
      {
       lengtharr[thisidx]= 0;
      }
    
    
    while(done== 0)
      {
        c= getchar();
    
    
    if(c== ''|| c == '\t'|| c == '\n'|| c == EOF)
        {
    if(inspace== 0)
          {
           firstletter= 0;
            inspace= 1;
    
    
    if(wordlen<= MAXWORDLEN)
            {
    if(wordlen> 0)
              {
               thisval= ++lengtharr[wordlen - 1];
    if(thisval> maxval)
                {
                 maxval= thisval;
                }
              }
            }
    else
            {
              thisval= ++lengtharr[MAXWORDLEN];
    if(thisval> maxval)
              {
               maxval= thisval;
              }
            }
          }
    if(c== EOF)
          {
            done= 1;
          }
        }
    else
        {
    if(inspace== 1 || firstletter == 1)
          {
            wordlen= 0;
           firstletter= 0;
            inspace= 0;
          }
          ++wordlen;
        }
      }
    
    
    for(thisval= maxval; thisval > 0; thisval--)
      {
        printf("%4d | ",thisval);
    for(thisidx= 0; thisidx <= MAXWORDLEN;thisidx++)
        {
    if(lengtharr[thisidx]>= thisval)
          {
            printf("* ");
          }
    else
          {
            printf("  ");
          }
        }
        printf("\n");
      }
      printf("     +");
    for(thisidx= 0; thisidx <= MAXWORDLEN;thisidx++)
      {
       printf("---");
      }
      printf("\n      ");
    for(thisidx= 0; thisidx < MAXWORDLEN;thisidx++)
      {
        printf("%2d",thisidx + 1);
      }
      printf(">%d\n",MAXWORDLEN);
      getch();
    
    
    return0;
    }
    Question 1: When the lengtharr[thisidx] is being initialized, does that make thisidx equal to zero or its just a first element?

    Question 2: Which part stores the word count in the array or is it being store at all?

    Question 3: what does
    Code:
    thisval = ++lengtharr[MAXWORDLEN];
    do, does it adds an element or what?

    The output of the code is on The C Programming Language Exercise 1-13
    please scroll down.

    Any help is greatly appreciated and thank you for your time in reading this lengthy post.
    EDIT: the code is on the link as well, since I still don't know how to scroll the code on here.
    Last edited by piratemonkey247; 01-17-2013 at 03:54 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I would suggest you edit your post, and have another go at pasting your code.

    So that it isn't all on one very long line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    12
    Quote Originally Posted by Salem View Post
    I would suggest you edit your post, and have another go at pasting your code.

    So that it isn't all on one very long line.
    The link where the output is at has the code as well, but I think I have it pasted correctly now.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try 'paste as text', not paste as html.

    Check other posted code in code tags on this forum - it looks a lot better than that!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    12
    This thread is officially closed due to the crappy code output

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with the exercise from the book
    By kenryuakuma in forum C++ Programming
    Replies: 19
    Last Post: 09-26-2008, 08:07 AM
  2. Help with K&R Book Exercise
    By Alejandrito in forum C Programming
    Replies: 5
    Last Post: 03-11-2008, 01:24 PM
  3. book exercise
    By luigi40 in forum C# Programming
    Replies: 1
    Last Post: 11-13-2005, 11:28 AM
  4. Exercise from my book.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 12-05-2002, 08:54 AM
  5. any recommended exercise book for C++?
    By gogo in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2001, 04:44 PM