Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ template and 7.12 are below please use template. Write a program that will read a line of text and output a list of all

C++ template and 7.12 are below please use template.

Write 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

Your program will need to sort the arrays according to the values in the integer array. You can modify the function sort given in Display 7.12 and use it in your program. You cannot use sort to solve this problem without changing the function.

Display 7.12

#include

void fillArray(int a[], int size, int& numberUsed); //Precondition: size is the declared size of the array a. //Postcondition: numberUsed is the number of values stored in a. //a[0] through a[numberUsed - 1] have been filled with //nonnegative integers read from the keyboard.

void sort(int a[], int numberUsed); //Precondition: numberUsed //The array elements a[0] through a[numberUsed - 1] have values. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0]

void swapValues(int& v1, int& v2); //Interchanges the values of v1 and v2.

int indexOfSmallest(const int a[], int startIndex, int numberUsed); //Precondition: 0 //values. //Returns the index i such that a[i] is the smallest of the values //a[startIndex], a[startIndex + 1], ..., a[numberUsed - 1].

int main( ) { using namespace std; cout

int sampleArray[10], numberUsed; fillArray(sampleArray, 10, numberUsed); sort(sampleArray, numberUsed);

cout for (int index = 0; index cout cout

return 0; }

//Uses iostream: void fillArray(int a[], int size, int& numberUsed)

void sort(int a[], int numberUsed) { int indexOfNextSmallest;

for (int index = 0; index {//Place the correct value in a[index]: indexOfNextSmallest = indexOfSmallest(a, index, numberUsed); swapValues(a[index], a[indexOfNextSmallest]); //a[0] //elements. The rest of the elements are in the remaining positions. } }

void swapValues(int& v1, int& v2) { int temp; temp = v1; v1 = v2; v2 = temp; }

int indexOfSmallest(const int a[], int startIndex, int numberUsed) { int min = a[startIndex], indexOfMin = startIndex; for (int index = startIndex + 1; index if (a[index] { min = a[index]; indexOfMin = index; //min is the smallest of a[startIndex] through a[index] }

return indexOfMin; }

TEMPLATE \\

#include #include #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 //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]

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 //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

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 }

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 for ( int i = 0; i cout }

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions

Question

2. Avoid controlling language, should, must, have to.

Answered: 1 week ago

Question

Compare and contrast licensing and subcontracting.

Answered: 1 week ago