| | |
Recursive Function to Reverse an Integer
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I have an assignment to write a recursive function that takes one integer variable and output the integer in reverse order to the screen. I've tried converting it to a char array(c++ "string" class not allowed) but could not get a counter to work. I can reverse an int array with and "upper" and "lower" variable no problem, but I'm stuck without them.
???? reverse(int num) //This is what the assignment calls for.
I'm not looking for anyone to write it for me but I would appreciate some tips.
Thanks in advance.
???? reverse(int num) //This is what the assignment calls for.
I'm not looking for anyone to write it for me but I would appreciate some tips.
Thanks in advance.
Why would you need to use a char array? using int array should be fine.
It might be logically flawed somewhere..just make sure you have a base case (where you would stop calling the reverse function).
reverse (num) array[count] = num count ++ get a new num reverse (num) print out array[count] count--
It might be logically flawed somewhere..just make sure you have a base case (where you would stop calling the reverse function).
Last edited by freudian_slip; Nov 4th, 2008 at 12:54 am.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
If you're just displaying the reversed thing to the screen, there is no need to store any numbers in any arrays.
Each call to the function will print out a single digit (the digit of number in the units place). If the number is more than 9, the function will call itself with number/10 as the argument. This will recursively print out the number in reverse. When all digits have been printed, the function will stop, and return, due to the above stated call condition.
Each call to the function will print out a single digit (the digit of number in the units place). If the number is more than 9, the function will call itself with number/10 as the argument. This will recursively print out the number in reverse. When all digits have been printed, the function will stop, and return, due to the above stated call condition.
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
I'd do it that way too, just I'd add a little if-else-abs() trick to handle n<0 (otherwise it would print -567 reversed as -7-6-5).
something like
something like
C++ Syntax (Toggle Plain Text)
if(n<0) { cout << "-" << abs(n%10); revprint(n/-10); } else { cout << n%10; revprint(n/10); }
Last edited by mrboolf; Nov 4th, 2008 at 10:32 am.
![]() |
Other Threads in the C++ Forum
- Previous Thread: VERY BASIC C++ >> reading from a text file
- Next Thread: read a text from a text file
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






