• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

need some help with c++ program

1student

Member
Hi

I wrote a small program, but have a problem with it, and don't find it... maybe u can halp me.

example - I enter the numbers: 132 56 1345 98424 56789.
What I have to get printed is: 321 65 5431 98765
Instead of it I get: 3 6 5 9 (The first digits, and not the whole number)...

Here is the program:
 
Without looking at your code, I would say something is wrong with your loop. Could be:

  • The program is leaving the loop too soon

    The print stmt is only being called after the first check.

I've never been good at looking at other peoples code, sorry.
 
First of all, the function are recursive.

Second, I run the program and saw that all the function work (I printed out all the actions the program does, and it does all of them, the problem is in what I get in the end - the first digits, and not the whole numbers..

Hope u understood..

Originally posted by: Dre
I've never been good at looking at other peoples code, sorry.

I understand..

anyone else...?
 
Does everything in the program have to be recursive? Because if not, it seems to me you're going about it the hard way here. In particular, the recursion in print_increased_nums is much less clear than a simple loop would be (and less efficient as well).

If I were tasked to write something like this, I'd most likely just sort the digits rather than go through all of the division/modulus/recursion code that you are using.
 
Okay. In that case, make sure you are actually retreiving the return value from all of your recursive functions properly. I could tell you exactly where to look, but I think you should be able to figure it out from here.
 
The problem isn't in print_increased_nums(). Look at the following line from inside recur_num():

recur_num (num/10, temp);

Recur_num() returns a long value, but you are discarding it after every one of these recursive calls, and only returning the value from the initial call made by print_increased_nums(). Hence you are only getting the one digit (the one found by the first call). Make sense?
 
hmm... I think I understand what u wrote.

I'll explain u what I thought when I wrote it...
I'll take as example the number 1234-
num=1234 and temp=0;
num>0 ->true
so: temp = 4
num = 123
I return it to the function.
and again:
num>0->true
temp=40+3
num=12
etc...

How could I solve the problem?



 
It doesn't do any good to calculate "temp = 40 + 3" when you never return that value to the caller. Put a test output of the variable "temp" after the recursive call to recur_num() and watch what happens to temp as the recursive calls "unwind". You need to do something with the long value that is being returned by each of the recursive calls. I won't tell you exactly what to do with it just yet though ... you should take another look at it first to see if you can figure out how to handle it for yourself.
 
I thought about something like this:

==I changed it and still have a problem==
Here is what I get:
Enter 5 numbers
1234 65463 8548 6362 626
4 43 432 4321 4800
8 84 845 8458 9395
6 62 626 694
END

---

What do you say?
 
Adding stuff to the return value of the recursive function isn't required, as you are passing all components of the number down to each level of recursion, so the value calculated for temp at the deepest level of recursion is the complete answer.

I have to apologize for something here: I didn't realize that you were actually using the cout statements in recur_num() for output. I thought they were just in there for debugging, since you were also doing a cout of the final return value from recur_num() back in the original calling function. Nonetheless, I think it is a lot simpler to not use cout in the recursive function, because of the way you are calculating the result. (The fact that you calculate the complete answer at the deepest level of recursion is exactly why I assumed that you were using the cout statements as debugging.

I also took a closer look at your original post, and your example is a little misleading. If I understand the problem from the comments in your source, you are only supposed to print the reversed number if the digits of the original number were in ascending order, is that right? If so, 132 should be skipped, rather than returned as 321 as your original post suggested.

Now that I hopefully understand the desired results, you also have a 2nd coding problem in the is_increased() function where it only detects the non-increased condition if the first two digits checked are in the wrong order. Once again, this is a case of not returning the result of the recursion process back through the recursion chain.

Since we've gone back and forth enough times now without quite having a meeting of the minds, here's my adjustments to your code so you can compare the methods and results. I'll comment the changed lines. I hope this helps you.
 
Back
Top