Thread: Can't figure out how to get exponents

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Can't figure out how to get exponents

    I'm doing a program that prints out tables of exponents (using nested loops). Before I even got into the program I decided to make a simpler program that is just input base, input power, output exponent. However, I can't figure out how to do exponents. Searching gives me some stuff relating to "math.h", which I know nothing about. Here is my simple (just base power result) program which doesn't work:

    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int base;
        int power;
        int result;
        int output = 0;
        cout << "Enter a base: ";
        cin >> base;
        cout << "Enter a power: ";
        cin >> power;
        for(int track = 1; track < power; track++) {
                output = base * base * track;
                base++;
                }
                cout << output;
                }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    While you can use the pow function from <math.h> (or rather <cmath> since this is C++), you might as well try your hand at implementing this by hand, given that it only involves integers.

    Consider a simple case of 5 to the power of 4: 5 * 5 * 5 * 5. Clearly, a naive solution exists such that you can start with 1, then loop 4 times, multiplying the intermediate result by 5 each time. So, you can generalise this for x to the power n: start with 1, loop n times, multiplying the intermediate result by x each time.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    I had gotten it to work with some numbers, mainly base 2 power 3 but not others, mainly base 5 power 4. Now I messed around with it and it always prints 0.
    Here is the first code I came up with that works with base 5 power 4 but not base 2 power 3:
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int base;
        int power;
        int result = 0;
        cout << "enter base: ";
        cin >> base;
        cout << "enter POWER: ";
        cin >> power;
        for(int lvar = 1; lvar <= power; lvar++) {
              //  cout << lvar;
              //  cout << endl;
                lvar = base * base;
               // cout << lvar;
               // cout << endl;
                result = lvar * base * base;
                }
                cout << result;
                }
    Here is the latest code:

    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int base;
        int power;
        int result = 0;
        cout << "enter base: ";
        cin >> base;
        cout << "enter POWER: ";
        cin >> power;
      //  int track = 0;
        for(int lvar = 1; lvar <= power; lvar++) {
              //  result = lvar * result;
              //  cout << lvar;
              //  cout << endl;
              for(int track = 0; track <= lvar; track++) {
                       base = base * base;
                       }
                result = base * base;
               // cout << lvar;
               // cout << endl;
              //  result = result * base * base;// power * lvar * result; // lvar * base * base;
               
                }
                cout << result;
                }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is sheer luck that code happens to work with some values, but no surprise it doesn't work with others.

    Turn off your computer. Try writing down on paper exactly how you would compute the value of "a to the power b" where b is an integral value - step by step. Don't turn on your computer (i.e. don't try to write code) until you have an approach that works reliably.

    Guesswork, or simply tapping away in the hope you'll get something to work, is not a reliable way to achieve working code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That is only computing base*base - the same result, ten times over.

    If base is 2 and power is 10, the value result will be assigned the value 4 - the first time, the second time, the third time, .... every time until the tenth time and the printed value will be 4.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Update: I found a recursive function that works. The main problem I was trying to solve was printing out a table of exponents- for example, with rows 2 columns 3 printing:
    1 1 1
    2 4 8
    but I can't figure out how I would do that. I'm aware of setw for the spacing itself, however.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    So you've progressed from guesswork to obtaining and using a sample of code that you don't understand.

    Given that laserlight gave a clear description of how to solve your problem in the first reply to this thread, your approach (continuing with guesswork and then finding/using a sample of code without understanding it) is appalling.

    Welcome to my ignore list.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggested a simple loop as a naive solution, with a recursive solution mind for something more efficient. Since you are printing a table, the simple loop is no longer naive but in fact appropriate and efficient.

    If you want to find code to help you along, go ahead, but if you cannot implement this simple loop, then I'd be amazed if you can understand the recursive function that you found.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    Since you are printing a table, the simple loop is no longer naive but in fact appropriate and efficient.
    Assuming the table is required to present the values in some order (ascending or descending), a reasonably efficient recursive solution is feasible. But appropriateness..... hmmm! It would be dreadful to maintain if requirements changed.

    It is the silly season
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exponents
    By ladysniper in forum C++ Programming
    Replies: 8
    Last Post: 03-18-2006, 12:19 PM
  2. Exponents??
    By -JM in forum C++ Programming
    Replies: 5
    Last Post: 07-04-2005, 03:28 PM
  3. exponents
    By joeshmoe1337 in forum C Programming
    Replies: 4
    Last Post: 08-14-2004, 04:00 PM
  4. how to use exponents
    By guitargatler in forum C++ Programming
    Replies: 11
    Last Post: 02-01-2003, 09:09 PM
  5. exponents
    By dustmanx in forum C++ Programming
    Replies: 1
    Last Post: 02-16-2002, 12:44 PM