Question

In: Computer Science

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 command line arguments are always strings. You will have to convert them to integers or individual characters for your use.
    • The program execution will be similar to ./a.out M c1 c2 c3 ... N
    • M represents the number of allowable characters in the password.
    • Based on the value for M, the next set of arguments will be the allowable characters. For example, if M = 3, then the next 3 arguments will be the characters allowed in the password. c1 = 'a', c2 = 'b', c3 = 'c'. The values of c1, c2, c3does not have to be in alphabetical order. It can be any of the 26 characters.
    • Another example, if M = 2, then there will only be 2 arguments: c1 = 'x', and c2 = 'y'.
    • N represents the maximum length of the password.
    • Using the value of M create a dynamic array of characters. Copy the next set of arguments c1, c2, ... into the array.
  • Create a function called printPasswords().
    • This function will take in three parameters - N, M, and the array of allowable characters.
    • This function will not return anything.
    • This function will print all allowable passwords of length 1 <= length <= N, each on a separate line.
    • You can write as many helper functions as needed.

Following is an example output 1, when the program execution is ./a.out 3 a b c 2

a
b
c
aa
ab
ac
ba
bb
bc
ca
cb
cc

Following is an example output 2, when the program execution is ./a.out 2 a b 3

a
b
aa
ab
ba
bb
aaa
aab
aba
abb
baa
bab
bba
bbb

Solutions

Expert Solution

// C implementation


#include <stdio.h>
#include<math.h>
#include <stdlib.h>
#include <string.h>


void convert_To_Len_th_base(int n,   int arr[],   int len,   int L)
{
   for (int i = 0; i < L; i++)
   {
       printf("%d",arr[n % len]);
       n /= len;
   }
   printf("\n");
}

// Print permuataions


void print(int arr[], int len, int L)
{
   // There are  (len)^l permutations
   for (int i = 0; i < (int)pow(len, L); i++) {
       // Convert i to len th base
       convert_To_Len_th_base(i, arr, len, L);
   }
}

// Driver code


int main(int argc,char *argv[])
{   
//argv[0] is file name
  
int len=atoi(argv[1]); //number of charcters
   int arr[len];
   for(int i=2;i<argc-1;i++)
   arr[i-2]=atoi(argv[i]);
   int L = atoi(argv[argc-1]); //length of permutation

   // function call
   for(int i=1;i<=L;i++)
   print(arr, len, i);

   return 0;
}


Related Solutions

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...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
Code in C Instructions For this programming assignment you are going to implement a simulation of...
Code in C Instructions For this programming assignment you are going to implement a simulation of Dijkstra’s solution to the Dining Philosophers problem using threads, locks, and condition variables. Dijkstra’s Solution Edsgar Dijkstra’s original solution to the Dining Philosophers problem used semaphores, but it can be adapted to use similar mechanisms: • Each philosopher is in one of three states: THINKING, HUNGRY, or EATING. • Every philosopher starts out in the THINKING state. • When a philosopher is ready to...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a...
introduction: C PROGRAMMING For this assignment you will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
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...
IN C# WITH SCREENSHOTS OF THE CODE RECURSION Objectives • Learn the basics of recursion. Background...
IN C# WITH SCREENSHOTS OF THE CODE RECURSION Objectives • Learn the basics of recursion. Background There are many problems that loops simplify, such as displaying every pixel to a screen or receiving repetitive input. However, some situations that can be simplified with looping are not easily solvable using loops. This includes problems that require back tracking and being able to use information from previous iterations, which would normally be lost when using an iterative loop. In those cases, it...
Description: The purpose of this assignment is to write code that calculates the amount of interest...
Description: The purpose of this assignment is to write code that calculates the amount of interest accrued on a credit card on its expenses. You will create a C program that prompts the user for the total amount of purchases on the credit card, an annual percentage rate (APR), and the number of days the money is borrowed. The program will calculate the amount of the compound interest for the purchases, then print the original amount of purchases, the amount...
Purpose For this programming project, you will create an application that draws fractal-like shapes using recursion....
Purpose For this programming project, you will create an application that draws fractal-like shapes using recursion. The class that creates the graphics window for the display of the shapes is given to you (see below). What you have to do is create a set of classes for the three shapes that are displayed in the graphics window. Two of the shapes must be a Sierpinski triangle and an H-shape. The third shape is your choice, though it must be a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT