cybertextron
cybertextron

Reputation: 10981

multiplying two large numbers in C / C++

I was trying to come up with the solution for that ... two large numbers, a and b are represented by char[] or char* and the goal is to multiply them into a third pointer, char* c:

void multiply( const char* a, const char* b ){
    int len_a = strlen( a );
    int len_b = strlen( b );
    int* c = new int[ len_a + len_b];
    memset( c, 0, sizeof(int) * ( len_a + len_b ));

    for( int i = len_a - 1; i >= 0; i-- ){
        for( int j = len_b - 1; j >= 0; j-- ){
            c[ i + j + 1 ] += ( b[ j ] - '0') * ( a[ i ] - '0' );
        }
    }

    for( int i = len_a + len_b; i >= 0; i-- ){
        if( c[ i ] >= 10 ){
            c[ i - 1 ] += c[ i ] / 10;
            c[ i ] %= 10;
        }
    }

    cout << a << " * " << b << " = " << c << endl;
    delete[] c;
}

I wrote the above function to do this operation for me ... however, when I use the inputs:

int main( void ){
    const char* a = "999";
    const char* b =  "99999";
    multiply( a, b );
    // I expect the answer to be 1 and 6
    // profit = 0.92
    return 0;
}

I got:

999 * 99999 = 0x100100080

Why am I getting the memory address and not the actual number? Thanks!

Upvotes: 1

Views: 8062

Answers (5)

Ankush Virmani
Ankush Virmani

Reputation: 43

for(c++14) we can use boost libarary..

#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
int main()
{
  mp::cpp_int s1("12368123681263817263863821638126328136218362182");
  mp::cpp_int s2("345897937325785470923092923709887329092470423707534025");
  mp::cpp_int S=s1*s2;
  std::cout << S << '\n';
  }

Upvotes: 0

erol yeniaras
erol yeniaras

Reputation: 3795

Your logic is correct. Just a quick reminder: When you create an integer pointer and want to use it as an array, it points to "the first element of the array" therefore when you print it, you see the address of the first element of the array c, which is "0x100100080" in your case.

To print the number (characters) stored in c you need to de-reference the pointer, i.e., print the elements in the array one after another. Or alternatively you can convert your array into a number and print it at once. For the latter, please refer to: How to convert array of integers into an integer in C?. For printing the characters one by one you could replace

std::cout<<c; 

with the following code:

int n=strlen(c);
for(int i=0; i<n; i++) {
    std::cout<<c[i];
}

This will print the number.

Upvotes: 1

cybertextron
cybertextron

Reputation: 10981

cout << a << " * " << b << " = ";
    for( int i = 0; i < len_a + len_b; i++ ){
        cout << c[ i ];
    }

    cout << endl;

will yield the desired result ...

Upvotes: 1

eq-
eq-

Reputation: 10096

std::ostream (of which type std::cout is) doesn't have any overloaded operators specifically for int*, thus it falls back to the void* overload which simply outputs the pointer value in an implementation-defined manner.

Furthermore, it wouldn't be possible for an int* overload to determine that the pointer points to an array, and further still, how many elements such an array would have.

Upvotes: 0

smocking
smocking

Reputation: 3709

Because c is an int pointer and the stream operator for cout will print a memory address if passed such a pointer. To get the value you need to dereference the pointers with e.g. *c. You'll probably need to write a loop to print the whole "string" of integers.

Upvotes: 3

Related Questions