Sample pattern is given,
input : 16
output: 16 11 6 1 -4 1 6 11 16
If the input is 10, then program should print output as
10 5 0 5 10
Note: The above sequences decrement/increment by 5.
The challenge is to not declare any variables or loops. use only recursion.
I have tried with the following code.
void sequence(int input, int base){
input = input - (input > 0?5:-5); //main execution
printf("input:%d\n",input);
if(input == base)return;
sequence(input,base);
}
//for eg. input and base(initial Value) is 16. The above method recurse itself until input = base.
I can print upto this sequence (in Bold)
16 11 6 1 -4 1 6 11 16
How to complete the sequence. In the above method, in main execution line, i need to check condition as input = input - (input < 0?5:-5);
to print the remaining sequence. But i am not sure how can i do this without any variables or loops. Is there any algorithm available or any other better solution.
Some example code to my comment, which would match if it doesn't have to be strictly left- or right-recursive:
void sequence(int n)
{
printf("%d ", n);
if (n > 0)
{
sequence(n-5);
printf("%d ", n);
}
}
Further notes:
1.) This seems to be about functional programming , where a key concept is that you can never assign a variable ... see how it is avoided here. (Strictly speaking, it's not functional because of the printf
side effects)
2.) It's not strictly left- or right-recursive (meaning the recursion happens in the middle of evaluation), so it can't be easily transformed to something iterative.
It seems that the recursion should stop when it reaches 0
or below. So try this condition in you recursive function (reduced to only one argument):
void sequence(input) {
// print input
if (input > 0) {
// recursive call
sequence(input);
}
// print input again (sometimes...)
}
For the sake of symmetry, also the reverse version of Felix's solution can be used
void sequence(int n)
{
if (n > 0) {
printf("%d ", n);
sequence(n-5);
}
printf("%d ", n);
}
Both feature a kind of asymmetry that seems not to fit to the palindromic structure of the problem, and there is another problem: the blemish of a trailing space. So let's introduce to you a obvious solution that deals with these tiny(?) flaws:
void sequence(int n)
{
if (n > 0) {
printf("%d ", n); sequence(n-5); printf(" %d", n);
} else {
printf("%d", n);
}
}
Use two different functions to cout up and down:
void down(int input, int base) {
printf("input:%d\n",input);
if(input > 0) down(input - 5, base);
else up(input + 5, base);
}
void up(int input, int base) {
printf("input:%d\n",input);
if(input == base) return;
up(input + 5, base);
}
Start the calculation by calling down
.
#include <stdio.h>
using namespace std;
void printSequence(int input) {
printf("%d ", input);
if (input <= 0)
return;
printSequence(input - 5);
printf("%d ", input);
}
int main ()
{
int input = 0;
scanf("%d", &input);
printSequence(input);
return 0;
}
The base case
of your recursive code seems wrong. I think your code goes into infinite recursion the way it is. You can use a flag to achieve your sequence:
void sequence(int input, int base){
static int flag = 0;
//input = input - (input > 0?5:-5); //main execution, wrong
printf("input:%d\n",input);
input -= (flag == 0)?5:-5; //use flag for base case of sequence
if(input <= 0)
flag = 1;
if(input == base){
printf("input:%d\n",input);
return;
}
sequence(input,base);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.