Task:
Problem: Write a recursive function `seven_up` which takes a number and returns that number with all the sevens turned into eights.
- Example
cout<<seven_up(777)<<endl; //prints 888
cout<<seven_up(1234567890)<<endl; //prints 1234568890
cout<<seven_up(50)<<endl; //prints 50
- Hint: It's like removeFirst, except:
0. Base Case: If the number is one digit long, we don't want to erase it (by returning 0). Instead, riddle me this: When I have a one-digit number, what happens if I change all the 7's into 8's? Well, if the number is 7, it becomes 8, but otherwise it's unchanged.
1. Recursive Call: If the number is longer, then we strip off the last digit, figure out what the answer for the rest is (the recursive call on n/10), and then put the last digit back on the number when you're done.
Hint: You probably need to store the least digit (the n%10) and check if it's 7 separately.
#include <iostream>
usingnamespace std;
int removeFirst(int n)
{
if(n<10)
return 0;
return 10*removeFirst(n/10)+n%10;
}
int main()
{
int x;
cout<<"Enter a number ";
cin>>x;
while(x<10)
{
cout<<"Enter a number ";
cin>>x;
}
cout<<removeFirst(x);
return 0;
}
I don't understand how to solve this. I tried other algorithms but i dont get it. Please help me solve! it is due today and if possible, include a explaination!
If you are allowed to use strings to do this, you can just convert the number to string and use a for-loop
I wrote this one; the idea is to go through each individual number and do the search. If you want an explanation, I will gladly supply one.
1 2 3 4 5 6 7 8
int Seve_up(int num)
{
if ( num < 7 )
return num;
int G(log10(num)), f(pow(10, G)), k(num/f);
if ( k == 7 ) { num -= k * f; num += (8 * f); k = num/f; }//pull apart and stitch
return ( (k * f) + Seve_up( num - (k*f) ) );
}
log10 is a clever way of finding out the length of a number, kinda like you would do with strlen for strings. Since all our numbers are in base 10, the maximum power of 10 that could divide a number without making the number zero is [10 ^ (number of digits in the number - 1)]. So if you are able to find that power of 10, if you add 1 to it, you have the length of the digits...that is what log10 does.
The function you have created will not always work unless 7 is the last digit and the only 7 in the number you are working with. I don't understand what you mean by not being allowed to use log and pow; can you post other restrictions you have on this assignment also?
I tried your function, and I found what you are missing in your return statements. First off, your base case is wrong because if the number is 121, the function will return 128 and if the number is 1772, the function will return 1778. So as you can see, the function is returning 8 at the wrong place. Your base case should return n, because if n is less than 7 (i.e. n is 6,5,4,3,2,1,0), you are not concerned with those numbers, so just return them.
In your second return statement, you need to do something with that n%10. What happens if n%10 equal 7? So you have 2 choices when it comes to returning this recursively:
1: create a variable (t) that holds the value of n%10 and before your last return statement, check if this number is equal to 7, if it is, change it to 8, then in your return, you return 10 * removeFirst(n/10) + t
2: use the ternary operator to do the above directly in the return statement: return 10 * removeFirst(n/10) + ( n % 10 == 7 ? 8 : n%10 )
@incognitocpp
Recursive function. It splits work to two parts, makes one self and asks another copy of itself to do the rest. There must be an end condition that stops the recursion when the job can no longer be subdivided. Furthermore, when recursive call returns, the results are collected together.
Questions about removeFirst and seven_up:
* What is the job (data) that is divided?
* How is the job divided?
* What is the end condition?