My assignment is asking me to have x and y multiply each other with any number t
ID: 3573631 • Letter: M
Question
My assignment is asking me to have x and y multiply each other with any number that i want and display the result on screen. Very easy to do with long long or unsign long long or double but they cannot display very large numbers. I NEED to do this with string /array so that if i wanted x to be 9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000+ and y to be the same or even larger number, the result would be displayed correctly. i have to also use classes. My current code is below but i cannot get this to work. any help would be appreciated
#include<iostream>
#include<string>
using namespace std;
class Product
{
//declare arrays to hold large numbers
const int SIZE = 101;
string x[SIZE];
string y[SIZE];
string result[SIZE];
public:
//default constructor
Product()
{
x = 0;
y = 0;
result = 0;
}
void get_values()
{
//Enter the numbers of the integer one by one by stepping through the array
//the array will end when the user hits enter
cout << "Enter num1 = ";
cin >> x;
cout << "Enter num2 = ";
cin >> y;
}
char multiply()
{
//algorithm to multiply two very large numbers
//take the last element of the second number and multiply each element of the first array starting from the last number
//if there is a tens digit leftover add it to the next multiplying numbers
//store the multiplied numbers in another array called run1
for(int x = 0; x.length-1; x++)
{
last1 = x[x.length-1]
last1 * last2;
}
//search for last character and store it in last
//store product of the two numbers in result
return result;
}
};
int main()
{
//declare object prod1 of type class product
Product prod1;
result;
//call method get_values of class Product
prod1.get_values();
//call method multiply of class Product
result = prod1.multiply();
cout << "Product of x and y = " << result << endl;
}
Explanation / Answer
#include <iostream>
#include <string>
#define OVERFLOW 2
#define ROW b_len
#define COL a_len+b_len+OVERFLOW
using namespace std;
class product {
private:
string x , y;
public:
int getCarry(int num) {
int carry = 0;
if(num>=10) {
while(num!=0) {
carry = num %10;
num = num/10;
}
}
else carry = 0;
return carry;
}
int num(char a) {
return int(a)-48;
}
string mult(string a, string b) {
string ret;
int a_len = a.length();
int b_len = b.length();
int mat[ROW][COL];
for(int i =0; i<ROW; ++i) {
for(int j=0; j<COL; ++j) {
mat[i][j] = 0;
}
}
int carry=0, n,x=a_len-1,y=b_len-1;
for(int i=0; i<ROW; ++i) {
x=a_len-1;
carry = 0;
for(int j=(COL-1)-i; j>=0; --j) {
if((x>=0)&&(y>=0)) {
n = (num(a[x])*num(b[y]))+carry;
mat[i][j] = n%10;
carry = getCarry(n);
}
else if((x>=-1)&&(y>=-1)) mat[i][j] = carry;
x=x-1;
}
y=y-1;
}
carry = 0;
int sum_arr[COL];
for(int i =0; i<COL; ++i) sum_arr[i] = 0;
for(int i=0; i<ROW; ++i) {
for(int j=COL-1; j>=0; --j) {
sum_arr[j] += (mat[i][j]);
}
}
int temp;
for(int i=COL-1; i>=0; --i) {
sum_arr[i] += carry;
temp = sum_arr[i];
sum_arr[i] = sum_arr[i]%10;
carry = getCarry(temp);
}
for(int i=0; i<COL; ++i) {
ret.push_back(char(sum_arr[i]+48));
}
while(ret[0]=='0'){
ret = ret.substr(1,ret.length()-1);
}
return ret;
}
void printhuge(string a) {
cout<<" ";
for(string::iterator i = a.begin(); i!=a.end(); ++i) {
cout<<*i;
}
}
void get_values()
{
//Enter the numbers of the integer one by one by stepping through the array
//the array will end when the user hits enter
cout << "Enter num1 = ";
cin >> x;
cout << "Enter num2 = ";
cin >> y;
printhuge( mult(x,y));
}
};
int main() {
product prod1;
prod1.get_values();
return 0;
}
/*output :
Enter num1 = 111111111111111111111111111111
Enter num2 = 333333333333333333333333333333
37037037037037037037037037036962962962962962962962962962963
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.