Question

In: Computer Science

Write a C++ program that asks for the name and age of three people.   The program...

Write a C++ program that asks for the name and age of three people.   The program should then print out the name and age of each person on a separate line from youngest to oldest.

Hint: Start with a program that just asks for the names and ages then prints them out in the same order they are entered. Then modify the program so it prints out the list of names in every possible order.    Note that one of these will be the correct result. Then add conditions to each set of results so that only one set of results is true.

For example; part of my program will print:

name2
name1 

name3

Therefore the condition for this output would be:  (age2 < age1) && (age2 < age3) && (age1 < age3)

You can assume that all three ages will be different but consider what your program would do if two or more ages were the same. (If your program is written correctly - it should not matter)

Use if, else if and else statements in order to minimize the total number of comparisons. (See Rubric for how you will be graded on use of conditionals)

HINT: Plan out you approach ahead of time. Use pseudocode or flow chart if that helps.

Example Output:

Peter   2
Paul    20
Mary    54

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

using namespace std;

int main(){

                //declaring needed variables

                string name1,name2,name3, tempStr;

                int age1, age2, age3, tempInt;

                //prompting and reading names and ages

                cout<<"Enter name and age of first person: ";

                cin>>name1>>age1;

                cout<<"Enter name and age of second person: ";

                cin>>name2>>age2;

                cout<<"Enter name and age of third person: ";

                cin>>name3>>age3;

                //we are now arranging names and ages, so that age1 <= age2 <= age3

                //if age1>age3, swapping age1 & age3, also name1 and name3

                if(age1>age3){

                                //swapping age1 & age3 using a temporary variable

                                tempInt=age1;

                                age1=age3;

                                age3=tempInt;

                                //swapping name1 and name3 using a temporary variable

                                tempStr=name1;

                                name1=name3;

                                name3=tempStr;

                }

                //if age1>age2, swapping age1 & age2, also name1 and name2

                if(age1>age2){

                                tempInt=age1;

                                age1=age2;

                                age2=tempInt;

                                tempStr=name1;

                                name1=name2;

                                name2=tempStr;

                }

                //now if age2>age3, swapping age2 & age3, also name2 and name3

                if(age2>age3){

                                tempInt=age2;

                                age2=age3;

                                age3=tempInt;

                                tempStr=name2;

                                name2=name3;

                                name3=tempStr;

                }

                //now the elements must be in sorted order of ages, simply displaying names and ages

                cout<<name1<<" "<<age1<<endl;

                cout<<name2<<" "<<age2<<endl;

                cout<<name3<<" "<<age3<<endl;

                return 0;

}

/*OUTPUT*/

Enter name and age of first person: Oliver 23

Enter name and age of second person: John 29

Enter name and age of third person: Barry 17

Barry 17

Oliver 23

John 29


Related Solutions

Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
Write a java program which asks the user to enter name and age and calls the...
Write a java program which asks the user to enter name and age and calls the following methods: printName(): Takes name as parameter and prints it 20 times using a while loop. printAge(): Takes age as parameter and prints all the numbers from 1 up to age. Write a java program that will print first 10 multiples of 3 in a single line.
C++ Write a program that asks a teacher to input a student’s first name, last name,...
C++ Write a program that asks a teacher to input a student’s first name, last name, and four test scores. The program should find the average of the four test scores and should then write the following information to a file named “students.txt” last_name first_name average A student's first name of “XX” should be used as a sentinel value and no numeric grades less than 0 or greater than 100 should be accepted.  The program should then read the information in...
Write a C program that loops and asks you every time to enter the name of...
Write a C program that loops and asks you every time to enter the name of a text file name, then it will read the file and perform statistics on its contents and display the results to the screen and to an output file called out.txt. The input files can be any text file. The program should calculate these integers as it reads character by character as we did in the last class example filecopy and make use of the...
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Write a C program that performs the following: Asks the user to enter his full name...
Write a C program that performs the following: Asks the user to enter his full name as one entry. Asks the user to enter his older brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have any older brother). Asks the user to enter his younger brothers’ names each at a time (the user should be instructed by the program to enter NULL if he does not have...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Write a C++ program that asks the user for the values of three resistances. We suppose...
Write a C++ program that asks the user for the values of three resistances. We suppose that the user will not enter a negative value. The program then asks the user whether he has a series or parallel montage. The user will answer with S or s for series and P and p for parallel. (If the user enters a wrong answer, he will see a message on the screen alerting him, and the program will end.) Finally, the program...
Program specifics: Write a C++ program that does the following: a. Asks the user for the...
Program specifics: Write a C++ program that does the following: a. Asks the user for the distance to the pin and the depth of the green (both in yards). (Note: The pin is the hole in the green and the depth is the diameter of the green, assuming it is circular.) b. Asks the user for an integer club number from 2 to 10, where 10 is the pitching wedge (this club lifts the ball out of rough, sand, etc)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT