I have an assignment for my C++ class that has me completely stumped. Before I go any further, I am not looking for any code or solutions, just hints and tips that might guide me in the right direction.
I am to write a recursive type void function that takes in an integer and displays it backwards. For example, if I input '12345', the output should be '54321'.
The only hint that my professor would give me was to use division and the modulus operator. Unfortunately, I am still stuck...
If anybody could help me out, your help would be greatly appreciated. Once again, I am not asking for code, just guidance.
You could use %10 to get the first digit. Subtract the first digit and use %100 for the second... and so on. You could call the function once for each digit until the end of the number.
A resursive function doesn't have to return anything.
In your case, the function is supposed to print (use cout), so you are all set. Just remember how to split a number up using % and /.
This is very much the same question as here: http://www.cplusplus.com/forum/general/31200/#msg168879
You aren't counting anything. You are just printing what you find in the one's place, then recursing until there isn't any number left.
Once you get that working, you might want to think about a special consideration: what would happen if the user called your function with an argument of zero?
Thanks for the help, guys, but I am still completely clueless with this problem... One thing that I am wondering, however is if my base case is wrong (which I'm guessing it probably is). Right now my base case is:
1 2
if (x <= 0)
cout<<x;
Is that close to being right?
Thanks for your help once again, and sorry for not picking up on this.