Question

Your task is to write a program that will print out all the permutations of the command line arguments to a program. For examFor the output procedure called in the pseudocode, you should use the PrintArray method provided in the below skeleton (which

public class Permutations
{
    // Helper method for outputting an array.
    private static void PrintArray(string[] array)
    {
        foreach (string element in array)
        {
            Console.Write($"{element} ");
        }
        Console.WriteLine();
    }
    
    // Helper method for invoking Generate.
    private static void Generate(string[] array)
    {
        Generate(array.Length, array);
    }
        
    public static void Main(string[] args)
    {
    
    }
}
procedure generate(k : integer, A : array of any):
    if k = 1 then
          output(A)
    else
        // Generate permutations with kth unaltered
        // Initially k == length(A)
        generate(k - 1, A)

 
        // Generate permutations for kth swapped with each k-1 initial
        for i := 0; i < k-1; i += 1 do
            // Swap choice dependent on parity of k (even or odd)
            if k is even then
                swap(A[i], A[k-1]) // zero-indexed, the kth is at k-1
            else
                swap(A[0], A[k-1])
            end if
            generate(k - 1, A)

 
        end for
    end if
Your task is to write a program that will print out all the permutations of the command line arguments to a program. For example, given the arguments qaz, wsx, and 'edc', your program should output: wsx qaz edc wsx edc qaz edc wsx qaz Your implementation is expected to use Heap's algorithm according to the following pseudocode: procedure generate(k integer, Aarray of any): if k-1 then output CA) // Generate permutations with kth unaltered generate(k 1, A) // Generate permutations for kth swapped with each k-1 initial else // Initially k1ength(A) // Swap choice dependent on parity of k (even or odd) if k is even then swap (ATİ], A[k-1]) // zero-indexed, the kth is k-1 at else swap(ALO], A[k-1]) end if generate(k1, A) end for end if This algorithm uses recursion, which is when a procedure (e.g. method or function) calls itself from within its own code For the output procedure called in the pseudocode, you should use the PrintArray method provided in the below skeleton (which you will use as a basis for your program). Because the algorithm involves recursion, it is necessary for the recursive procedure generate to receive an initial k value equal to the length of the provided array. Therefore, an optional helper method Generate has been provided to wrap your implementation.
For the output procedure called in the pseudocode, you should use the PrintArray method provided in the below skeleton (which you will use as a basis for your program). Because the algorithm involves recursion, it is necessary for the recursive procedure generate to receive an initial k value equal to the length of the provided array. Therefore, an optional helper method Generate has been provided to wrap your implementation. public class Permutations // Helper method for outputting an array. private static void PrintArray(string] array) foreach (string element in array) Console.write(S"element" Console.WritelineO; // Helper method for invoking Generate private static void Generate(string] array) Generate(array. Length, array): public static void Main(stringl args) To complete the implementation, you will need to Optionally, but strongly recommended, impement an additional helper method that swaps two elements of an array. You will find this to be useful for your implementation of Heap's algorithm. Implement the generate procedure described in the above pseudocode (calling PrintArray in place of output) via a method with the following signature Generate(int, stringt] Connect Main with your Generate implementation.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

using System.IO;
using System;

public class Permutations
{
// Helper method for outputting an array.
private static void PrintArray(string[] array)
{
foreach(string element in array)
{
Console.Write($"{element} ");
}
  
Console.WriteLine();
  
}
  
// Helper method for invoking Generate.
private static void Generate(string[] array)
{
Generate(array.Length,array);
}
  
private static void Generate(int k, string[] array)
{
if(k==1)
       PrintArray(array);
   else
   {
   // Generate permutations for kth unaltered
   // Initially k == length(array)
  
   Generate(k-1,array);
  
   // Generate permutations for kth swapped with each k-1 initial
   for(int i=0;i<k-1;i++)
{
   // Swap choice dependent on parity of k(even or odd)
if(k%2 == 0)
{
swap(array,i,k-1); // zero indexed , the kth is at k-1
}else
{
swap(array,0,k-1);
}
Generate(k-1,array);
}
   }
}
  
private static void swap(string[] array, int i, int j)
{
   string temp = array[i];
   array[i] = array[j];
   array[j] = temp;
}
  
public static void Main(string[] args)
{

       Generate(args);
}
}

Output:

Input

Command Line arguments : qax wsx edc

Output:

qaz wsx edc wsx qaz edc edc qaz wsx qaz edc wsx wsx edc qaz edc wsx qaz

Add a comment
Know the answer?
Add Answer to:
Public class Permutations { // Helper method for outputting an array. private static v...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C# QUESTION Requirements Your task is to write a program that will print out all the s of the com...

    C# QUESTION Requirements Your task is to write a program that will print out all the s of the command line arguments to a program For example, given the arguments 'gaz, wx,and 'edc, your program should output wsxaazea Your implementation is expected to use Heap's algorithm according to the following pseudocode: procedure generatelk: integer, A: array of any): if k 1 then output A) else // Generate permutations with kth unaltered //Initially k length(A) // Generate permutations for kth swapped...

  • public class Test { private static int i =0; private static int j =0; public static...

    public class Test { private static int i =0; private static int j =0; public static void main(String[] args) { int i = 2; int k = 3; { int j =3; System.out.println("i + j is : " + i + j); } k = i + j; System.out.println("K is " + k ); System.out.println("K is " + j); } } why is the output i + j = 23 K =2 K =0 Please explain a step by step...

  • a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a...

    a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a collection of n values, where 0 < k < n. Write a program that uses a minheap method to find the kth smallest value in a collection of n values. Use the MinHeap class defined in part a. public final class MinHeap<T extends Comparable<? super T>>              implements MinHeapInterface<T> {    private T[] heap;      // Array of heap entries; ignore heap[0]    private int...

  • Part 1. Primitive Types, Sorting, Recursion for Homework.java a) Implement the static method initializeArray that receives...

    Part 1. Primitive Types, Sorting, Recursion for Homework.java a) Implement the static method initializeArray that receives as a parameter an array of integers. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. b) Implement the static method printArray that receives as a parameter an array of integers. Use a for statements to print all the elements in the array. c) Implement the static method insertionSort...

  • import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) {...

    import java.util.LinkedList; public class testprintOut { private static LinkedList[] array; public static void main(String[] args) { int nelems = 5; array = new LinkedList[nelems]; for (int i = 0; i < nelems; i++) { array[i] = new LinkedList<String>(); } array[0]=["ab"]; System.out.println(array[0]); } } //I want to create array of linked lists so how do I create them and print them out efficiently? //Syntax error on token "=", Expression expected after this token Also, is this how I can put them...

  • in java Part III: Permutations (10 points) Write a recursive method public static ArrayList<int[] > permuteArray...

    in java Part III: Permutations (10 points) Write a recursive method public static ArrayList<int[] > permuteArray (int[] array) that returns an ArrayList of all permutations of the the parameter array. The ArrayList may contain the ar- rays in any order. Example: Suppose array = {4, 7, 1, 2}. Then the ArrayList would contain the following arrays, but in any order: 4 7 12 7 4 1 2 2 1 4 7 1 24 7 4 7 2 1 7 4...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • Analyze the following code: public class Test { private int t; public static void main(String[] args)...

    Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } } The variable t is private and therefore cannot be accessed in the main method. The program compiles and runs fine. t is non-static and it cannot be referenced in a static context in the main method. The variablet is not initialized and therefore causes errors. The variable x is not initialized and therefore causes errors.

  • 4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array...

    4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...

  • Analyze the following code: public class Test { private int t; public static void main(String[] args)...

    Analyze the following code: public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } } t is non-static and it cannot be referenced in a static context in the main method. The program compiles and runs fine. The variable t is not initialized and therefore causes errors. The variable x is not initialized and therefore causes errors.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT