Question

C# QUESTION Requirements Your task is to write a program that will print out all the s of the command line arguments to a pro

ConsoleWxiteline) // Helper method for invoking Generate. private static void Generate string array Generatelarrax leagth, ar

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 with each k-1 initial // Swap choice dependent on parity of k (even or odd) if k is even then swap(Al, Alk-1//zero-indexed, the kth is atk-1 else swap AlO], Alk-1) end if end for 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. public class Permutations /Helper method for outputting an array private static void PrintAccaystringl] array) foreach (string element in array) Console.Write(S elementł":
ConsoleWxiteline) // Helper method for invoking Generate. private static void Generate string array Generatelarrax leagth, array) public static void Mainstring ares To complete the implementation, you will need to: .Optionally, but strongly recommended, implement 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 Bri in place of output) via a method with the following signature: Generate(İnt, stringm 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);
}
   }
}
// Helper method to swap the ith and jth index element of 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)
{
string[] array = {"qaz","wsx","edc"};
Generate(array);
}
}

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:
C# QUESTION Requirements Your task is to write a program that will print out all the s of the com...
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
  • Public class Permutations { // Helper method for outputting an array. private static v...

    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 -...

  • In this assignment, you will write a Java program(s) to print the binary representation of a...

    In this assignment, you will write a Java program(s) to print the binary representation of a positive integer inserted from command line.  You must finish your assignment in 2 different ways: Using a recursive method Using an iterative method     Your main method must be: public static void main(String[] args) {      int input;         input = Integer.parseInt(args[0]);     print_recursion(input);     print_binary(input); }     You must implement a class and test your program by different input values, including 0. Comment your program properly Example of test...

  • Need help figuring this out Write a program that reads in a sequence of numbers (doubles)...

    Need help figuring this out Write a program that reads in a sequence of numbers (doubles) from standard input until 0 is read, and stores them in an array, This part is done using iteration (loop). You may assume that the maximum size of the array will not be more than 100. Your program computes the maximum number stored in the array, the count of negative numbers, and computes the sum of positive numbers, using recursion. You need to have...

  • Prelab Exercises Your task is to write a Java program that will print out the following...

    Prelab Exercises Your task is to write a Java program that will print out the following message (including the row of equal marks): Computer Science, Yes!!!! ========================= An outline of the program is below. Complete it as follows: a. In the documentation at the top, fill in the name of the file the program would be saved in and a brief description of what the program does. b. Add the code for the main method to do the printing. //...

  • 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...

  • In this assignment, you sort a list of strings using mergesort and the compareToIgnoreCase method of...

    In this assignment, you sort a list of strings using mergesort and the compareToIgnoreCase method of the String class. The strings are provided as arguments to your main method. In case you haven't yet learned to configure command-line arguments to main in your IDE, now is a good time to learn. Your program should sort the array of strings using mergesort, then print the strings one per line, in order from smallest ("a") to largest ("z"). The name of your...

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • PROJECT TASKS 1. Read the problem definition below and write a C++ program that implements your...

    PROJECT TASKS 1. Read the problem definition below and write a C++ program that implements your solution. Readability of the program will be graded based on variable naming, indentation, commenting (not too little and not too much), spacing, consistency, and styling in general including choice of functions. [NOTE: the code at the end of this document does not completely meet requirements as it uses many single letter variables and has no internal documentation. You need to improve it.] 2. Compile...

  • A test harness program for testing sorting methods is provided with the rest of the textbook...

    A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...

  • Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from...

    Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from a file that contain positive integers and should insert those numbers into the RB tree in that order. Note that the input file will only contain distinct integers. Print your tree by level using positive values for Black color and negative values for Red color Do not print out null nodes. Format for a node: <Node_value>, <Parent_value>). For example, the following tree is represented...

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