C++ Directions/Requirements: A sample run of the program should look like this:
ID: 3605938 • Letter: C
Question
C++
Directions/Requirements:
A sample run of the program should look like this:
Enter a word: dog
Enter a word: Dogs
Enter a word: beaver
Enter a word: Cat
Enter a word: Mouse
Enter a word: rabbit
Enter a word: 0
Your sorted list is: Cat Dogs Mouse beaver dog rabbit
I'm having a really hard time right now trying to understand strings(syntax and application) so just showing me where to start would do wonders for me.
Store the words entered by the user in an array of strings (each element of the array is of type string) The maximum number of words for which the program needs to work must be set using the following constant (a global constant at the top of the program) const int NUM WORDS 10 Then, throughout the program wherever this size is needed, the constant must be used rather than a literal number You can assume that each word entered by the user will not contain any spaces, so the words can be read in using "cin >>". The program must stop prompting for further input if the maximum number of words is reached, or if the user enters "0" as one of the words. If the user enters "0" to end the input sequence, then the "O" must not be counted as one of the words sorted and later displayed Sorting of the words must be done using the Bubble Sort method, implemented in a function with this prototype void bubbleSort (string words[], int num); This function must sort the first num strings of the words array from "smallest" to "largest" as determined by the compare function (for any strings, whether of letters or not), which corresponds to alphabetical order if the strings are of letters Don't do anything special to account for upper- or lower-case letters - all upper-case letters will be considered as coming before all lower-case letters (this is known as lexicographic sorting)Explanation / Answer
The output will as expected you mentioned above. I included the comment above the lines where they are required. So do understand and comment for any further help. First of all understand and run the code. Good Luck :)
**CODE:
#include <iostream>
#include <string.h>
using namespace std;
const int NUM_WORDS = 10;
void bubbleSort(string words[], int num);
int main()
{
int count = 0;
string input, arr[NUM_WORDS];
// Iterating the loop until the input is 0
while(1){
cout << "Enter a word: ";
cin >> input;
// Check whether the input is 0 if yes break the loop
if(input == "0")
break;
else
arr[count] = input;
count++;
}
bubbleSort(arr, count);
return 0;
}
void bubbleSort(string words[], int num){
string temp;
// Bubble sort Algorithm
for (int j=0; j<num-1; j++)
{
for (int i=j+1; i<num; i++)
{
// Checking if the string is greater than
if (words[j] > words[i])
{
// Swapping the string. Moving the greater string to the far right
temp = words[j];
words[j] = words[i];
words[i] = temp;
}
}
}
cout << "Your sorted list is: ";
for (int j=0; j<num; j++)
cout << words[j] << " ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.