In this exercise, we are going to implement a command prompt solely operating on
ID: 3545454 • Letter: I
Question
In this exercise, we are going to implement a command prompt solely operating on an array of string type elements. In the following description, I will describe each command your program should support and you should implement code one
command at a time in the order that is given.
In order to work with this exercise, you will need to declare the following variables (others will need to be declared as well):
an array of string type elements of size 10. I will refer to this array as words.
an integer that stores the lower bound of a range. I will refer to this value as lowerBound.
an integer that stores the upper bound of a range. I will refer to this value as upperBound.
Before you start your program, lowerBound should be initialized to 0, upperBound should be initialized to 9, so that the range lowerBound upperBound inclusive is referring to all the elements of the array words of size 10.
Commands for your command prompt:
1. quit - Your program should keep prompting for new commands until the user entered the command quit.
2. range - The lowerBound and upperBound were initialized to some certain value, but the user could reset it to any range they want, as long as it is a valid range. When the user enter this command, it will be prompted to enter two integer numbers for lower bound and upper bound as well as the condition they have to satisfy. If the user entered a set of invalid range, the original range will stay intact? otherwise, lowerBound and upperBound will be reset to the values given.
3. display - display all the words within the range (inclusive). See sample input/output of how you should display something after the input command.
4. reset - after enter this command, the user is prompted to enter one word, and all the elements in the array within the current range will be set to this word.
5. input - after enter this command, the user is prompted to enter multiple words, one at a time to fill in the current range.
6. reverse - reverse all the content within the range. There are more than one way to solve this problem, no matter what way you choose, we ask that you DO NOT declare an extra 4 array to do the reversing. Think about ways to reverse the values in the range inplace, by swapping corresponding values at the beginning and at the end of the range.
7. smallest - find the array element that holds the smallest word (in alphabetical order, i.e., 'apple' is smaller than 'book') within current range. Print out the smallest word as well as the index of the array where this word is stored.
Explanation / Answer
Here is your solution. Let me know if you need more help:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string words[10], command = "input", tempWord;
int lowerBound = 0, upperBound = 9, tempLower, tempUpper;
do
{
if(command.compare("quit") == 0)
{
exit(0);
}
else if(command.compare("range") == 0)
{
cout << "Enter the new range: lowerBound:";
cin >> tempLower;
cout << " upperBound:";
cin >> tempUpper;
if(tempUpper >= tempLower && tempLower < 10 && tempLower > -1
&& tempUpper < 10 && tempUpper > -1)
{
lowerBound = tempLower;
upperBound = tempUpper;
}
else
{
cout << "Invalid range. ";
}
}
else if(command.compare("display") == 0)
{
for (int i = lowerBound; i <= upperBound; i++)
{
cout << "Position " << i << ": " << words[i] << " ";
}
}
else if(command.compare("reset") == 0)
{
cout << "Entered the word to reset all the words to:";
getline(cin, tempWord);
lowerBound = 0;
upperBound = 9;
for (int i = lowerBound; i <= upperBound; i++)
{
words[i] = tempWord;
}
}
else if(command.compare("input") == 0)
{
cout << "Enter the new words: ";
for (int i = lowerBound; i <= upperBound; i++)
{
cout << "Position " << i << ": ";
getline(cin, words[i]);
}
}
else if(command.compare("reverse") == 0)
{
for (int i = 0; i < 5; i++)
{
tempWord = words[i];
words[i] = words[9 - i];
words[9 - i] = tempWord;
}
}
else if(command.compare("smallest") == 0)
{
int index = lowerBound;
for (int i = lowerBound + 1; i <= upperBound; i++)
{
if(words[index].compare(words[i]) > 0)
{
index = i;
}
}
cout << "Smallest word is: " << words[index] << " At the index: "
<< index << endl;
}
else
{
cout << "Invalid command, Please retry! ";
}
cout << "Enter next command: ";
getline(cin, command);
}
while (command.compare("quit") != 0);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.