Sum of two arrays(arrays)

2 test cases are passed and 2 cases are wrong. please tell the problem in this code.

#include
using namespace std;
int main(){
int a[1000]={0},b[1000]={0},c[1000]={0};
int m,n,t;
cin>>m;
for(int i=m-1;i>=0;i–){
cin>>a[i];
}
cin>>n;
for(int j=n-1;j>=0;j–){
cin>>b[j];
}
t=max(m,n)+1;
for(int i=0;i<=t;i++){
c[i]=a[i]+b[i]+c[i];
if(c[i]>=10){
c[i]=c[i]-10;
c[i+1]++;
}
}
if(a[t]==0){
t–;
}
for(int i=t;i>=0;i–){
cout<<c[i]<<", ";
}
cout<<“END”;
return 0;
}

@gauravmadan583 save your code on ide and save it here

Hi Gaurav, the carry operation in your algorithm for addition is not correct. It should be this in line 17&18:

c[i+1] = c[i]/10;
c[i] = c[i]%10;

Also sometimes there appears 2 preceding 0s instead of 1 in some sums. So instead of applying 1 time if use a while loop in line 22 instead of the if like:

while ( c[t] == 0 && t > -1)
      - - t ;

Your code will pass all the testacases now. Also from next time pls save your code on ide.codingblocks.com and share the URL in doubt thread and try to write well indented code.

Hope this helps :slight_smile:

Hey Gaurav,
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.