I'm a little confused here. Let's look at the following code:
bool testing(int i) {
if((i%2)==0) {
return true;
} else {
--i;
testing(i);
}
return false;
}
When I do testing(5), I was expecting the function to return true because at some point, 5 will become 4, so 4 % 2 == 0, so the function will return true but it just wasn't the case. What's wrong?
You should return testing(i); instead of just testing(i);
The idea of recursion is when a function calls itself, directly or indirectly.
The function in your code will become recursive if it is modified to:
bool testing(int i){
// test if even, if so return true
if((i % 2) == 0){
return true;
// otherwise decrement and test again
}else{
// at this point the function calls itself with decremented argument
return testing(--i);
}
// I doubt that this case will be ever returned
// more likely your function will return "true" or run "forever" decrementing
return false;
}
To avoid infinite cycles you need a base case, termination condition that produces result without recursion. For example if i becomes very small or negative you return false.
bool testing(int i){
// base case
if(i < 0) return false;
// rest of the function
if((i % 2) == 0){
return true;
}else{
return testing(--i);
}
}
Making it a bit more concise, you finally have three cases:
bool testing(int i){
// base case
if(i < 0) return false;
// test if even
if((i % 2) == 0) return true;
// recursion step
return testing(--i);
}
For further reading, check this
You don't bubble up the final return value; you need to use return on the recursive call. Additionally, you can simplify the pre-decrement:
return testing(--i);
Because of you only call testing(i) function. That's why it's not call recursively.
you should write return testing(i)
it returns false because its return value is overridden by the last statement of "return false".
Related
I've been trying to get a better understanding of recursive functions, and I was working on a problem trying to figure out why it wasn't working properly.
I want it to go through the array, and when it finds a 0, to return true.
But the function stills return true when there's no zero.
Here is my code:
bool containZero(int randomArray[], int length)
{
if(randomArray[length] == 0)
return true;
else
{
return containZero(randomArray, length - 1);
}
}
Your algorithm is not handling the possibility that the array may not actually contain a zero. You need to stop iterating when the end of the array is reached.
You are also accessing the array out of bounds from the very 1st iteration, and also if no zero is found.
Not only that, but there is no branch of your recursion logic that ever returns false. Your return statements are always returning true.
Try this instead:
// this searches backwards...
bool containZero(int randomArray[], int length)
{
if (length <= 0)
return false;
--length;
if (randomArray[length] == 0)
return true;
return containZero(randomArray, length);
}
Online Demo
Alternatively:
// this searches forwards...
bool containZero(int randomArray[], int length)
{
if (length <= 0)
return false;
if (randomArray[0] == 0)
return true;
return containZero(randomArray+1, length-1);
}
Online Demo
I was wondering what I may have done wrong in writing this simple function which is supposed to return true if the given number is a prime, or false if not a prime.
bool isPrime(int num)
{
if (num <= 1)
{
status = false;
}
else
{
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
{
dividers++;
}
}
if (dividers == 2)
{
status = true;
}
else
{
status = false;
}
}
return status;
}
Obviously, my main looks like this:
bool isPrime(int num);
bool status;
int dividers = 0;
int main() {
isPrime(2);
if (!isPrime)
{
std::cout << "Not prime" << std::endl;
}
else
{
std::cout << "Prime" << std::endl;
}
return 0;
}
I'm a C++ beginner and I'd really appreciate it if someone could help me there and correct my logic.
Have a good day:)
The immediate problem is in this two lines:
isPrime(2);
if (!isPrime)
The first line calls the function and discards the returned value. The second line converts a pointer to the function to bool. The output of your code does not depend on what you actually do in isPrime.
That is not how you call a function and use its result!
Instead you want
if (isPrime(2)) {
or
bool isP = isPrime(2);
if (isP) { ...
As mentioned in comments, there are also problems in the implementation of isPrime, but I hope this is enough to set you back on the right track.
PS: You should get rid of the global variable status. You do not need both, the return value and a global that stores the result, and if you can choose, you should definitely go for the return value.
I implemented the following GMP function for an RSA program. Basically, the program generates random mpz*t numbers until one of them returns true for this function.
bool isPrime(const mpz_t bignum)
{
mpz_t modnum; mpz_init(modnum);
if(mpz_cmp_ui(bignum,4)<0 && mpz_cmp_si(bignum,0)>=0) {
fprintf(stderr,"Trivially prime.\n");
return false;
}
else if(mpz_mod_ui(modnum,bignum,2)==0)
return false;
mpz_clear(modnum);
mpz_t i,rootnum;
mpz_inits(i,modnum,rootnum,NULL);
mpz_sqrt(rootnum,bignum);
mpz_set_str(i,"3",10);
for(;mpz_cmp(rootnum,i)>0; mpz_add_ui(i,i,2)) {
mpz_mod(modnum,bignum,i);
if(mpz_cmp(modnum,i)==0)
return false;
}
mpz_clears(modnum,i,rootnum,NULL);
return true;
}
Here is the function that calls isPrime() as a subroutine:
void generate_pq(mpz_t& p, mpz_t& q)
{
gmp_randstate_t rstate;
gmp_randinit_default(rstate);
gmp_randseed_ui(rstate,time(NULL));
printf("\nGenerating keys...\n");
do {
mpz_urandomb(p,rstate,32);
} while(!isPrime(p));
printf("\n***** p *****\n");
gmp_printf(" %Zd\n",p);
do {
mpz_urandomb(q,rstate,32);
} while(!isPrime(q));
gmp_randclear(rstate);
printf("\n***** q *****\n");
gmp_printf(" %Zd\n",q);
}
The program compiles and runs no problem. However, the numbers generated are not prime and yet isPrime() still returns true for them. Could anyone point out the flaw in the algorithm of my primality test? Here is the regular int version of my isPrime() function, if you'd like to compare:
bool isPrime(uint64_t n)
{
//waste of time
if(n < 4) {
fprintf(stderr,"Trivially prime.\n");
return true;
}
//even #, not prime
else if(n%2==0) {
return false;
}
//check if divisible by all odd #s < sqrt(n)
for(uint64_t i=3; i<(uint64_t)sqrt(n+1); i+=2) {
if(n%i==0) {
return false;
}
}
return true;
}
The error was in my for-loop.
for(;mpz_cmp(rootnum,i)>0; mpz_add_ui(i,i,2)) {
mpz_mod(modnum,bignum,i);
if(mpz_cmp(modnum,i)==0) // <-----not supposed to do if(modnum==i)
return false;
The correct version of this for-loop is here:
for(;mpz_cmp(rootnum,i)>0; mpz_add_ui(i,i,2)) {
mpz_mod(modnum,bignum,i);
if(mpz_cmp_ui(modnum,0)==0) // <-- if bignum % modnum == 0, return false
return false;
I'm trying to make function that has a loop that checks every member of an array made from boolean variables and exits when it finds the first "true" value.
That's what I have now:
bool solids[50];
int a,i;
//"equality" is a function that checks the equality between "a" and a defined value
solids[0] = equality(a,&value_1);
solids[1] = equality(a,&value_1);
solids[2] = equality(a,&value_1);
solids[3] = equality(a,&value_1);
for (i = 0; solids[i] != true; i++)
{
[...]
}
But I have no idea, what should I put into the loop?
My attempt was
for (i = 0; i <= 50; i++)
{
if (solids[i] == true)
{
return true;
break;
} else {
return false;
}
}
,that should return true after the first found true and return false if the array has no member with true value, but it doesn't seem to work in the code.
Is it wrong? If yes, what is the problem?
PS.: I may count the number of trues with a counter but that's not an optimal solve to the problem, since I just look for the FIRST true value and consequently, the program doesn't have to check all the 50 members. Needley to count, how many unnecesary steps should this solve would mean.
here's a short example usage of std::find() as advised by #chris:
bool find_element_in_array() {
bool solids[50];
int length;
/* ... do many operations, and keep length as the size of values inserted in solids */
bool* location = std::find(solids, length, true);
// if element is found return true
if (location != solids + length)
return true;
// else return false
return false;
}
Once you have solids correctly set (it looks like you're currently setting every value to the same thing), you can make a loop that exits on the first true like this:
for (i = 0; i < 50; i++)
{
if (solids[i] == true)
{
return true;
}
}
return false;
I'd also just move the declaration of i into the for loop body, since it's not used outside, but the above answers your question.
return immediately exits the function, so there is no need to break the loop after.
If it's sufficient to exit the function right after the search, you should write something like:
for (int i = 0; i < 50; i++) {
if (solids[i]) return true;
}
return false;
If you need to use the result of the search in the same function, use additional variable:
bool found = false;
for (int = 0; i < 50; i++) {
if (solids[i]) {
bool = true;
break;
}
}
if (found) { ...
I'm having trouble trying to figure out how to keep track of the amount of times my recursive function calls itself while performaing collatz function. I have the function definition:
template<class myType>
myType recursionSet<myType>::collatz(myType n)
{
if(n == 1)
return 1;
else {
if(n%2 == 1)
return collatz(3*n+1);
else
return collatz(n/2);
}
}
how can i keep track of the number of times this function calls itself? I cannot seem for the life of me to come up with a solution. Thanks!
reference to collatz function: http://www.xamuel.com/collatz-recursion/
You are trying to compute the length of the Collatz chain, aren't you. Do you realise that currently you always return 1? You should modify your code to return the count instead. That means adding the current iteration to the recursive call:
template<class myType>
myType recursionSet<myType>::collatz(myType n)
{
if(n == 1)
return 1;
else {
if(n%2 == 1)
return 1 + collatz(3*n+1);
else
return 1 + collatz(n/2);
}
}