Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Probably a simple question

Options
  • 12-03-2010 3:45pm
    #1
    Registered Users Posts: 2,283 ✭✭✭


    Consider the following function that convert a positive decimal number to base 8 and displays the result.
    void displayOctal (int n)
    {
    if (n > 0)
    { if (n/8 >0)
    displayOctal(n/8);
    cout << n%8;
    } //end if
    } //end displayOctal
    Describe how the algorithm works. Trace the function with n = 100.

    Please describe to me how this algorithm works as I cannot see how this converts 100 to its octal equivalent. Yes I am a n00b :o


Comments

  • Closed Accounts Posts: 1,397 ✭✭✭Herbal Deity


    To convert a number to a different base, you divide your number by the base you want to convert to, and the remainder/modulus will be the least significant digit. Then you divide the result by the base again, and the remainder/modulus will be the second least significant digit, and so on until the result is 0.

    EDIT: I deleted half my post because I think I'm giving too much away. This is clearly homework.


Advertisement