Question

In: Computer Science

Write a program that will ask the user for three words (strings). Re-arranges them in ascending...

Write a program that will ask the user for three words (strings). Re-arranges them in ascending order by using a function (it should return void). Global variables are forbidden. Hint: You don't need to exchange the values among variables. You can just print them in the correct order. (C++ Please)

Example of Output:

Word 1: apple

Word 2: orange

Word 3: strawberry

--------

orange apple strawberry

Solutions

Expert Solution

Program:

#include <iostream>

using namespace std;

void orderingWords(string word1, string word2, string word3) {
    // Ascending order of words by comparing given three words
    if (word1 <= word2 && word2 <= word3)
    {
        // Printing the ascending order of words
        cout << "Word1: " << word1 << endl;
        cout << "Word2: " << word2 << endl;
        cout << "Word3: " << word3 << endl;
    }
    else if (word1 <= word3 && word3 <= word2)
    {
        cout << "Word1: " << word1 << endl;
        cout << "Word2: " << word3 << endl;
        cout << "Word3: " << word2 << endl;
    }
    else if (word2 <= word1 && word1 <= word3)
    {
        cout << "Word1: " << word2 << endl;
        cout << "Word2: " << word1 << endl;
        cout << "Word3: " << word3 << endl;
    }
    else if (word2 <= word3 && word3 <= word1)
    {
        cout << "Word1: " << word2 << endl;
        cout << "Word2: " << word3 << endl;
        cout << "Word3: " << word1 << endl;
    }
    else if (word3 <= word1 && word1 <= word2)
    {
        cout << "Word1: " << word3 << endl;
        cout << "Word2: " << word1 << endl;
        cout << "Word3: " << word2 << endl;
    }
    else if (word3 <= word2 && word2 <= word1)
    {
        cout << "Word1: " << word3 << endl;
        cout << "Word2: " << word2 << endl;
        cout << "Word3: " << word1 << endl;
    }
}

int main()
{
    string word1, word2, word3;
    // Taking input for three words
    cout<<"Enter 3 words : ";
    cin >> word1 >> word2 >> word3;
    // Calling method for ordering words in ascending order
    orderingWords(word1, word2, word3);

    return 0;
}

Output:


Related Solutions

Write a class that asks a user to enter three integers. Display them in ascending and...
Write a class that asks a user to enter three integers. Display them in ascending and descending order. Save the file as Sorting.java Again, Don’t forget to create the application/project  SortingTest.java Class that has the main method and an object to use the Sorting class.
c++ Write a program that will ask the user for three pairs of integer values. The...
c++ Write a program that will ask the user for three pairs of integer values. The program will then display whether the first number of the pair is multiple of the second. The actual work of making the determination will be performed by a function called IsMultiple that takes two integer arguments (say, x and y). The function will return a Boolean result of whether x is a multiple of y.
Write a program that ask the user for three integers. Use two functions to determine the...
Write a program that ask the user for three integers. Use two functions to determine the largest and smallest number of the three. One function will determine the largest number, and the other function will determine the smallest number. (6 points) In c++ using functions.
Write a program that prompts the user to enter a series of strings, but with each...
Write a program that prompts the user to enter a series of strings, but with each string containing a small integer. Use a while loop and stop the loop when the user enters a zero. When the loop has finished, the program should display: the number of user inputs (not counting the final zero input). the total of the integers in the strings entered. the average of the integers accurate to one decimal place. Any help is greatly appreciated, this...
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
How do I get my program to ask the user to re enter the correct information...
How do I get my program to ask the user to re enter the correct information if the information entered does not match the database records? When I run my program it does let me know that the information entered does not match the database records but it does not ask me to enter the information again. Please add code (in bold) in the proper area with short explanation package MailMeSQL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import...
How do I get my program to ask the user to re enter the correct information...
How do I get my program to ask the user to re enter the correct information if the information entered does not match the database records? When I run my program it does let me know that the information entered does not match but it does not ask me to enter the information again. package MailMeSQL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; import com.mysql.cj.jdbc.JdbcConnection; public class mailmeMain {    public static void...
Seasons Re-Visited Write a program that contains an array of strings in main(). Your array of...
Seasons Re-Visited Write a program that contains an array of strings in main(). Your array of strings will contain the seasons, “Winter”, “Spring”, “Summer”, and “Fall”. Your program will then contain a function that will use your seasons array to display a menu and ask the user to choose their favorite season and return that value to the main() function. Your function must include a data validation loop. The user must enter in a 1 for winter, a 2 for...
1. Write a program that will ask the user to enter a character and then classify...
1. Write a program that will ask the user to enter a character and then classify the character as one of the following using only IF-ELSE and logical operators. (50 points - 10pts for syntax, 10pts for commenting and 30pts for successful execution) • Integer • Lower Case Vowel • Upper Case Vowel • Lower Case Consonant • Upper Case Consonant • Special Character
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT