C++ programming. In this assignment, we will keep track of the strings a user ha
ID: 3598327 • Letter: C
Question
C++ programming.
In this assignment, we will keep track of the strings a user has entered and the amount of times each string was recorded.
Create an array of strings of a large size. 1000 is a good number. You will not use the entire array, but you will partially fill it as the program runs. Create a variable that keeps track of the number of different inputs that have been entered, so you know how much of the array you have already filled.
Create a parallel array of ints to store the frequency of each string, which is the same number of elements as the string array.
Create a menu. Use the following code:
If the user chooses option 1, prompt them to enter a string:
If you used cin to input the menu choice, be sure to use cin.ignore() before calling getline to input the string.
Once the user inputs the string, implement the following logic:
If the user chooses menu option 2, print the sorted list of strings and their frequencies. A sample output is shown below:
Don't forget that capital letters are sorted alphabetically earlier than lowercase. This doesn't mean your code isn't working.
For searching and sorting your arrays, use the following prototypes. I will be unit testing them:
Explanation / Answer
#include <iostream>
#include <algorithm>
using namespace std;
int searchInputs(string info[], string input, int count){
int index = -1;
for(int i = 0; i < count ; i++){
if(info[i] == input){
index = i;
break;
}
}
return index;
}
void sortInputs(string info[], int freq[], int count){
sort(info, info + count);
for(int i = 0; i < count ; i++){
cout << "Input: " << info[i] << endl;
cout << "Frequency: " << freq[searchInputs(info, info[i], count)] << endl;
}
}
int main()
{
string input, strings[50];
int freq[100], choice, result, count = 0;
while(1){
cout << "(1) Enter a string " << endl;
cout << "(2) Print a sorted list of inputs and their frequencies" << endl;
cout << "(3) Quit ";
cin >> choice;
if(choice == 1){
cout << "Enter a string: ";
cin >> input;
result = searchInputs(strings, input, count);
if(result == -1){
strings[count] = input;
freq[count] = 0;
count++;
}
else{
strings[result] = input;
freq[result]++;
}
}
else if(choice == 2){
sortInputs(strings, freq, count);
}
else
break;
}
return 0;
}
**Comment for any further queries.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.