$powerOf() (adjusted for C++)

By vaseline28 on Jan 27, 2009

Image

( Above is an example of use )

I basically adjusted my mIRC $powerOf snippet for use in C++ to help me learn (only been doing this for 2 days).

Snippet requires some sense as if you use ridiculously high numbers, you WILL overload it. Please also note that due to use of "int" instead of "float" decimals cannot be used.

#include <iostream>
using namespace std;

int main ()
{
    int x = 1, y = 1, inputStartNumber, inputToNumber, inputConstant;
    int * result;
    result = new int [5];

    cout << "Enter starting number: ";
    cin >> inputStartNumber;
    cout << "Calculate [ " << inputStartNumber << " ] to the power of: ";
    cin >> inputToNumber;
    inputConstant = inputToNumber;

    cout << "[1] " << inputStartNumber << "\n";

    while ( x < inputConstant )
    {
        inputStartNumber = inputStartNumber * inputStartNumber;
        result[x] = inputStartNumber;
        x++;
    }
    while ( y < inputConstant )
    {
        cout << "[" << y+1 << "] " << result[y] << "\n";
        y++;
    }
    main ();
}

Comments

Sign in to comment.
Firstmate   -  Jan 28, 2009

To acommodate for both decimals and extremely large numbers (+/- 1.7e308) I suggest using double. e.g.

    double x = 1, y = 1, inputStartNumber, inputToNumber, inputConstant;
    double * result;

I'm still learning C++ so I can't say that's the answer D:

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.