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

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

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

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

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

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

  • Your task is to write a command-line program that helps to decrypt a message that has been encrypted using a Caesar cipher1 . Using this method, a string may contain letters, numbers, and other ASCII characters, but only the letters (upper- and lower-case

    When your program is correctly run, it will be provided two strings: the encrypted message and a string that is in the deciphered text. If fewer/more arguments are provided, your program is to output an error:$ java edu.wit.cs.comp1050.PA4aPlease supply correct inputs: : <encrypted string> <substring>$ java edu.wit.cs.comp1050.PA4a aPlease supply correct inputs:  : <encrypted string> <substring>$ java edu.wit.cs.comp1050.PA4a a b cPlease supply correct inputs:  : <encrypted string> <substring>If the correct arguments are supplied, you are to output any shifts (00-25) that contain the...

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...

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
ADVERTISEMENT