Reversing a set of numbers using recursion

This is a discussion on Reversing a set of numbers using recursion within the C++ Programming forums, part of the General Programming Boards category; I have an algorithm here that'll let me compute the reverse of a given set of integers without using strings ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    Angry Reversing a set of numbers using recursion

    I have an algorithm here that'll let me compute the reverse of a given set of integers without using strings or arrays (which is the goal). However, I've no idea how to work recursion into this function. It's driving me up a wall.

    -----------------------------------------

    #include <iostream.h>
    #include <stdlib.h>
    #include <limits.h>

    void ReverseDigits(int number);

    int main()
    {
    char reply;
    int number;

    do
    {
    cout << "Enter a number between 0 and "
    << INT_MAX << ": ";
    cin >> number;
    cin.ignore(999, '\n');
    while (number < 0)
    {
    cerr << "Input error: value entered is not between 0 and "
    << INT_MAX << endl;

    cout << "Enter a number between 0 and "
    << INT_MAX << ": ";
    cin >> number;
    cin.ignore(999, '\n');
    }

    cout << "The number written in the usual way: " << number << endl;
    cout << "The number with its digits reversed: ";
    ReverseDigits(number);
    cout << endl;

    cout << "Do another customer? (n = no, any other = yes) ";
    cin >> reply;
    cin.ignore(999, '\n');
    }
    while (reply != 'n' && reply != 'N');

    cout << "Press Enter or Return when ready..." << endl;
    cin.get();
    return EXIT_SUCCESS;
    }

    // write definition for ReverseDigits

    // Here's where I get lost. How do I make this recursive?

    void ReverseDigits(int number)
    {
    int result = 0;

    while ( value % 10 )
    {
    result *= 10;
    result += value % 10;
    value /= 10;
    }
    }

  2. #2
    Unregistered
    Guest
    recursion occurs when the function calls itself until a predefined endpoint is reached (if not end point then you have frozen the computer, and that ain't good).

    So what's the endpoint? If number is < 10 I suppose.

    here's a basic recursive function outline

    returnType functionName(parameterList)
    {
    conditional statement to check for endpoint;
    if endpoint achieved do something (or nothing too sometimes);

    otherwise manipulate things if you need to here;
    call the funciton again; //re-curse it!
    do something else here if you want
    }

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    2

    Answer

    Answered my own question:

    void ReverseDigits(int number)
    {
    int temp;

    if(number > 1)
    {
    temp = number % 10;
    cout << temp;
    ReverseDigits(number/10);
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Replies: 8
    Last Post: 01-18-2008, 03:06 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21