Question: IN C PROGRAMMING PLEASE, THANK YOU 6.15Program 6: Using Arrays to Count Letters in Text 1. Introduction In this program, you will practice working with

IN C PROGRAMMING PLEASE, THANK YOU

6.15Program 6: Using Arrays to Count Letters in Text

1. Introduction

In this program, you will practice working with arrays. Your program will read lines of text from the keyboard and use an array to track the number of times each letter occurs in the input text. You will use the contents of this array to generate a histogram (bar graph) indicating the relative frequencies of each letter entered.

Test cases are available inthis document.

Notes:

  • You MUST complete the Blackboard assignment "Program 6 style assessment" to get all of the points for this program. You can complete this "assignment" by typing a short message indicating you submitted your code through the textbook IDE.
  • The early submission deadline is Friday, 10/30; submissions by this date will earn 10 bonus points.
  • The final due date for this assignment is Monday, 11/2.

2. Specification

Note: As discussed in class, there are a couple of inefficient brute-force methods to determine which letter is entered. (These prohibited methods are described in Section 3, Hints.) Using either method will incur a10 point deduction.

Note 2: As also discussed in class, this program shouldnot use strings.

Program Variables

At a minimum, you will need variables inmain()to track:

  • The number of times each letter in the alphabet has occurred in the input. These values should be stored in an array, in which the first entry represents the number of'A'or'a'characters entered, and the last entry represents the number of'Z'or'z'characters entered.
  • For example, if your first input is:Hello World!, the array should hold:{0,0,0,1,1,0,0,1,0,0,0,3,0,0,2,0,0,1,0,0,0,0,1,0,0,0}.
  • The non-zero values indicate the number of'D','E','H','L','O','R', and'W'characters entered, respectively.
  • Note that the character inputs are case-insensitiveuppercase and lowercase letters are treated the same.
  • The maximum number of times any letter occurred3, in the example above.

These variables are updated every time the user enters the 'R' command followed by a line of input, and reset to 0 every time the user enters the 'C' command. They are used to print the histogram whenever the 'P' command is used.

The variables should be updated inside theReadText()function, and printed using theDrawHist()function.

Program Structure

Your program should repeatedly prompt the user to enter a single character command until the user enters the "quit" command. The program should perform the following operations for the listed commands:

  • 'C','c': Clear the letter counts to 0 in preparation for analyzing new text.
  • 'R','r': Read one line of text from the keyboard. Update the letter counts appropriately, using theReadText()function.
  • 'P','p': Plot the histogram of letter frequencies in all lines of text entered since the last'C'command, using theDrawHist()function.
  • 'Q','q': Quitexit the program.

Functions

Your program should contain the functions described below, as well as any other functions you choose to add. Prototypes for these functions should be inprog6_functions.h, while their definitions should be written inprog6_functions.c:

  • void ReadText(int histo[], int *max);Read one line of text from the keyboard, updating the arrayhisto[]with the number of occurrences of each letter of the alphabet. Also, use the pointer max to update the variable holding the number of occurrences of the most frequent letter.
  • void DrawHist(int histo[], int max);Given the array holding the letter counts (histo[]) and the number of occurrences of the most frequent letter (max), plot a histogram of the letter counts. Note that:
  • maxhelps you determine the height of the histogram, and therefore the number of rows to print.
  • When printing each row, print a vertical bar and a space"| "in the appropriate column if the letter count is large enough to be displayed in that row. Print two spaces otherwise.
  • Remember to leave spaces between columnsyour output should line up exactly as shown in the test cases.
  • Remember that you are printing from the top of the histogram to the bottomthat fact will affect the design of this function.

See the test cases that demonstrate the proper format for input and output.

3. Hints

ASCII values

Recall that each character has a corresponding integer value, according to the ASCII standard. You may find it useful to work directly with ASCII values in this program, rather than testing if each input character matches a particular letter.'A'has the ASCII value 65;'a'has the ASCII value 97.

Character functions

You may find the following built-in functions from thelibrary useful. Remember thatintandchardata types are compatible:

  • int isalpha(int ch): This function returns a non-zero value ifchis a letter of the alphabet, and zero otherwise.
  • For example,isalpha('Q')returns a non-zero value;isalpha('3')returns 0.
  • int tolower(int ch): Ifchis an uppercase letter, this function returns the lowercase version. Otherwise,tolower()returns the original value ofch.
  • For example,tolower('Q')returns'q';tolower('a')returns'a'.
  • int toupper(int ch): Ifchis a lowercase letter, this function returns the uppercase version. Otherwise,tolower()returns the original value ofch.
  • For example,toupper('q')returns'Q';toupper('A')returns'A'.

