Help needed in debugging(Sum of two arrays)

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

void calSumUtil(int a[], int b[], int m, int n);
void calSum(int a[], int b[], int n, int m)
{
if(n >= m)
calSumUtil(a, b, n, m);
else
calSumUtil(b, a, m, n);
}
void calSumUtil(int a[], int b[], int n, int m)
{
int sum[n];
int i = n-1, j = m-1, k = n-1;
int carry = 0, s = 0;
while(j >= 0)
{
s = a[i] + b[j] + carry;
sum[k] = s%10;
carry = s/10;
k–;
i–;
j–;
}
while(i >= 0)
{
s = a[i] + carry;
sum[k] = s % 10;
carry = s/10;
i–;
k–;
}
if(carry)
{
cout << carry << ", ";
}
for(int i = 0; i <= n-1; i++){
if(i == n-1)
cout << sum[i] << β€œ, END”;
else
cout << sum[i] << ", ";
}
}
int main()
{
int n, m;
cin >> n;
int a[n];
int i, j;
for(i = 0; i < n; i++)
cin >> a[i];
cin >> m;
int b[m];
for(j = 0; j < m; j++);
cin >>b[j];
calSum(a, b, n, m);
return 0;
}
Could you please tell me where i am going wrong in updating the sum array?

@utkarshvashisth94 please provide your code in coding blocks IDE.

@utkarshvashisth94
for(j = 0; j < m; j++); // remove this semi colon , it would work then
cin >>b[j];

if this solves your doubt mark it resolved

1 Like