Last active
August 15, 2016 23:11
-
-
Save raunaqbn/c4881b431655c006dd0cb01be0ff8f4b to your computer and use it in GitHub Desktop.
Arrays 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Program to multiply two arbitary precision arrays */ | |
vector<int> Multiply (vector<int> num1, vector<int> num2) | |
{ | |
// The result will be sum of size of 2 numbers + 1. setting it to 0 initially | |
vector result(num1.size()+num2.size()+1,0); | |
//Return early if either is empty | |
if(num1.empty() || num2.empty()) | |
return result; | |
// check signs and take absolute values of the numbers | |
bool sign = (num1.front()<0 && num2.front()>0 )||(num1.front()>0); | |
num1.front() = abs(num1.front()); | |
num2.front() = abs(num2.front()); | |
// We perform standard partial multiplication | |
for (int i = num1.size(); i>0; i--) | |
{ | |
for (int j = num2.size(); j>0; j--) | |
{ | |
result[i+j+1] += num1[i]*num2[j]; | |
result[i+j] = result[i+j+1]/10; | |
result[i+j+1] = result[i+j+1]%10; | |
} | |
} | |
if (sign) | |
result.front()*= -1; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment