Question: C++ USE TEMPLATE BELOW Problem: rite a program that will read a line of text and output a list of all the letters that occur
C++ USE TEMPLATE BELOW
Problem:
rite a program that will read a line of text and output a list of all the letters that occur in the text together with the number of times each letter occurs in the line. End the line with a period that serves as a sentinel value. The letters should be listed in the following order: the most frequently occurring letter, the next most frequently occurring letter, and so forth. Use two arrays, one to hold integers and one to hold letters. You may assume that the input uses all lowercase letters. For example, the input
do be do bo.
Should produce output similar to the following:
| Letter | Number of Occurrences |
|---|---|
| o | 3 |
| d | 2 |
| b | 2 |
| e | 1 |
TEMPLATE:
#include
struct char_freq { char ch; int count; };
//NOTE that all that is necessary in the headers is to //replace int with char_freq
void sort(char_freq a[], int number_used); //Precondition: number_used <= declared size of the array a. //The array elements a[0] through a[number_used - 1] have //values. //Postcondition: The values of a[0] through //a[number_used - 1] have been rearranged so that //a[0] <= a[1] <= ... <= a[number_used - 1].
void swap_values(char_freq& v1, char_freq& v2); //Interchanges the values of v1 and v2.
int index_of_largest(const char_freq a[], int start_index, int number_used); //Precondition: 0 <= start_index < number_used. //Referenced array elements have values. //Returns the index i such that a[i].count is the largest of //the values //a[start_index].count, a[start_index + 1].count, ..., //a[number_used - 1].count
void input( char_freq list[], int& size); //list is an array of char_freq structs. //reads input, period is sentinel. Sets the char members of the //char_freq struct elements of list to newly occurring characters, // otherwise, if the struct already has the character, this routine // increments the count for that character.
void output( char_freq list[], int size ); //lists Letter, Number of Occurrences in columns
bool lookup( char c, char_freq list[], int& size ); //traverses the elements of list through size elements //if an element containing c is found, // increment count in the list member for that character // returns true (1) (c is in the list) //else // returns false(0) (c is not in the list)
void insert( char c, char_freq list[], int& size ); //precondition: c is not in the list //post condition: c has been added in position size, //size has been incremented. //size <= 26
int main() { char_freq list[50] ={0,0}; //we are going to pick up //punctuation, etc. int list_size = 0; input( list, list_size ); sort( list, list_size ); output( list, list_size); return 0; }
void input(char_freq list[], int& size) { using namespace std; char ch; cin >> ch; //we want to ignore blanks, so we use cin >>... ch = tolower(ch); // Push everything to lowercase. while('.' != ch) { if (!lookup(ch, list, size)) insert(ch, list, size); cin >> ch; } }
void insert(char c, char_freq list[], int& size) { list[size].count=1; list[size].ch = c; size ++; assert( size <= 26); }
bool lookup(char c, char_freq list[], int& size) { int i = 0; bool success = false; // Complete the function
return success; }
void output( char_freq list[], int size ) { using namespace std; cout << "Letter:\tNumber of Occurrences." << endl; for ( int i = 0; i < size; i++) cout << list[i].ch << '\t' << list[i].count << endl; }
void sort(char_freq a[], int numberUsed) { // Complete the function }
void swap_values(char_freq& v1, char_freq& v2) { // Complete the function }
int index_of_largest(const char_freq a[], int startIndex, int numberUsed) { // Complete the function
return indexOfMax; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
