IT, Programming, & Web Development › Forums › The C Programming Language by Brian & Ritchie (2nd edition) › Chapter 1: A Tutorial Introduction › Word length histogram program without dynamic memory allocation in C
- This topic is empty.
-
AuthorPosts
-
December 2, 2023 at 10:41 am #1836Exercise 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
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#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(); }
[/dm_code_snippet]
What I visualize for wordstoragecount is count the number of letters per word. Suppose user enters:C is a programming language.Thenwordstoragecount[0] = 1wordstoragecount[1] = 2wordstoragecount[3] = 1wordstoragecount[4] = 11wordstoragecount[5] = 8But 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:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#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[i]; j++) { putchar('*'); } putchar('\n'); } return 0; }
[/dm_code_snippet]
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 inwordstoragecount
. - 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:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#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[i]); } return 0; }
[/dm_code_snippet]
- I introduced a constant
-
AuthorPosts
- You must be logged in to reply to this topic.