hey guys im new to recursion to recursion..pls look at the function call ...question is how do i call in two functions at the same time..and which one should i return
thank you.

#include <iostream>
using namespace std;
bool canMakeChange(int total, int nums[], int length){
if(total==0)
    return true;
    
else
	 canMakeChange(total,nums,length-1);
 return canMakeChange(total-nums[length-1],nums,length-1);

}
int main() 
{
	const int TOTAL = 20;
	int coins[4]={10,5,5,10};
	if (canMakeChange(TOTAL, coins, 5))
		cout<<"true"<<endl;
		
	return 0;
}

hey guys im new to recursion to recursion..pls look at the function call ...question is how do i call in two functions at the same time..and which one should i return
thank you.

This code cannot return false. You need at least one line that says "return true" and one line that says "return false".

As far as calling two functions at the same time, you don't. Everything happens in order. You call one function, then you call another function. Not sure what you mean by that phrase.

Line 8 causes infinite recursion since total doesn't change. Line 9 will never be executed.

What is the logic of this function? What constitutes being able to "make change"? If the coins add up to the total? You'll need to elaborate on what this function does.

thanks for the reply..the logic is to find if it is possible to get correct change(total) by given coins(nums)....for example say u want 50 cent...(total) and nums are 10,10,10,20...thus bool would return true....there are two ways to check and see if the change(total could be made)..first is by taking out the last element from the array and adding the rest...second is taking of the last element and subtract that from the total. I been working on this all night but still couldnt figure it...any help be great...and another quick question since line 8 is infinit loop is it possible to run line 9 first and then line 8 would get the total from line 9???
and again thanks for all the help

thanks for the reply..the logic is to find if it is possible to get correct change(total) by given coins(nums)....for example say u want 50 cent...(total) and nums are 10,10,10,20...thus bool would return true....there are two ways to check and see if the change(total could be made)..first is by taking out the last element from the array and adding the rest...second is taking of the last element and subtract that from the total. I been working on this all night but still couldnt figure it...any help be great...and another quick question since line 8 is infinit loop is it possible to run line 9 first and then line 8 would get the total from line 9???
and again thanks for all the help

First, you need another base condition. When do you give up and decide you cannot make change? Test for it at the top and if it's impossible, return false.

I didn't say you could not call two functions. I said you cannot "call them at the same time". But you don't need to be able to do that.

bool method1 = canMakeChange(total,nums,length-1);
bool method2 =  canMakeChange(total-nums[length-1],nums,length-1);

if (method1 || method2)
    return true;  // if either method can give you correct change, then change can be made
else
    return false; // neither way worked

Thanks again for the reply..and finally the function make sense to me...however now every time i run the program it gets an error and stops the program...:(...if its possible for you can you pls try to run the program and see if it works..

#include <iostream>
using namespace std;
bool canMakeChange(int total, int nums[], int length){
	bool method1= canMakeChange(total,nums,length-1);
	bool method2= canMakeChange(total-nums[length-1],nums,length-1);
	
	if(method1 || method2)
		return true;
	else
		return false;


}
int main() 
{
	const int TOTAL = 20;
	int coins[4]={10,5,5,10};
	bool test =canMakeChange(TOTAL, coins, 4);
		if(test)	//cout true if change is possible
		cout<<"Yes, you can make change"<<endl;
                else
                   cout<<"Sorry cann't make change"<<endl;
		
	return 0;
}

i Just found one error...the length was incorrect...it says 5 and it should be 4..however program still dont run :/

Thanks again for the reply..and finally the function make sense to me...however now every time i run the program it gets an error and stops the program...:(...if its possible for you can you pls try to run the program and see if it works..

bool canMakeChange(int total, int nums[], int length){
	bool method1= canMakeChange(total,nums,length-1);
	bool method2= canMakeChange(total-nums[length-1],nums,length-1);
	
	if(method1 || method2)
		return true;
	else
		return false;
}

You took out the base cases. Without them, the function never returns. Put 'em back in and add one for false:

bool canMakeChange(int total, int nums[], int length){
    if(total==0)
        return true;

    if (/* some test */)
        return false;

    bool method1= canMakeChange(total,nums,length-1);
    bool method2= canMakeChange(total-nums[length-1],nums,length-1);
	
    if(method1 || method2)
        return true;
    else
        return false;
}

omg i found it out...i can check it with the length of array...because we only want to repeat untill we have elements in the array...and thank you so much for your help...lesson of the day...for every bool function need to have a true and false...:)....

if(length<0)
		return false;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.