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, c3 does 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

in C please.

Solutions

Expert Solution

Code:-

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//following function prints all the possible paawords
void printPasswords(int m, char** arr,int n, char* ans)
{
   //if max allowed length of string is 0, return
   if(n==0)
   {
       return;
   }
   //print all the possible strings by adding a single character to existing ans string
   int i;
   for(i=0;i<m;i++)
   {
       //temp stores the answer fater adding a character at the end of ans and prints it
       char* temp = (char*)malloc(sizeof(ans));
       strcpy(temp,ans);
       strcat(temp,arr[i]);
       printf("%s\n",temp);
   }
   //calling recursion on all possible strings that can be generated
   for(i=0;i<m;i++)
   {
       char* temp = (char*)malloc(sizeof(ans));
       strcpy(temp,ans);
       strcat(temp,arr[i]);
       printPasswords(m,arr,n-1,temp);
   }
}
//driver function
int main(int argc, char *argv[])
{
   //assigns value of m
   int m = atoi(argv[1]);
   //assigns value on n
   int n = atoi(argv[argc-1]);
   //string array to store the characters passed as arguments
   char** arr =(char **)malloc(m*sizeof(char*));
   //initialising the answer string
   char* ans =(char*)malloc((n+1)*sizeof(char));
   ans = "";
   //initializing arr array
   int i;
   for(i=0;i<m;i++)
   {
       arr[i] = argv[i+2];
   }
   //calling print printPasswords
   printPasswords(m,arr,n,ans);
   return 0;
}

Input 1:-

Output 1:-

Input 2:-

Output 2:-

Please UPVOTE thank you...!!!


Related Solutions

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...
PLESE CODE IN C# not java RECURSION Objectives • Learn the basics of recursion – Part...
PLESE CODE IN C# not java RECURSION Objectives • Learn the basics of recursion – Part II (last week was Part I) Submission Guidelines: You will turn in 2 program files (one for each lab problem) Tasks This lab has two parts: Write a driver program that calls this method from the Main program. • • Write a driver program that calls this method from the Main program. Note: Your program name should match the name of your java /...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT