Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

These variables must be declared as well as others an array of string type eleme

ID: 3545509 • Letter: T

Question

These variables must be declared as well as others
an array of string type elements of size 10 called words
an integer that stores the lower bound as 0 called lowerBound
an integer that stores the upper bound as 9 called 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.


Here is some sample input and it must be in this format


Enter a command for range 0 - 9:
range
Enter lower bound and upper bound so that 0 <= lower bound <= upper bound < 10:
2 8
Enter another command for range 2 - 8:
range
Enter lower bound and upper bound so that 0 <= lower bound <= upper bound < 10:
-2 6
Invalid range, range stays as 2 - 8.
Enter another command for range 2 - 8:
range
Enter lower bound and upper bound so that 0 <= lower bound <= upper bound < 10:
5 3
Invalid range, range stays as 2 - 8.
Enter another command for range 2 - 8:
range
Enter lower bound and upper bound so that 0 <= lower bound <= upper bound < 10:
2 2
Enter another command for range 2 - 2:
range
Enter lower bound and upper bound so that 0 <= lower bound <= upper bound < 10:
2 10
Invalid range, range stays as 2 - 2.
Enter another command for range 2 - 2:
range
Enter lower bound and upper bound so that 0 <= lower bound <= upper bound < 10:
6 8
Enter another command for range 6 - 8:
quit
Quitting the program.



or



Enter a command for range 0 - 9:
display
[0]: ''
[1]: ''
[2]: ''
2
[3]: ''
[4]: ''
[5]: ''
[6]: ''
[7]: ''
[8]: ''
[9]: ''
Enter another command for range 0 - 9:
range
Enter lower bound and upper bound so that 0 <= lower bound <= upper bound < 10:
3 7
Enter another command for range 3 - 7:
reset
Enter a word:
black
Enter another command for range 3 - 7:
range
Enter lower bound and upper bound so that 0 <= lower bound <= upper bound < 10:
1 8
Enter another command for range 1 - 8:
display
[1]: ''
[2]: ''
[3]: 'black'
[4]: 'black'
[5]: 'black'
[6]: 'black'
[7]: 'black'
[8]: ''
Enter another command for range 1 - 8:
range
Enter lower bound and upper bound so that 0 <= lower bound <= upper bound < 10:
4 6
Enter another command for range 4 - 6:
input
Enter 3 words:
[4]:
white
[5]:
green
[6]:
yellow
Enter another command for range 4 - 6:
display
[4]: 'white'
[5]: 'green'
[6]: 'yellow'
Enter another command for range 4 - 6:
range
Enter lower bound and upper bound so that 0 <= lower bound <= upper bound < 10:
3
0 9
Enter another command for range 0 - 9:
display
[0]: ''
[1]: ''
[2]: ''
[3]: 'black'
[4]: 'white'
[5]: 'green'
[6]: 'yellow'
[7]: 'black'
[


or


Enter a command for range 0 - 9:
input
Enter 10 words:
[0]:
aa
[1]:
bb
[2]:
cc
[3]:
dd
[4]:
ee
[5]:
ff
[6]:
gg
[7]:
hh
[8]:
ii
[9]:
kk
Enter another command for range 0 - 9:
smallest
The smallest word in this range is: aa.
And its index is: 0.
Enter another command for range 0 - 9:
range
Enter lower bound and upper bound so that 0 <= lower bound <= upper bound < 10:
3 5
Enter another command for range 3 - 5:
smallest
The smallest word in this range is: dd.
And its index is: 3.
Enter another command for range 3 - 5:
reverse
Enter another command for range 3 - 5:
display
[3]: 'ff'
[4]: 'ee'
[5]: 'dd'
Enter another command for range 3 - 5:
smallest
The smallest word in this range is: dd.
And its index is: 5.
8


or


Enter a command for range 0 - 9:
duck duck goose
Invalid command.
Enter another command for range 0 - 9:
quit
Quitting the program.

Explanation / Answer

#include <iostream>

#include <string>

using namespace std;



int main()

{

string words[10];

int lowerBound = 0;

int upperBound = 9;

string command, word, line, tmp;

int i, j, smallest;


cout << "Enter a command for range 0-9:" << endl;

cin >> command;

getline(cin, line); // skip this line


while (command != "quit")

{

if (command == "range")

{

cout << "Enter lower bound and upper bound so that 0 <= lower bound <= upper bound < 10:"

<< endl;

cin >> i >> j;

if (i < 0 || j >= 10 || i > j)

{

cout << "Invalid range, range stays as " << lowerBound << "-"

<< upperBound << "." << endl;

}

else

{

lowerBound = i;

upperBound = j;

}

}

else if (command == "display")

{

for (i = lowerBound; i <= upperBound; i++)

cout << "[" << i << "]: '" << words[i] << "'" << endl;

}

else if (command == "reset")

{

cout << "Enter a word:" << endl;

cin >> word;

getline(cin, line);

for (i = lowerBound; i <= upperBound; i++)

words[i] = word;

}

else if (command == "input")

{

cout << "Enter " << (upperBound-lowerBound+1) << " words:" << endl;

for (i = lowerBound; i <= upperBound; i++)

{

cout << "[" << i << "]:" << endl;

cin >> words[i];

getline(cin, line);

}

}

else if (command == "reverse")

{

for (i = lowerBound, j = upperBound; i < j; i++, j--)

{

tmp = words[i];

words[i] = words[j];

words[j] = tmp;

}

}

else if (command == "smallest")

{

smallest = lowerBound;

for (i = lowerBound; i <= upperBound; i++)

if (words[i] < words[smallest])

smallest = i;

cout << "The smallest word in this range is: "

<< words[smallest] << "." << endl;

cout << "And its index is: " << smallest << "." << endl;

}

else

{

cout << "Invalid command." << endl;

}


cout << "Enter another command for range " << lowerBound << "-"

<< upperBound << ":" << endl;

cin >> command;

getline(cin, line); // skip this line

}


cout << "Quitting the program." << endl;

return 0;


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote