Sum of two array?

pls tell why my problem fail in two test case;
and also correct my code
#include
using namespace std;
void suma(int arr[],int a[],int a1[],int k,int n,int m){

for(int i = k; i > 0 ;i--){
int carry = 0;
int sum = arr[i-1]+a[i]+carry;
a1[i]+= sum%10;
a1[i-1] += sum/10;
}
 for(int i =0; i < k;i++){
    cout<<a1[i]<<", ";
}
cout<<"END";

}
int main() {
int n; cin>>n;
int arr[n];
for(int i =0; i < n;i++){
cin>>arr[i];
}
int m; cin>>m; int a[m];
for(int i =0; i < m;i++){
cin>>a[i];
}
int k; if(n>m){ k = n; } else { k = m; }

int a1[k] = {0};

suma(arr,a,a1,k,n,m);


return 0;

}

Hello @kshitiz.gzb,

When you run your code for the sample input:
4
1 0 2 9
5
3 4 5 6 7
Your Output:
0, 5, 5, 9, -169697330, END

If you observe it carefully, it is giving junk value for last index. Why?
This is because you are iterating the loop inside the value for wrong values.
arr[k-1] and a[k] contains junk value.

Solution:
for(int i = k; i >0 ;i–){}

Hope, this would help.
Give a like, if you are satisfied.

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.