• This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #1836
    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. (Source Brian & Kernighan, 2nd edition).
    Here is how I approach
    
    
    #include<stdio.h>
    int main()
    {
        int x = getchar();
        int wc = 0;
        int charactercount = 0;
        int wordstoragecount[]
        while (x != EOF)
        {
        
            if (x == ' ' || x == '\t' || x == '\n')
            {
                wc = wc + 1;
                putchar(x);
                x = getchar()
                charactercount = 0;
            }
            else
            charactercount++;
            putchar(x)
            x = getchar();
            
        }

    What I visualize for wordstoragecount is count the number of letters per word. Suppose user enters:
    C is a programming language.
    Then
    wordstoragecount[0] = 1
    wordstoragecount[1] = 2
    wordstoragecount[3] = 1
    wordstoragecount[4] = 11
    wordstoragecount[5] = 8
    But I doubt this can be performed by array as the number of elements to be entered by the user is unknown and dynamic.
    Pointers and malloc are not yet introduced in the first chapter.
    I understand that if the user enters space/tab/new line right at the beginning, then the code will wrongly count as 1 word. But for the time being, I am ignoring this.

    ChatGPT:

    It looks like you’re on the right track for counting the lengths of words in the input. However, you can simplify the process and use an array to store the counts for each word length. Here’s a modified version of your code:

    
    
    #include <stdio.h>
    
    #define MAX_WORD_LENGTH 15  // Assuming a maximum word length of 15 characters
    
    int main() {
        int x;
        int wc = 0;
        int charactercount = 0;
        int wordstoragecount[MAX_WORD_LENGTH + 1] = {0};  // Initialize array with zeros
    
        while ((x = getchar()) != EOF) {
            if (x == ' ' || x == '\t' || x == '\n') {
                if (charactercount > 0 && charactercount <= MAX_WORD_LENGTH) {
                    wordstoragecount[charactercount]++;
                }
                charactercount = 0;
                wc++;
            } else {
                charactercount++;
            }
        }
    
        // Print the histogram
        printf("Word Length Histogram:\n");
        for (int i = 1; i <= MAX_WORD_LENGTH; i++) {
            printf("%2d | ", i);
            for (int j = 0; j < wordstoragecount; j++) {
                putchar('*');
            }
            putchar('\n');
        }
    
        return 0;
    }
    

     

    In this code:

    • I introduced a constant MAX_WORD_LENGTH to represent the maximum expected word length.
    • An array wordstoragecount is used to store the counts of words of different lengths. The array is initialized with zeros.
    • The charactercount is incremented for each character within a word.
    • After encountering a space, tab, or newline character, the code checks if charactercount is greater than 0 and within the specified limit (MAX_WORD_LENGTH). If so, it increments the corresponding element in wordstoragecount.
    • Finally, it prints a horizontal histogram based on the counts.

    This program avoids the need for dynamic memory allocation, making it suitable for the early chapters where pointers and malloc are not yet introduced.

     


    Here is how frequency of word length counted using nested while loop:

    
    
    #include <stdio.h>
    
    #define MAXWORDLENGTH 15
    
    int main() {
        int c, cc, wc;
        cc = wc = 0;
        int wordcountstorage[MAXWORDLENGTH] = {0};
        
        c = getchar();
        while (c != EOF) {
            while (c != ' ' && c != '\t' && c != '\n' && cc < MAXWORDLENGTH) {
                cc++;
                c = getchar();
            }
            wordcountstorage[cc]++;
            cc = 0;
            c = getchar();
        }
        
        printf("Word Lengths Distribution:\n");
        for (int i = 0; i < MAXWORDLENGTH; i++) {
            printf(" wordcountstorage of %d length is %d \n", i, wordcountstorage);
        }
    
        return 0;
    }
    

     
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Scroll to Top