Can someone help me fix my multiplication. 2 int arrays come into the method and
ID: 3677269 • Letter: C
Question
Can someone help me fix my multiplication. 2 int arrays come into the method and return 1 array with the answer. thats all i need help with and this is in java not c++ thanks. Right now it works with 12*2=24 but it doesnt work with 37*26 when there are 2 numbers in the 2nd array. the array or the number can be as big as 85544975 or w.e. Also if anyone knows how to fix my div and mod that would be great aswell. If someone wants to see full program then leave ur emal in comment.
private int[] mulArr(int[] a, int[] b)
{
int size = (a.length + b.length);
int len1 = a.length;
int len2 = b.length;
int count;
int[] c = new int[size];
if(len1>len2)
{
count = a.length;
}
else
{
count = b.length;
}
for(int i=0; i {
int carry =0;
int sum;
for(int j=0; j {
sum = c[j] + (b[i]*a[j]) + carry;
carry = sum/10;
sum = sum % 10;
c[j] = sum;
count--;
for(int k = 0; k {
c[k]+=0;
}
}
}
return removeLeadingZero(c);
}
Explanation / Answer
hey firstly im confused what your code is about, if you have sent whole code I would have corrected it anyways when i went through your code i found one mistake in second where theres this line sum = c[j] + (b[i]*a[j]) + carry , I find this line wrong because you didnt initialize c[0] actually so it will take some ASCII value so I have written one line c[0]=0 after declaration so check it now if it stills shows you error mail me your complete code to karthik712008@gmail.com
private int[] mulArr(int[] a, int[] b)
{
int size = (a.length + b.length);
int len1 = a.length;
int len2 = b.length;
int count;
int[] c = new int[size];
c[0]=0;
if(len1>len2)
{
count = a.length;
}
else
{
count = b.length;
}
for(int i=0; i<count;i++) {
int carry =0;
int sum;
for(int j=0; j<count;j++) {
sum = c[j] + (b[i]*a[j]) + carry;
carry = sum/10;
sum = sum % 10;
c[j] = sum;
count--;
for(int k = 0; k<count;k++) {
c[k]+=0;
}
}
}
return removeLeadingZero(c);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.