Thread: Power of N + Overcome Size Limitations

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Power of N + Overcome Size Limitations

    Hi, i am writing a program that asks the person for a number and then will out put that number by the power of 2. so 2 ^ 6 = 64. This program needes to be created without using the math functions all the maths have to be done programmaticaly.

    i know i am prob wrong because i cant get anywhere with this program. here is the code so far.

    Code:
    #include <iostream>
    
    using namespace std;
    int power;
    int n;
    int counter;
    int main ()
    
    {
    
    cout <<"Please enter a power number";
    
    cin >> n;
    
    counter = 2;
    
    if(counter < n)
    {
    counter*2;
    }
    
    if(counter == n)
    {
    counter * n;
    }
    
    cout <<counter;
    
    system("pause");
    }
    the other problem i have is once i have the power to work i need to allow the program to overcome size limitations and show all the numbers. for example i need

    Code:
    2^200 = 1606938044258990275541962092341162602522202993782792835301376.
    if anyone can help me with this then thanx soo much as i am finding this program hard to complete.

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    This will be a very hard problem for you*, because it seems as if you're a complete beginner. My advice is to study some example code and try to make some easier programs first, so you learn the basic concepts.

    *) the last problem that is, the first one is quite easy. To solve the first one you should use a loop.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You need 2 variables - the counter, that you input, and a power variable, which will hold the output.

    Edit: The second problem is much harder - you're trying to implement arbitrary-precision arithmetic.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You'll probably want to use a loop.

  5. #5
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    i have gone through all of the sheet on the basics n then this is the next question, lol. i am really stuck on it as you can prob tell.

  6. #6
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    can anyone give me any pointers on how to overcome the size limitations?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by peckitt99
    can anyone give me any pointers on how to overcome the size limitations?
    If you're having trouble with the first part, then forget about the second part. It's orders of magnitude harder. I doubt if your instructor expected anyone to actually finish that part.

    Edit: Actually, when I think about it, if the only arithmetic you have to implement is powers of 2, it's not nearly as bad as the general case. Don't get me wrong, it's still much harder than the first part, but still doable with some work. The general idea for arbitrary-precision arithmetic is that you represent numbers by strings instead of built-in numerical types, then implement by hand the rules of arithmetic you need, since the computer won't do it for you. I still strongly suggest you not think about it until after completing the first part.
    Last edited by robatino; 10-16-2006 at 03:01 PM.

  8. #8
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    not done the first part cause i cant seem to get it right for some reason.

    i dont know about the size limitations and didnt know if it was that hard or what it involved.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    HINT:

    Inside your loop, you are going to need something like this:

    x = x * 2; // Do this n times in a loop

    Don't forget to initialize x before entering the loop.

    A type unsigned long int will (probably) hold bigger numbers than a regular int. But, it's not going to hold 2^200 !!!
    Last edited by DougDbug; 10-16-2006 at 05:11 PM.

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by robatino
    Edit: The second problem is much harder - you're trying to implement arbitrary-precision arithmetic.
    It's trivially easy if you output the numbers in hexadecimal
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  11. #11
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah, still trying to get the first bit done, just not happeneing, taking in all the advice but dont think i am implementing it in correctly because i dont get the right output etc.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Cat
    It's trivially easy if you output the numbers in hexadecimal
    I don't know. You still have to break the string into blocks and handle the carries properly. Offhand it seems tricky.

  13. #13
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    A power of 2 in binary will only have a single bit set. In hex, you just need to modula 4 to tell if the first hex digit should be 1,2,4, or 8, then divide by 4 to tell how many zeroes should follow it.

    E.g. 2^200 in hex is:

    0x100000000000000000000000000000000000000000000000 000

    That is how I personally would solve the problem (assuming they didn't explictly specify decimal answers) and bank on the grader finding it clever.
    Last edited by Cat; 10-16-2006 at 06:55 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You're right. That's even easier than solving the first part using decimals - and of course the first part just falls out as a special case (assuming the grader buys it).

    Edit: Actually, I'm thinking of binary. Well, if they buy base 16, why not 2?
    Last edited by robatino; 10-16-2006 at 07:04 PM.

  15. #15
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    at the moment i am still trying to calculate the power for the first bit.

    am i right in using this to help work it out?

    Code:
    {
        if  (n == 0)      // Base case
    
            return  1;
    
        else  if  (n > 0) // First  general  case
    
            return ( x * Power (x, n - 1));
        else

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heapsort
    By xENGINEERx in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 07:17 PM
  2. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  3. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  4. File Size and File Size on Disk
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-15-2001, 08:03 PM