Need help!

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.

removeFirst code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace 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!
The assignment has a lot of hints. The "Base Case" is talking about row 6. The "Recursive Call" is about row 7.
I know, I wrote that code.
Good. What the row 6 should return?
What do you mean?
If the removeFirst would act as seven_up, what should the row 6 return instead of 0?
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) ) );
}
Sorry i didn't respond for a while. It would be helpful if you wouldn't mind giving an explanation. Thank you!
@ smac89
though, i didnt learn log, pow yet. we been using exponential with our own functions we create.
so far i have this but obviously it isn't functional.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int sevenUp(int n)
{
	if(n<7)
		return 8;
	return 10*removeFirst(n/10)+n%10;
}
int main()
{
	int x;
	cout<<"Enter a number ";
	cin>>x;
	cout<<sevenUp(x);
	return 0;
}

Last edited on
You don't need those. As the hint says, work from removeFirst().

Give us explanation on what the rows 6 and 7 do in it. You said that you wrote the code, so you should know.

Edit: Didn't see the latest code post while writing this.
Last edited on
@incognitocpp

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 )
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

unsigned int seven_up( unsigned int x )
{
	const unsigned int base = 10;
	unsigned int digit = x % base;

	if ( digit == 7 ) ++digit;

	return ( ( ( x /= base ) == 0 ) ? digit : base * seven_up( x ) + digit );
}

int main()
{
	unsigned int x = 777;

	std::cout << x << '\t' << seven_up( x ) << std::endl;

	return 0;
}
@vlad: Neat, if incognitocpp can explain it.

@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?
Last edited on
I need also this type of help so thanks a lots.....
Topic archived. No new replies allowed.