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

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...
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...
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 java simple document retrieval program that first asks the user to enter a single...
Write a java simple document retrieval program that first asks the user to enter a single term query, then goes through two docuements named doc1.txt and doc2.txt (provided with assignment7) to find which document is more relevant by calculating the frequency of the query term in each document. Output the conclusion to a file named asmnt7output.txt in the following format (as an example for query “java” and “problem”). The percentages in parenthese are respective supporting frequencies. java: doc1(6.37%) is more...
How many people/families use a financial planner vs how many “do it on their own”?   And...
How many people/families use a financial planner vs how many “do it on their own”?   And if you can find it, why they decided on that approach?
1. A question on a student survey asks: In a typical week, how many times do...
1. A question on a student survey asks: In a typical week, how many times do you eat at a fast food restaurant? The following data was then collected: No. of times per week        No. of students           5 or more                           2           4                                       2           3                                       3           2                                       6           1                                       0           0                                       3 A. Find the mode (number of times per week) for this data. B. Find the median (number of times per week) for this...
In your own word, what do you understand by the word "Civilization"? How many types of...
In your own word, what do you understand by the word "Civilization"? How many types of Constitutions can you think of?
2. Write a program C++ that asks the user for a number (not necessary to force...
2. Write a program C++ that asks the user for a number (not necessary to force any particular requirements). Write a function with the following signature: double square(double x) that returns the square of the user's number (x * x). 3. Write a C++ program that asks the user for an integer. Write a function that returns 1 of the number is even, and 0 if the number is odd. Use this function signature: int isEven(int x). 4. Write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT