URL to question is: https://online.codingblocks.com/player/12975/content/4796?s=2165
We have to add two arrays like numbers (arithmetic addition)
This is my code:
It is not executing properly, but I dont know where I have made an error.
While running the program, it crashes. Please do let me know where the error is.
#include
using namespace std;
void addArrays(int a[], int n, int b[], int m)
{
int i = n-1;
int j = m-1;
int k = 0;
int carry =0,sum=0;
int result[k]={0};
while(i>=0 && j>=0)
{
sum = a[i] + b[j]+carry;
result[k] = sum %10;
carry = sum/10;
k++;
i–;
j–;
}
//Printing out the remaining part of the array.
while(i>=0)
{
sum = carry + a[i];
carry = sum/10;
result[k] = sum%10;
i--;
k++;
}
while(j>=0)
{
sum = carry + b[j];
carry = sum/10;
result[k] = sum%10;
j--;
k++;
}
for(int s=k-1;s>=0;s--)
{
cout<<result[s]<<","<<" ";
}
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 array2[m];
for(int i=0;i<m;i++)
{
cin>>array2[i];
}
addArrays(arr,n,array2,m);
return 0;
}