Debug small error

Nov 16, 2017 at 1:05am
I have this code that I a suppose to debug. I am not sure what is the problem I am facing. Please help and advice.
#include <iostream>
#include <cstring>

using namespace std;

int main(){
char *x="23489";
char *y="99";
int *z;
int c=0;
int n=strlen(x);
int n2=strlen(y);
z = new int[n+1];

if(n>=n2) {

for(int i=1; i<=n2;i++) {
if((x[n-i]-48)+(y[n2-i]-48)>=10) {
z[n+1-i]=((x[n-i]-48)+(y[n2-i]-48)+c)%10;
c=1;
}
else {
z[n+1-i]=(x[n-i]-48)+(y[n2-i]-48)+c;
c=0;
if(z[n+1-i]>=10) {
z[n+1-i]%=10;
c = 1;
}
}
}
if(n==n2) {
z[0]=c;
}
else {
z[n-n2]=(x[n-n2-1]-48)+c;
c=0;
if(z[n-n2]>=10) {
z[n-n2]%=10;
c = 1;
}
for(int i=1; i<n-n2; i++) {
z[n-n2-i]=(x[n-n2-1-i]-48)+c;
c=0;
}
z[0]+=c;
}
for(int i=0; i<n+1; i++)
cout << z[i] << endl;
}
}
Nov 16, 2017 at 5:32am
You could start by looking at those arrays. Check if they're being accessed outside of their range.
Nov 16, 2017 at 10:16am
You have to tell us what the problem is first, then we might be able to tell you where the problem is and how to fix it.

Does it compile?
Does it crash ?
Wrong output?
No output?
Topic archived. No new replies allowed.