Question

In: Computer Science

11.10 LAB: All permutations of names PLEASE ANSWER IN C++! Write a program that lists all...

11.10 LAB: All permutations of names

PLEASE ANSWER IN C++!

Write a program that lists all ways people can line up for a photo (all permutations of a list of strings). The program will read a list of one word names (until -1), and use a recursive method to create and output all possible orderings of those names, one ordering per line.

When the input is:

Julia Lucas Mia -1

then the output is (must match the below ordering):

Julia Lucas Mia 
Julia Mia Lucas 
Lucas Julia Mia 
Lucas Mia Julia 
Mia Julia Lucas 
Mia Lucas Julia 

----File: main.cpp----

#include <vector>
#include <string>
#include <iostream>

using namespace std;

// TODO: Write method to create and output all permutations of the list of names.
void AllPermutations(const vector<string> &permList, const vector<string> &nameList) {

}

int main(int argc, char* argv[]) {
vector<string> nameList;
vector<string> permList;
string name;

// TODO: Read in a list of names; stop when -1 is read. Then call recursive method.

return 0;
}

Solutions

Expert Solution

I added another parameter to AllPermutation function -"len" which represents the swapping position.

code in text-

#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
// Write method to create and output all permutations of the list of names.
void AllPermutations( vector<string> &permList, vector<string> &nameList,int len) {
    //when full swapping is done
    if (len == nameList.size()-1) {
        for (int i = 0; i < nameList.size(); i++) {
            cout << nameList[i] << " ";
            permList.push_back(nameList[i]);
        }
        cout << endl;
    }
    else
    {
        // for Permutations
        for (int i = len; i <= nameList.size()-1; i++)
        {

            // Swapping
            swap(nameList[len], nameList[i]);

            // Recursion called
            AllPermutations(permList,nameList, len+1);

            //back swapping
            swap(nameList[len], nameList[i]);
        }
    }
}
int main(int argc, char* argv[]) {
vector<string> nameList;
vector<string> permList;
string name;
// Read in a list of names; stop when -1 is read. Then call recursive method.
string a="0";//for -1;
while (a!="-1"){
    cin >> a;
    if(a!="-1"){
        nameList.push_back(a);
    }
}
int len=0;//for swapping while caluclating
AllPermutations(permList,nameList,len);
return 0;
}

screen shot of code

output-

If you like this answer please give an UPVOTE, if you have any query feel free to ask in comment section.


Related Solutions

C# PLEASE Lab7B: For this lab, you’re going to write a program that prompts the user...
C# PLEASE Lab7B: For this lab, you’re going to write a program that prompts the user for the number of GPAs to enter. The program should then prompt the user to enter the specified number of GPAs. Finally, the program should print out the graduation standing of the students based on their GPAs. Your program should behave like the sample output below. Sample #1: Enter the number of GPAs: 5 GPA #0: 3.97 GPA #1: 3.5 GPA #2: 3.499 GPA...
Write in C++ Write a program that accepts the names of three political parties and the...
Write in C++ Write a program that accepts the names of three political parties and the number of votes each received in the last mayoral election. Display the percentage of the vote each party received.   Be sure to provide labels (party name) and number-align your output values.
use linux or c program. please provide the answer in details. Write a program that will...
use linux or c program. please provide the answer in details. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally...
C++ Vector Write a program that allows the user to enter the last names of the...
C++ Vector Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, votes received by that candidate, and the percentage of the total votes received by the candidate. Assume a user enters a candidate's name more than once and assume that two or more candidates receive the same number of votes. Your program should output the...
Write a C program named as listLetterFreq.c that lists the frequency of the letters from the...
Write a C program named as listLetterFreq.c that lists the frequency of the letters from the input via ignoring the case sensitivity. For example, sample outputs could be like below. Please input a string: This is a list of courses. CSC 1010 - COMPUTERS & APPLICATION Here is the letter frequency: Letter a or A appears 3 times Letter b or B appears 0 times Letter c or C appears 5 times Letter d or D appears 0 times Letter...
Write an assembly language program that corresponds to the following C program ****Please give correct answer...
Write an assembly language program that corresponds to the following C program ****Please give correct answer using Pep/9 machine**** int num1; int num2; ;int main () { scanf("%d", &num1); num2 = -num1; printf("num1 = %d\n", num1); printf("num2 = %d\n", num2); return 0; }
(C++) All programs will be included! This lab gives you a little practice with linked lists...
(C++) All programs will be included! This lab gives you a little practice with linked lists ·Debug insertSorted() and implement sorted() in numberlist.cpp ·Hint: insertSorted() is just missing a few parts. What is in the function can work fine once you add the right code, though you are free to rewrite it ·You need to have main.cpp, numberlist.h and numberlist.cpp in this project, and if you are using Code::Blocks, remember to set the Build Options to indicate you want to...
Please if you are able to answer the question below: Write C++ program using native C++...
Please if you are able to answer the question below: Write C++ program using native C++ (you can use STL)  that produces Huffman code for a string of text entered by the user.  Must accept all ASCII characters.  Pleas explain how you got frequencies of characters, how you sorted them, how you got codes.
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT