Question

In: Computer Science

Asks the user "How many parrots do you own?"         2: Reads in that many single...

Asks the user "How many parrots do you own?"

        2: Reads in that many single word names (one for each parrots) from the user and stores the names in an array.

        3: After the user has finished entering names, the program should display the list of all the parrots the user entered.

        4: then display the parrots  in alphabetical order for part 3. Note: string objects can be compared with the “<” operator, which will determine if a given string is alphabetically less than another string.

You may assume the user will own at most 100 parrots.

c++

"output"

Welcome! How many dogs do you own?

- 4

Ok, enter the names!

> james

> flyingmo

> homer

> kitty

these are the names you entered: flyingmo, homer, james, kitty

Solutions

Expert Solution

Dear student,

The solution to the above problem statement using C++ programming language is as follows:

CODE:

#include <iostream>

using namespace std;

void swap(string *x, string *y)  //used to swap values between two positions in an array
{  
    string temp = *x;  
    *x = *y;  
    *y = temp;  
}  


int main()
{
    int numPar;
    cout<<"Hello, How many parrots do you own?\n";
    cin>>numPar;
    string parNames[numPar];
    for(int i=0;i<numPar;i++) //loop to take input from user one by one
    {
        cin>>parNames[i];
    }
    
    //Now we will use bubble sorting technique to sort the names
    
    int x = sizeof(parNames)/sizeof(parNames[0]);
    
    for (int i = 0; i < x-1; i++)
    {
    for (int j = 0; j < x-i-1; j++)
       {
        if (parNames[j] > parNames[j+1])  
        swap(&parNames[j], &parNames[j+1]);  //calls the swap function defined above
        }    
    }
    
    cout<<"The names entered by you in ascending order are:\n";
    
    for(int i=0;i<numPar;i++)
    {
        cout<<parNames[i]<<"\n";
    }

    return 0;
}

-------------------------------------------------------------------------------------------------------------------------------------------------

SAMPLE OUTPUT:

-------------------------------------------------------------------------------------------------------------------------------------------------

I hope the given solution helps clear your doubt.

Don't forget to give it a thumbs up.

Regards


Related Solutions

Asks the user "How many parrots do you own?"         2: Reads in that many single...
Asks the user "How many parrots do you own?"         2: Reads in that many single word names (one for each parrots) from the user and stores the names in an array.         3: After the user has finished entering names, the program should display the list of all the parrots the user entered.         4: then display the parrots  in alphabetical order for part 3. Note: string objects can be compared with the “<” operator, which will determine if a...
Write a program that asks a user how many classes they are taking this term, the...
Write a program that asks a user how many classes they are taking this term, the name of each class, the credit hours for each class, and their final letter grade for each class, and then calculates their term GPA. Use either a list for each course or a single list-of-lists. The grading system and examples can be found on the TAMU website: http://registrar.tamu.edu/Transcripts-Grades/How-to-Calculate-GPA
Write a program that asks the user for their filing status (single or married) and their...
Write a program that asks the user for their filing status (single or married) and their taxable income. Then, using the below table, compute the tax owed and display the filing status, taxable income, and tax owed. If the filing status is single and the taxable income is overbut not over   the tax is of the amount over$0$9,52510%0$9,526$38,700$952.50 + 12%$9,525$38,701$82,500$4,453.50 +22% $38,700 $82,501unlimited$14,089.50 +24% $82,500 If the filing status is married filing jointly and the taxable income is overbut not overthe...
C++. Write a program that asks the user to enter a single word and outputs the...
C++. Write a program that asks the user to enter a single word and outputs the series of ICAO words that would be used to spell it out. The corresponding International Civil Aviation Organization alphabet or ICAO words are the words that pilots use when they need to spell something out over a noisy radio channel. See sample screen output for an example: Enter a word: program Phonetic version is: Papa Romeo Oscar Golf Romeo Alpha Mike The specific requirement...
A survey asks: On an average evening, how many hours do you spend online sur ng...
A survey asks: On an average evening, how many hours do you spend online sur ng social media? Identify two issues with the wording of this question which could be improved in order to make its response more useful.
​​​​​ 1 Do you own a pet? 2 How old are you? 3 How much do...
​​​​​ 1 Do you own a pet? 2 How old are you? 3 How much do you weigh? (lbs) 4 Male or Female? 5 Do you drink alcohol? 6 How often do you watch movies at the theater per month? 7 How often do you eat fast food per week? 8 Do you like spicy food? 9 Do you prefer Android or iOS? 10 Do you prefer Windows of Mac? 11 How many times a year do you go on...
Write a C# console program that continually asks the user "Do you want to enter a...
Write a C# console program that continually asks the user "Do you want to enter a name (Y/N)? ". Use a "while" loop to accomplish this. As long as the user enters either an upper or lowercase 'Y', then prompt to the screen "Enter First and Last Name: " and then get keyboard input of the name. After entering the name, display the name to the screen. --- Create a Patient class which has private fields for patientid, lastname, firstname,...
Write a C# console program that continually asks the user "Do you want to enter a...
Write a C# console program that continually asks the user "Do you want to enter a name (Y/N)? ". Use a "while" loop to accomplish this. As long as the user enters either an upper or lowercase 'Y', then prompt to the screen "Enter First and Last Name: " and then get keyboard input of the name. After entering the name, display the name to the screen.
Write a C# console program that continually asks the user "Do you want to enter a...
Write a C# console program that continually asks the user "Do you want to enter a name (Y/N)? ". Use a "while" loop to accomplish this. As long as the user enters either an upper or lowercase 'Y', then prompt to the screen "Enter First and Last Name: " and then get keyboard input of the name. After entering the name, display the name to the screen.
Write a C++ program that asks the user to enter a series of single-digit numbers with...
Write a C++ program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program should display the sum of all the single-digit numbers in the string. For example, if the user enters 2514, the program should display 12, which is the sum of 2, 5, 1, and 4. The program should also display the highest and lowest digits in the string. It is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT