This program must be written in C++ The solution to classwork 4 problem 2 does n
ID: 3757043 • Letter: T
Question
This program must be written in C++
The solution to classwork 4 problem 2 does not accurately multiply some numbers. For example, 23 x 19 = 022127 because the multiply function does not carry the results from one column to the next column.
CLASSWORK 4 PROBLEM 2 BELOW:
Define a dynamic array class whose data members are private.
class dynamicArray
{
public:
void show();
dynamicArray(int new_size = 8);
dynamicArray(const dynamicArray& new_array);
const dynamicArray& operator=(const dynamicArray& source);
private
int *elts;
int size;
}
Define the default constructor, copy constructor, assignment function, and show method
The elements of the array are the digits from 0 to 9.
Define a function that multiplies the elements of two arrays. The function returns the product of the array elements. If the product of two array elements is too big to fit in an array position then some of the product carries to the next higher position in the array. The function's prototype is
dynamicArray multiply(const dynamicArray& da1, const dynamicArray da2);
9 x 3 = 27 appears in the zeroth column
9 x 2 = 18 and 1 x 3 = 3 which means that 18 + 3 = 21 which appears in the first column
Finally, 2 x 1 = 2 appears in the second column.
Again the function generates this answer because products that do not fit in one column do not carry to the next column.
Accurate multiplication obeys this rule: the product of the ith digit of one number and the jth digit of the other number is added to any number in the (i + j)th column of the product. If this sum is greater than 10 then the remainder of the sum divided by 10 remains in the (i + j)th column and the quotient of the sum divided by 10 is added to any number in the (1 + i + j)th column.
Applying this rule, an accurate multiplication of 23 and 19 with carries follows:
9 x 3 = 27. The 2 carries to the first column of the product and the 7 appears in the zeroth column of the product..
9 x 2 = 18 where the 8 is in the first column of the product and the 1 is in the second column of the product. We add the 2 from the first multiplication in the first column to the 8 in the first column. 8 + 2 = 10. The 0 appears in the first column of the product and 1 carries the second column of the product. A 1 is already in the second column of the product. Therefore we add this 1 to the 1 that is already there.
At this stage of the multiplication, 9 x 23 equals the product is 207.
We now multiply 1 x 23. 1 x 3 = 3 in the first column of the product. We add the 3 to the 0 that is already in the first column: 0 + 3 = 3. We multiply the 1 in the first column of 19 by the 2 in the first column of 23. 1 x 2 = 2 which appears in the second column of the product. We add this 2 to the 2 that is already in the second column. Now 4 is in the second column of the answer. The final answer is 437.
Explanation / Answer
#include<iostream>
using namespace std;
int length(int num1 , int num2){
int max;
if(num1>num2)max=num1;
else max=num2;
int count=0;
while(max){
count++;
max/=10;
}
return count;
}
int main(){
int num1,num2;
int sum;
int i=0,j=0;
cout<<"Enter num1 : ";
cin>>num1;
cout<<"Enter num2 : ";
cin>>num2;
int size = length(num1,num2);
size*=2;
size-=1;
int *arr = new int[size];
for( i=0;i<=size;i++)arr[i]=0;
i=0;
j=0;
for(int exp1=1;num1/exp1>0;exp1*=10){
j=0;
for(int exp2=1;num2/(exp2)>0;exp2*=10){
arr[i+j] += (num1/exp1)%10 *((num2/exp2)%10);
if(i+j+1<size)arr[i+j+1]=arr[i+j]/10;
arr[i+j]%=10;
j++;
}
i++;
}
cout<<"Your multiplication : ";
for( i=size-1;i>=0;i--){
cout<<arr[i];
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.