In: Computer Science
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.
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
// 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;
}