amol0010
10-12-2009, 08:55 AM
Sorry to create another thread, but I earlier said on the thread that the exponentiation function is running fine but it isnt for some strange reason. I am absolutely stumped on why not..
I am using the multiplication function which I wrote earlier, and the multiplication function runs absolutely fine, which I tried running for a lot of combinations.
Here it is:
string MultiplyIntegers(string a, string b )
{
//Base to divide the integers
int base = 10;
//If Either of the strings are zero, then return the result as zero
if(a=="0" || b=="0")
return "0";
//Set the width of the resultant string to be size of the first string + second string, initialize it with zeros filled in
string result(a.length() + b.length() - 1, '0');
for (int i = b.length() - 1; i >= 0; i--)
{
int carry=0;
for(int j = a.length() - 1; j >= 0; j--)
{
int t = (a[j] - '0') * (b[i] - '0') + result[i + j] - '0' + carry;
result[i + j] = t % base + '0';
carry = t / base;
}
if (i ==1)
result[i - 1] = carry + '0';
else
result.insert(result.begin(), carry + '0');
}
if (result[0] == '0')
{
return result.substr(1);
}
else
{
return result;
}
}
Then I wrote the exponentiation function this way - it works fine only upto the power 2 and for some integers to the power of 3. After the power 2 or 3, it starts giving weird answers.
e.g. if I enter 12^2 - it returns 144, but on 12^3 it gives 16848 where it should return 1728.
My exponent function is declared as:
//Multiply two long integers as strings, return a string
string ExponentIntegers(string a, string b)
{
//Convert the number to be raised to an integer
int counter = atoi(b.c_str());
//Number to be returned is set to 1
string multiplicant("1");
//If smaller number is 0 (0 exponent), return 1
if(counter == '0')
{
return multiplicant;
}
//With each iteration, multiply the number with itself
for(int i = 0; i<counter; i++)
{
cout<<a<<" ";
cout<<multiplicant<<endl;
multiplicant = MultiplyIntegers(a,multiplicant);
}
//Return the result
return multiplicant;
}
What am I doing wrong in the exponentiation function ??? I have absolutely no clue ..
I am using the multiplication function which I wrote earlier, and the multiplication function runs absolutely fine, which I tried running for a lot of combinations.
Here it is:
string MultiplyIntegers(string a, string b )
{
//Base to divide the integers
int base = 10;
//If Either of the strings are zero, then return the result as zero
if(a=="0" || b=="0")
return "0";
//Set the width of the resultant string to be size of the first string + second string, initialize it with zeros filled in
string result(a.length() + b.length() - 1, '0');
for (int i = b.length() - 1; i >= 0; i--)
{
int carry=0;
for(int j = a.length() - 1; j >= 0; j--)
{
int t = (a[j] - '0') * (b[i] - '0') + result[i + j] - '0' + carry;
result[i + j] = t % base + '0';
carry = t / base;
}
if (i ==1)
result[i - 1] = carry + '0';
else
result.insert(result.begin(), carry + '0');
}
if (result[0] == '0')
{
return result.substr(1);
}
else
{
return result;
}
}
Then I wrote the exponentiation function this way - it works fine only upto the power 2 and for some integers to the power of 3. After the power 2 or 3, it starts giving weird answers.
e.g. if I enter 12^2 - it returns 144, but on 12^3 it gives 16848 where it should return 1728.
My exponent function is declared as:
//Multiply two long integers as strings, return a string
string ExponentIntegers(string a, string b)
{
//Convert the number to be raised to an integer
int counter = atoi(b.c_str());
//Number to be returned is set to 1
string multiplicant("1");
//If smaller number is 0 (0 exponent), return 1
if(counter == '0')
{
return multiplicant;
}
//With each iteration, multiply the number with itself
for(int i = 0; i<counter; i++)
{
cout<<a<<" ";
cout<<multiplicant<<endl;
multiplicant = MultiplyIntegers(a,multiplicant);
}
//Return the result
return multiplicant;
}
What am I doing wrong in the exponentiation function ??? I have absolutely no clue ..