Arrays-Sum of Two Arrays

Can anyone help me with the code for thr above problem.I am not able to pass testcases.

#include
#include
using namespace std;
int main() {
int N,M,i;
cin>>N;
int a[1000]={0},n1=0,n2=0;
for(i=N-1;i>=0;i–){
cin>>a[i];
}
cin>>M;
int b[1000]={0};
for(i=M-1;i>=0;i–){
cin>>b[i];
}
int z=max(N,M);
int c[1000]={0};
for(i=0;i<=z;i++){
c[i]+=a[i]+b[i];
if((c[i]>9) ||(c[i]<-9)){
c[i+1]+=c[i]/10;
if(c[i]>9){
c[i]=c[i]%10;
}
if(c[i]<-9){
c[i]=c[i]%10-10;
}
}
}

if(c[z]==0){
	for(i=z-1;i>=0;i--){
		cout<<c[i]<<",";
	}
}
else{
	for(i=z;i>=0;i--){
		cout<<c[i]<<",";
	}
}

cout<<β€œEND”<<endl;
return 0;
}

@priyeshant1104 suppose both N and M are 1000, then your third for loop will run from 0 - 1000 , which can cause wrong answer and error.
Your method seems correct, bit you are not handling the situation when we have end carry at last, and final array might be larger than both the number by 1.
Try to implement it, if this resolves your doubt mark it as resolved, else I would help you get the right code

I have changed c[1000] to c[1001] but now I am not getting any output

@priyeshant1104 but a and b are still 1000, and when z=1000, you need to access a[1000] // can lead to seg fault.

changed a an b to 1007 still no output

@priyeshant1104 where are you running this code? It gives output and correctly after making the changes.
It would not pass the grader yet, because you are not using spaces between numbers. Remember to pass grader your out put format must be correct.

#include <bits/stdc++.h>
using namespace std;

int main() {
    
int N,M,i;
cin>>N;
int a[1007]={0},n1=0,n2=0;
for(i=N-1;i>=0;i--){
cin>>a[i];
}
cin>>M;
int b[1007]={0};
for(i=M-1;i>=0;i--){
cin>>b[i];
}
int z=max(N,M);
int c[1007]={0};
for(i=0;i<=z;i++){
c[i]+=a[i]+b[i];
if((c[i]>9) ||(c[i]<-9)){
c[i+1]+=c[i]/10;
if(c[i]>9){
c[i]=c[i]%10;
}
if(c[i]<-9){
c[i]=c[i]%10-10;
}
}
}

if(c[z]==0){
    for(i=z-1;i>=0;i--){
        cout<<c[i]<<", ";
    }
}
else{
    for(i=z;i>=0;i--){
        cout<<c[i]<<", ";      // space added after comma
    }
}
cout<<"END"<<endl;
return 0;
}

This would pass the grader.
If this resolves your doubt mark it as resolved.

I am getting no output not even any error

@priyeshant1104 strange I am running the code and I am getting the right output

What should I do then?How do I submit my code ? Is there any problem with IDE?
Please help!

@priyeshant1104 try submitting the code now, it will work.
If this resolves your doubt mark it as resolved.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.