8 Replies - 4091 Views - Last Post: 18 September 2010 - 12:58 PM Rate Topic: -----

#1 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Histogram of the lengths of words. (C)

Posted 17 September 2010 - 04:57 PM

Task:
Exercise 1-13. 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; a vertical orientation is more challenging.

From:
The C Programming Language Book

I've decided not to worry about the histogram but rather I'm just trying to record all the different lengths in an array. So Length Array[0] would have the number of words with 1 character, Length Array[1] would have the number of words with 2 characters etc.

My Attempt:

Pseudo Code:

While character is not EOF
Increase character count by 1

If character = space or newline or tab then
Length Array[char count] = Length Array[char count] + 1
Character count = 0
End if
End while


C code:

#include <stdio.h>
#define MAXWORDLEN 10

main()
{
    int c, nw, nc, length[MAXWORDLEN];

    nw = nc = 0;

    while ((c = getchar()) != EOF) 
    {
        ++nc;
        if (c == ' ' || c == '\n' || c == '\t')
        {
            length[nc] = length[nc] + 1;
            ++nw;
            nc = 0;
        }
    }
    printf("%d %d %d\n", length[3], nw, nc);
}


It seems I'm struggling with The C Programming Language Book and I'm only in Chapter 1, is this book worth sticking with as I've read it's one of the best on programming in C.

This post has been edited by Matty919: 17 September 2010 - 04:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Histogram of the lengths of words. (C)

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Histogram of the lengths of words. (C)

Posted 17 September 2010 - 06:13 PM

Quote

It seems I'm struggling with The C Programming Language Book and I'm only in Chapter 1, is this book worth sticking with as I've read it's one of the best on programming in C.
The book is good, but you have to keep two things in mind. The first is that C isn't a beginner friendly language. If you aren't an experienced programmer, take that into account. Consider then that your book isn't about teaching how to program, it's about the C language. It works great for experienced programmers, but the less comfort you have with programming, the tougher the book will be.

Your code is looking good, so take that as a sign. I would, however, note that the correct signature for main is int main() and that by consequence, you should have return 0; at the end of main.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Guest*


Reputation:

Re: Histogram of the lengths of words. (C)

Posted 17 September 2010 - 06:24 PM

View PostOler1s, on 17 September 2010 - 05:13 PM, said:

Quote

It seems I'm struggling with The C Programming Language Book and I'm only in Chapter 1, is this book worth sticking with as I've read it's one of the best on programming in C.
The book is good, but you have to keep two things in mind. The first is that C isn't a beginner friendly language. If you aren't an experienced programmer, take that into account. Consider then that your book isn't about teaching how to program, it's about the C language. It works great for experienced programmers, but the less comfort you have with programming, the tougher the book will be.

Your code is looking good, so take that as a sign. I would, however, note that the correct signature for main is int main() and that by consequence, you should have return 0; at the end of main.


Thanks for the advice, I'm not really that experienced apart from some VB.NET but I will keep going at the book and maybe watch some uTube videos too. You said my program looks good but I (critically) forgot to mention that it seems to be giving me wrong answers.

If I entered the input:

aaaa aaaa aaaa


Those aaaas would be 4 characters each which would mean I would of thought

length[3] would equal 4? (as arrays start at an index of 0?)

But Length[3] seems to hold the value of 192...

Thanks.
Was This Post Helpful? 0

#4 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: Histogram of the lengths of words. (C)

Posted 17 September 2010 - 06:25 PM

And I wasn't logged in...
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6031
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: Histogram of the lengths of words. (C)

Posted 17 September 2010 - 06:41 PM

Because you did not initialize the values in the length array, you can't really be sure what is in the array at the start. So this line:
length[nc] = length[nc] + 1;

could be adding 1 to -92 for all you know.

The array should be initialized to all 0s like this:
length[MAXWORDLENGTH] = { 0 };


EDIT: And I hit the wrong key and submitted too soon.

string:                   aaaa aaaa aaaa
value of nc @ evaluation: 123451234512345



The way you're incrementing character count, when you hit the space and tally the length, what is the value of nc at that point?
Was This Post Helpful? 1
  • +
  • -

#6 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: Histogram of the lengths of words. (C)

Posted 18 September 2010 - 09:22 AM

View PostJackOfAllTrades, on 17 September 2010 - 05:41 PM, said:

The array should be initialized to all 0s like this:
length[MAXWORDLENGTH] = { 0 };


Hmm this doesn't seem to be working? Unexpected token = and }
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Histogram of the lengths of words. (C)

Posted 18 September 2010 - 09:32 AM

Are you sure you are initializing and not assigning?

int a = 0; //initialization
a = 1; //assignment



Notice that JackOfAllTrades pointed out syntax for initialization.
Was This Post Helpful? 1
  • +
  • -

#8 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: Histogram of the lengths of words. (C)

Posted 18 September 2010 - 12:37 PM

#include <stdio.h>
#define MAXWORDLEN 10

void draw(int length[]);

int main()
{
    int c, nw, nc;
    int length[MAXWORDLEN] = { 0 };
    nw = nc = 0;

    while ((c = getchar()) != EOF) 
    {
        if (c == ' ' || c == '\n' || c == '\t')
        {
            length[nc-1] = (length[nc-1]) + 1;
            ++nw;
            nc = 0;
        }
        else
        ++nc;
    }
    printf("%d %d %d\n", length[3], nw, nc);
    draw(length[MAXWORDLEN]);
    return 0;
}

void draw(int length[MAXWORDLEN])
{
    int i;
    int x;
    printf("\tFrequency\n");
    printf("-----------------------------\n");
    for(i=0; i <=10; ++i)
    {
        for(x=0; x <=length[i]; ++x)
        printf("*\n");
        x=0;
    }
    printf("1234568910\n");
    return 0;
}



Ok I think the first bit is correct, now I'm trying to draw the histogram with the use of a different function. I get a segmentation fault, I'm not sure what that is. To be honest I don't know if I have even declared the function properly or passed the array to it correctly. Any help would be great.

Thanks

This post has been edited by Matty919: 18 September 2010 - 12:38 PM

Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Histogram of the lengths of words. (C)

Posted 18 September 2010 - 12:58 PM

Quote

I get a segmentation fault, I'm not sure what that is.
It means your program tried to access memory it shouldn't have. There's a few different ways you can pull that off.

An easy way to is to write out of bounds.

int a[10]; 
a[100] = 0; //Hmmm?



See Wikipedia and Google for more information. Whenever you get an error you don't understand, don't be afraid to consult Google.

So, do you do something like that anywhere in your program? Do you go out of bounds when writing or reading an array somewhere?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1