Question

In: Computer Science

CS 400 Assignment 5 Recursive/Backtracking: Generating Permutations WRITE CODE IN C++ PROGRAMMING LANGUAGE WITH COMMENTS INCLUDED...

CS 400 Assignment 5
Recursive/Backtracking: Generating Permutations

WRITE CODE IN C++ PROGRAMMING LANGUAGE WITH COMMENTS INCLUDED

Description: 
Mimic the code for N-queen problem (https://introcs.cs.princeton.edu/java/23recursion/Queens.java.html),
develop a program that generates all permutations for the set {1, 2, 3, 4, 5}.
The output should contain all 5! = 120 permutations. 

Output Sample: 
P#1: 1 2 3 4 5 
P#2: 1 2 3 5 4
P#3: 1 2 4 3 5
...
P#120: 5 4 3 2 1

Hint:
- Thoroughly study the N-queen problem first. 
- The code for N-queen is very close to the code generating permutations, just some helper functions away. 
- Keep the structure of the enumerate() function. You may change the function head to take in the array size. 
- Implement printPermutation() function and use it to replace printQueens().
- Implement isConsistent() function for permutation check. This check should be easier than N-queen consistent check. 

File_name: 
- permutation.cpp

Grading: 
- compilable and meaningful attemps: 20 points. 
- structure of enumerate(): 30 points
- helper functions: 40 points.
- comments, file names and indentation: 10 points. 

Solutions

Expert Solution

#include <iostream>

using namespace std;

void printPermutation(int q[], int n) {
        for(int i=0; i<n; i++) {
                cout << q[i] << " ";
        }
        cout << endl;
}

bool isValid(int q[], int k, int n) {
        int counts[n+1];
        for(int i=0; i<=n; i++) {
                counts[i] = 0;
        }
        for(int i=0; i<k; i++) {
                if(counts[q[i]] > 0) {
                        return false;
                }
                counts[q[i]] += 1;
        }
        return true;
}

void enumerate(int q[], int n, int k) {
        if (k == n) printPermutation(q, n);
        else {
            for (int i = 1; i <= n; i++) {
                q[k] = i;
                if(isValid(q, k+1, n)) {
                        enumerate(q, n, k+1);
                }
            }
        }
}  

int main() {
        int n;
        cout << "Enter n: ";
        cin >> n;

        int data[n];
        enumerate(data, n, 0);
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that...
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates to a new function. Both f and g will be functions that take one parameter and evaluate to some result. The returned function should be the composition of the two functions with f applied first and g applied to f's result. For example (combine add1 sub1) should evaluate to a function equivalent to (define...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing...
In C/C++ programming language, write down the lines of codes (and figure) to show- a) assignment-by-sharing b) dangling reference
Code in C++ programming language description about read and write data to memory example.
Code in C++ programming language description about read and write data to memory example.
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write...
Programming II: C++ - Programming Assignment Vector Overloads Overview In this assignment, the student will write a C++ program that overloads the arithmetic operators for a pre-defined Vector object. When completing this assignment, the student should demonstrate mastery of the following concepts: · Object-oriented Paradigm · Operator Overloading - Internal · Operator Overloading - External · Mathematical Modeling Assignment In this assignment, the student will implement the overloaded operators on a pre-defined object that represents a Vector. Use the following...
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment,...
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random number between 0 and the...
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT