Calculating function and writing output number by number as an array

So, what I basically need is to calculate value of an exponential function (2^n)*(2^(n-1)-1) using c++, where n is a unsigned int type user input. Since n can be assigned any value in unsigned int range, I cannot simply calculate function value and assign it to some unsigned int variable (because resulting number will clearly be out of said range). Therefore, it seems reasonable to me to somehow get my desired function value number by number and write each number as a array element. The only struggle is to realise this method.

Any ideas (both using proposed method or any other one) would be much appreciated.

edit: Right now I have the following code:

outdated

As expected, it will only work with small values of n

Edit 2: as one of the commentators suggested, I've done some binary numbers by hand. It was pretty fruitful, but I still need sime assistance. Now i have the following code, which will correctly output binary value of said function:

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

class Exp {
    private:
        unsigned int n;
        char* dec;
        string bin;
        char* hex;
    public:
        Exp(){
            n = 0;
            bin = '0';
            dec = 0;
            hex = 0;
        };
        Exp(unsigned int);
        operator char* ();
};

Exp::Exp(unsigned int nn) {
    n = nn;
    bin=string(nn, '1');
    bin+=string(nn+1, '0');
}

Exp::operator char *() {
    static char str[3200];
    cout << "binary - " << bin << endl;
    return str;
}


int main(int argc, char* argv[]) {
    int n;
    if (argc != 2){
        cout << "Incorrect number of arguments" << endl;
        exit (2);
    }
    if ((sscanf(argv[1], "%u", &n) != 1) or ((n < 2) or (n > 4294967295))){
        cout << "incorrect exp value" << endl;
        exit (-3);
    }
    Exp num(n);
    cout << (char*) num << endl;
};

The only thing left is to convert this value to decimal and hexadecimal. I am not experienced at working with string class, so advice would be appreciated.



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation