Question:
Model using a mathematical function and design a recursive program that, given two non-negative integers m and n, indicates whether the first number m is strictly greater than the second number n, using only the equality comparison operator (==), the successor function (add 1), the predecessor function (subtract 1) and the conditional structure (if, if-else).
I don't understand what would be the base case to do it this is what I tried but it doesn't work.
#include <iostream>
using namespace std;
bool mayor(int m, int n);
int main()
{
int m;
int n;
cout<< mayor(m,n) << endl;
return 0;
}
bool mayor(int m, int n){
if(m==n){
return 0;
}
else{if(m==mayor(m,n+1)){
return 0;
}else{
return 1;
}
}
}
Answer:
One more answer: I took the key from the statement:
Given two nonnegative integers m and n, state whether the first number m is strictly greater than the second number n. therefore the comparison value 0 is valid as long as they have to be non-negative
and also
only the equality comparison operator (==), the successor function (add 1), the predecessor function (subtract 1), and the conditional structure (if, if-else)
Since I had to use both, the answer I came up with simulates a mutual rapprochement.
for example if the numbers are 10 and 5 for each call the numbers would get closer
major (10, 5);
(10, 5) -> 9 == 5? not -> 9 == 6? no
call greater(9,6);
(9, 6) -> 8 == 6? not -> 8 == 7? no
call major(8,7)
(8, 7) -> 7 == 7? if return true;
and in case greater is equal to or less than less, greater would have to be subtracted to zero, in which case it returns false
#include <iostream>
bool mayor (int m, int n);
int main ()
{
int m ;
int n ;
std::cout<< mayor(m,n) << std::endl;
return 0;
}
bool mayor (int m, int n)
{
if (m == 0)
return false;
else
{
m--;
if ( m == n ) return true;
else
{
n++;
if (m == n) return true;
else return mayor (m,n);
}
}
}