Write a recursive function that gets an int n and another number f that is between 0 and 3, the function returns 0 or 1 depending on the following conditions:
If f=0, the function will return 1 if the sequence (from right to left) is an ascending series Otherwise it will return 0.
If f=1, the function will return 1 if the sequence (from right to left) is a descending series Otherwise it will return 0.
If f=2, the function will return 1 if the sequence (from right to left) is an ascending series up to a certain number and then it's descending (up to the end of the number) Otherwise it will return 0.
If f=3, the function will return 1 if the sequence (from right to left) is a descending series up to a certain number and then it's ascending (up to the end of the number) Otherwise it will return 0.
If the number n has two consecutive same numbers, for every f value the function will return 0. For example, for the number 1223 for any f value, the function will return 0.
For example:
For 1234,0 the function returns 0
For 4321,0 the function returns 1
For 1234,1 the function returns 1
For 4321,1 the function returns 0
For 12341,2 the function returns 1
For 412341,2 the function returns 0
For 96589,2 the function returns 0.
For 12341,3 the function returns 0
For 96589,3 the function returns 1
For the number 1223 for any f value, the function will return 0.
I wrote this code but the case for f=2 always returns 1.
#include <stdio.h>
int func(int n, int f)
{
int zero = 0;
int* one = &zero;
if (n < 10)
return n;
if((func(n / 10, f) == n % 10)
return 0;
switch (f)
{
case(0):
if (func(n / 10, f) > n % 10)
return (n % 10);
else
return 0;
case(1):
if (func(n / 10, f) < n % 10)
return (n % 10);
else
return 0;
case(2):
if (func(n / 10, f) > n % 10)
return (n % 10);
else
return 0;
if (*one == 0)
if (func(n / 10, f) < n % 10)
{
*one++;
return (n % 10);
}
case(3):
if (func(n / 10, f) < n % 10)
return (n % 10);
else
return 0;
if (*one == 0)
if (func(n / 10, f) > n % 10)
{
*one++;
return (n % 10);
}
}
}
int main(void)
{
//int n=1234, f=0; //should return 0
//int n=4321, f=0; //should return 1
//int n=1234, f=1; //should return 1
//int n=4321, f=1; //should return 0
//int n=12341, f=2; //should return 1
//int n=412341, f=2; //should return 0
//int n=96589, f=2;//should return 0
//int n=96589, f=3; //should return 1
//int n=12341, f=3; //should return 0
if (func(n , f) != 0)
printf("1
");
else
printf("0
");
return 0;
}