Prohibited brute force methods

Brute force methods are inefficient solutions to problems. Such a method can be used to determine which letter was entered and update the appropriate array entry, but I want you to find a more efficient solution. Using a brute force method like the following will therefore incur a10 point deduction.

Again, each of the following is an example of what NOT to do:

  • Large switch (or if) statement: one case for each letter, with array index as a known constant in each case:

switch(ch) { // ch = input char case 'A': case 'a': // modify histo[0] break; case 'B': case 'b': // modify histo[1] break; // Remainder of cases } 
  • Loop: compares input character against all possible letters, using loop index as array index:

char test = 'A'; for (i = 0; i < 26; i++) { if (ch == test) // modify histo[i] test++; // Change test } // to next letter 

Note: Each brute force method essentially compares the input character against all possible letters. However, once you know the input character is a letter, you do not need to compare it to anything to determine what array entry to update! There's a simple "transformation" between the ASCII value of each letter and the corresponding index in the histogram array.

4. Grading Rubric

For this assignment, points are assigned as follows:

  • 40 points: Your code uses appropriate coding style such as including appropriate comments, indenting the main() function body, and appropriate variable declarations.You must complete the Blackboard assignment "Program 6 style assessment" to earn any of these points.
  • 60 points: Your code compiles and output matches the desired test outputs shown below. Each test case has a different number of points assigned to it, as shown in submit mode.This section will be auto-graded, while I will assign the other 40 points after inspecting your program. Seethe test casesfor a full sample run of the program.

Not sure where to start on error fixing:

Code I've Written but not sure where Im going wrong:

/* prog6_functions.h */

#ifndef prog6_functions_h

#define prog6_functions_h

void ReadText(int histo[],int *max);

void DrawHist(int histo[],int max);

#endif

/* prog6_functions.c */

#include"stdio.h"

#include"string.h"

/* read text */

void ReadText(int histo[], int *max) {

char line[100];

int i;

int len;

printf(" ENTER A LINE OF TEXT: ");

/* clear previous input */

fflush(stdin);

/* input for text */

fgets(line,100,stdin);

len = strlen(line);

/* read each character from line */

for (i = 0; i

/* if small character found */

if (line[i] >= 'a' && line[i] <= 'z')

{

/* increment number of character */

histo[(line[i] - 'a')]++;

}

/* if capital letter found */

if (line[i] >= 'A' && line[i] <= 'Z')

{

/* increment number of character */

histo[(line[i] - 'A')]++;

}

}

/* find maximum number from histogram */

for (i = 0; i<26; i++) {

if (*max

*max = histo[i];

}

}

}

/* draw histogram */

void DrawHist(int histo[], int max) {

int i, j;

printf(" LETTER FREQUENCIES IN TEXT: ");

/* print '|' if character found */

for (j = max; j >= 0; j--) {

for (i = 0; i<26; i++) {

if (histo[i] >= j + 1) {

printf("|");

printf(" ");

}

else {

printf(" ");

printf(" ");

}

}

printf(" ");

}

/* print +-+-+- like it */

for (i = 0; i<26; i++) {

printf("+-");

}

printf(" ");

/* print characters */

for (i = 0; i<26; i++) {

printf("%c ", ('A' + i));

}

printf(" ");

}

/* start main function */

int main() {

int histo[26];

char input;

int i;

int max;

/* initialize max and histogram */

max = 0;

for (i = 0; i<26; i++) {

histo[i] = 0;

}

/* loop */

while (1) {

printf("Enter command (C , R, P, or Q): ");

/* clear previous input */

fflush(stdin);

/* input */

scanf("%c", &input);

/* for quit */

if (input == 'Q' || input == 'q') {

break;

}

else {

/* for read */

if (input == 'R' || input == 'r') {

ReadText(histo, &max);

}

else {

/* for print */

if (input == 'P' || input == 'p') {

DrawHist(histo, max);

}

else {

/* for clear */

if (input == 'C' || input == 'c') {

for (i = 0; i<26; i++) {

histo[i] = 0;

}

max = 0;

}

else {

/* invalid input */

printf("Invalid command %c ", input);

}

}

}

}

printf(" ");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!