Hi,
Link to Question — https://online.codingblocks.com/player/12975/content/7855/4820
My code fails for Test Case -2
and Test Case -4
My code:
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press “Run” button to compile and execute it.
*******************************************************************************/
// CPP program to sum two numbers represented two
// arrays.
#include <bits/stdc++.h>
using namespace std;
// Return sum of two number represented by the arrays.
// Size of a[] is greater than b[]. It is made sure
// be the wrapper function
void
calSumUtil (long a[], long b[], long n, long m)
{
// array to store sum.
long sum[n];
long i = n - 1, j = m - 1, k = n - 1;
long carry = 0, s = 0;
// Until we reach beginning of array.
// we are comparing only for second array
// because we have already compare the size
// of array in wrapper function.
while (j >= 0)
{
// find sum of corresponding element
// of both arrays.
s = a[i] + b[j] + carry;
sum[k] = (s % 10);
// Finding carry for next sum.
carry = s / 10;
k--;
i--;
j--;
}
// If second array size is less the first
// array size.
while (i >= 0)
{
// Add carry to first array elements.
s = a[i] + carry;
sum[k] = (s % 10);
carry = s / 10;
i--;
k--;
}
long ans = 0;
// If there is carry on adding 0 index elements.
// append 1 to total sum.
if (carry)
ans = 10;
for(long i=0;i<=n-1;i++)
{
cout<<sum[i]<<","<<" ";
}
cout<<"END";
}
// Wrapper Function
void calSum (long a[], long b[], long n, long m)
{
// Making first array which have
// greater number of element
if (n >= m)
calSumUtil (a, b, n, m);
else
calSumUtil (b, a, m, n);
}
// Driven Program
int
main ()
{
long n;
cin >> n;
long a[n];
for (long i = 0; i < n; i++)
cin >> a[i];
long m;
cin >> m;
long b[m];
for (long i = 0; i < m; i++)
cin >> b[i];
calSum (a, b, n, m);
return 0;
}
Link to my code -
https://onlinegdb.com/HJQ3ZHnpN
Please can you guide me as to where my code is wrong.