Hi I am new to cpp i am enrolled in a high school cpp course, I have only been in it for half a semester and I really can't get the grasp of recursion I mean I know what its for and how it works but I can't put the concept to work. I have multiple scenarios I need to solve with recursion but I'll just ask for the first and hope that sparks my mind to get the rest.
How would i write a recursive function that checks a number for the presence of a 9. Now I know that I need to use int division to divide the number by 10 in order to chop off the last digit(that is when using int to declare the variable) and also mod the number by 10 to seclude the last digit which would then be checked if it was a 9 but even with all that info I still can't figure out how to setup the function(I don't even know how many parameters I need other than the one to take in a number that is to be checked)
And yes I checked all the other topics about recursion and i still dont understand.
i can start it off and hope someone finishes it so i can study it and understand it because after that my next step is to add all the digits of a number.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<iostream>
usingnamespace std;
int nine(int num); //do i need another parameter?
void main()
{
int num;
cout<<"Enter a number: ";cin>>num;
cout<<nine(num)<<endl;
}
int nine(int num)
{
//and this is where im lost i know i need a base case but what...
}
I will give you an example of a solution to a different problem that perhaps might let you see how it works.
Search for a value in an array. The algorithm is intentionally suboptimal to better demonstrate recursion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
Returns the index of the first appearance of the value, or -1 if it doesn't
exist.
start: the first element to be checked.
end: the last element to be checked, inclusive, such the the range [start;end]
will be checked.
*/
int find(int *array,int start,int end,int value){
//First termination condition. True if the value has been found.
if (array[start]==value)
return start;
//Second termination condition. True if the value could not be found.
if (start>end)
return -1;
//Recurse the function. The next level will check start+1, or return -1.
return find(array,start+1,end,value);
}
Basically, a recursive function is one that calls itself with modified arguments. So you should have that function call itself, with one argument, so that it calls itself for the value of (n/10), where in the function it checks to see if n%10 is nine
well i get that function but well I don't understand why I can't comprehend recursion but I can do one type of problem and not another and right now im having trouble with this digit checker thing. Say i entered 1987 in as the num how do i go about checking the digits...what is my base case for that matter do....do i need another parameter to store the last digit that gets modded off so that can be checked... I get that people are reluctant to answer my direct problem because they dont want to do my work but i mean i desperately need to understand it and all the recursive functions ive looked at for help dont give me any ideas towards this one...and this is the basic that i need to build off of for about 10 other problems just today. i also need to understand it for a test tomorow unfortuntely