SUM OF TWO ARRAYS.. m not getting what is wrong with my code.. can u plz help me out

#include
#include
using namespace std;
int main(){
int a[]={1,0,2,9};
int n=sizeof(a)/sizeof(a[0]);
int b[]={3,4,5,6,7};
int m=sizeof(b)/sizeof(b[0]);

 int i=n-1,j=m-1;
 int resSize=max(n,m)+1;
 int res[resSize];
 int k=resSize-1;
 int carry=0;
 while(i>=0 || j>=0){
 	int sum=carry;
 	
 	if(i>=0){
 		sum+=a[i];
	}
	if(j>=0){
		sum+=b[j];
	}
 	
 	res[k--]=sum%10;
 	carry=sum/10;
 	i--;
 	j--;
 	
 }
 while(carry){
 	res[k--]=carry%10;
 	carry=carry/10;
 }
 
 for(int i=0;i<=resSize-1;i++){
 	cout<<res[i];
 }

}

Hello @shikharskj what is the error that you are getting?

m getting the wrong output

Hello @shikharskj here is the correct code:


but i think your approach will not pass every test case.
and please be in a habbit of sharing your code by saving it on ide.codingblocks.com

what was wrong iin my code… ?

@shikharskj initialise k as ressize-2

why did we have to initialioze k with resSize-2??

@shikharskj i have made corrections according in your logic i have just made your code correct.
i told you this code may not be correct .
we have made k initiliase with that because it was giving garbage value at first index so io tried with k-2 and it worked.

if you dry run your code you will understand

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.