tags:

views:

153

answers:

3

Question goes like this.. Input

n [the number of multiplications <= 1000]
l1 l2 [numbers to multiply (at most 10000 decimal digits each)]

Text grouped in [ ] does not appear in the input file.

Output

The results of multiplications.

My code..

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    long int n,a,b,c,t;
    vector<long int> v(1000);
    vector<long int>::iterator p;
    cin>>n;
    t=n;
    p=v.begin();
    while(t>0)
    {
        cin>>a;
        cin>>b;
        *p=a;
        *(p+1)=b;
        p+=2;
        t--;
    }
    t=n;
    p=v.begin();
    while(t>0)
    {
        c= (*p) * (*(p+1));
        cout<<"\n"<<c;
        p+=2;
        t--;
    }
  return 0;
}
+3  A: 

long int isn't going to hold anywhere near 10,000 decimal digits.

−9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

sixlettervariables
sir, but the multiplication of two 10000 decimal digits will be held in a long int type varialble..
vaibhav
Actually how much long int can hold is implementation dependent, only a minimum is guaranteed.
Matthieu M.
@vaibhav, you mistake "x < 10000" and "x has 10000 digits" the latter means that you need 10000 characters to represent x on your screen!
Matthieu M.
I just gave a common range for `long int`. Users are free to find out what it is on their platform.
sixlettervariables
+1  A: 

Had to google your problem to understand what it was: here it is, please be considerate of your readers :x

Your code cannot actually work: long int is not long enough (and this is implementation dependent anyway)

You will have to read the integers 'char' by 'char' and roll your own implementation of BigInts which is the difficulty of the problem here I surmise.

Furthermore, you will have to deal with overflow: the result of int x int might not fit into an int. Truth to be told, you should be prepared to have a result that might take up to 20.000 digits

Matthieu M.
thank you sir for your help.. i got the question now.. but can you please tell me whether this question means that i cannot use arrays or strings??
vaibhav
What don't use a vector since you already have one? However you'll run into the FAST multiplication problem soon enough, you might want to consult http://mattmccutchen.net/bigint/ for example to see how he does it. The fast multiplication problem is a classic, but it is always fun.
Matthieu M.
A: 

It's not surprising that you can find 100s of test cases that your program will pass.

On most 32-bit compilers, long int (also written as long) is a 32-bit number. It can hold around 4 billion unique values (so that's a 10 digit number). So if you've only found 100 test cases that your program passes, you aren't really trying!

Did you try asking your program to multiply 100108810983456785767868356745567566892348745 by 29387345245742687609834756790356568098645?

Daniel Earwicker
thank you sir for your help.. i got the question now.. but can you please tell me whether this question means that i cannot use arrays or strings??
vaibhav
To be fair to the OP, the example input/output given on the problem page also didn't exercise even marginally large numbers, so there wasn't really a good way for the OP to know he wasn't fully understanding the problem.
Michael Burr
@vaibhav - I would think that you would absolutely have to use arrays and/or strings.
Daniel Earwicker
@Michael Burr - I didn't post this to tell the OP that they should have figured out the question for themselves. I posted it to address the claim in the subject line "although its working correct for hundreds of test cases"; it only has to fail one.
Daniel Earwicker