Skip to content

Commit 0d0fa6f

Browse files
authored
Add files via upload
Generating sequence of a given length using recursion
0 parents  commit 0d0fa6f

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

p.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//# program to generate a sequence of length l from the elemnts in array A
2+
3+
4+
#include<iostream>
5+
#include<bits/stdc++.h>
6+
using namespace std;
7+
void generate_sequence(int * A, int n, int len,int *B,int k){
8+
if(len==0) {
9+
for(int i=0;i<k;i++)
10+
cout<<B[i]<<" ";
11+
cout<<endl;}
12+
else{
13+
int i=0;
14+
while(i<n){
15+
B[k-len]=A[i];
16+
generate_sequence(A,n,len-1,B,k);
17+
i++;
18+
}
19+
20+
}
21+
22+
23+
}
24+
25+
int main()
26+
{
27+
28+
//n=size of array A;
29+
// Array A contains the integers which we are going to use to create sequence
30+
//len = the length of sequence you wish to create
31+
// B = the array where the created sequence will be stored
32+
// k= size of array B, k=len (inital value of len )
33+
34+
35+
36+
37+
int k,len,n;
38+
cin>>n>>len;
39+
k=len;
40+
int *A=(int *)malloc( n*sizeof(int));
41+
int *B=(int *)malloc( k*sizeof(int));
42+
cout<<"Enter "<<n <<" array elements \n";
43+
int i=0;
44+
for(i;i<n;i++)
45+
cin>>A[i];
46+
generate_sequence(A,n,len,B,k);
47+
48+
return 0;
49+
}

0 commit comments

Comments
 (0)