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.
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 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...
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
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
Write a program will ask the user for the annual income and the number of years...
Write a program will ask the user for the annual income and the number of years that the user has worked as their current job. The program will tell the user whether or not he or she qualifies for a loan based on the following condition: income is equal to or exceeds $35,000 or number of years on job is greater than 5 Do not use “nested if statements” in your solution. As an example of the output: What is...
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT