Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C language. Your program will read lines of text from the keyboard and use an

ID: 3681726 • Letter: I

Question

In C language.

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 .

2. This assignment uses multiple files, all of which you will have to write: • prog6_histogram.c: Source file containing your main function. • prog6_functions.h: Header file containing function prototypes.. • prog6_functions.c: Source file containing other user-defined functions.

3. There are a couple of inefficient brute-force methods to determine which letter is entered (loop or switch statement comparing input to different letters). Using either of these methods will result in a 10 point deduction. Program variables: At a minimum, you will need variables in main() to track: • The number of times each letter in the alphabet has occurred in the input: o 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-insensitive—uppercase and lowercase letters are treated the same. • The maximum number of times any letter occurred—3, 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 the ReadText() function, and printed using the DrawHist() 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 the ReadText() function.

• 'P', 'p': Plot the histogram of letter frequencies in all lines of text entered since the last 'C' command, using the DrawHist() function.

• 'Q', 'q': Quit—exit 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 in prog6_functions.h, while their definitions should be written in prog6_functions.c:

• void ReadText(int histo[], int *max); Read one line of text from the keyboard, updating the array histo[] 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: o max helps you determine the height of the histogram, and therefore the number of rows to print. o When printing each row:

§ Print a vertical bar | in the appropriate column if the letter count is large enough to be displayed in that row.

§ Print a space otherwise.

§ Remember to leave spaces between columns—your output should line up exactly as shown in the test cases. Remember that you are printing from the top of the histogram to the bottom— that fact will affect the design of this function.

Hints and tips 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 the library useful. Remember that int and char data types are compatible: • int isalpha(int ch): This function returns a non-zero value if ch is a letter of the alphabet, and zero otherwise.

o For example, isalpha('Q') returns 1; isalpha('3') returns 0. • int tolower(int ch): If ch is an uppercase letter, this function returns the lowercase version. Otherwise, tolower() returns the original value of ch. o For example, tolower('Q') returns 'q'; tolower('a') returns 'a'. • int toupper(int ch): If ch is a lowercase letter, this function returns the uppercase version. Otherwise, tolower() returns the original value of ch. o For example, toupper('q') returns 'Q'; toupper('A') returns 'A'.

Strings cannot be used

Explanation / Answer

#include <stdio.h>

int alphaCount[26];

void ReadText()

{

char line[200];

int i;

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

scanf("%[^ ]s",line);

getchar();

for(i=0;line[i]!='';i++)

{

char letter = line[i];

if(letter>64 && letter<91)

{

alphaCount[letter-'A']++;

}

else if(letter>96 && letter<123)

{

alphaCount[letter-'a']++;

}

}

}

void DrawHist()

{

int i,j;

printf(" LETTER FREQUENCIES IN TEXT : ");

int maxCount=0;

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

{

if(alphaCount[i]>maxCount)

{

maxCount = alphaCount[i];

}

}

for(i=maxCount;i>0;i--)

{

for(j=0;j<26;j++)

{

if(i<=alphaCount[j])

{

printf("| ");

}

else

{

printf(" ");

}

}

printf(" ");

}

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

{

printf("+-");

}

printf(" ");

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

{

printf("%c ",65+i);

}

printf(" ");

}

int main()

{

char ch;

int i;

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

{

alphaCount[i] = 0;

}

while(1)

{

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

scanf("%c%*c",&ch);

if(ch=='C' || ch=='c')

{

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

{

alphaCount[i] = 0;

}

}

else if(ch=='R' || ch=='r')

{

ReadText();

}

else if(ch=='P' || ch=='p')

{

DrawHist();

}

else if(ch=='Q' || ch=='q')

{

break;

}

printf(" ");

}

}