Question
I have to follow this template for this assignment on Hexadecimal numerals are integers written in base 16. The 16 digits used are 0 through
I have to follow this template for this assignment on Hexadecimal numerals are integers written in base 16. The 16 digits used are 0 through 9 plus a for the digit 10, b for the digit 11, c for the digit 12, d for the digit 13, e for the digit 14, and f for the digit 15.
//Input: a line of text terminated with a period, which serves as //sentinel. //Output: a list of letters sorted into decreasing order by //frequency. // //Data Structure: array of struct, struct holds a char and int //(the count). // //Algorithm: //The text is scanned a character at a time, if the character //is present in the data structure, then update the count, if //not present insert the new character in the data structure, //update the count to 1. The array of structs is sorted on //the count key.
#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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started