Dear all,

I have been having trouble with this code . This program is supposed to brute-force verbal arithmetic, but when it is testing large numbers, the values in array x1 screw up badly. They become negative and I have given up. Could anyone please tell me what's wrong? This same error came up in another program I tried to write.

Code:#include<stdio.h> #include<string.h> #include<stdlib.h> int pows(int a,int b) { int p=1; for(;b>0;b--) { p*=a; } return p; } int check(int data[],int dataCheck[],int k) { int i; int flag=1; for(i=0;i<k;i++) { if(data[i]!=dataCheck[i]) { flag=0; break; } } return flag; } int sortNum(int x1[],int x) { int i,j; int y=x; for(i=0;x!=0;i++) { x/=10; } x1[i]=10; x1[i+1]=10; j=i; for(i--;i>=0;i--) { x1[i]=y%10; y/=10; } return j; } int main() { char a[30],b[30],c[30]; char x[30]; int x1[30]; int aNum,bNum,cNum; int data[60]={0},dataCheck[59]={0}; int aLen,bLen,cLen; int cLent,cNumt; int xLen; int i,j,m,k=0,k1=0; fgets(a,10,stdin); fgets(b,10,stdin); fgets(c,11,stdin); aLen=strlen(a); bLen=strlen(b); cLen=strlen(c); a[aLen-1]='\0'; b[bLen-1]='\0'; strcpy(x,a); strcat(x,b); strcat(x,c); for(i=0;x[i]!='\n';i++) { for(j=i+1;x[j]!='\n';j++) { if(x[i]==x[j]) { data[k]=i; data[k+1]=j; k+=2; break; } } } for(m=0;m<pows(10,aLen+bLen-2);m++) { aNum=m%(pows(10,aLen-1)); bNum=(m-aNum)/pows(10,aLen-1); cNum=aNum+bNum; cNumt=cNum; for(cLent=0;cNumt!=0;cLent++) { cNumt/=10; } xLen=sortNum(x1,(aNum*pows(10,bLen+cLent-1)+bNum*pows(10,cLent)+cNum)); if(xLen!=aLen+bLen+cLen-3) { continue; } for(i=0,k1=0;i<xLen;i++) { for(j=i+1;j<xLen;j++) { if(x1[j]==x1[i]) { dataCheck[k1]=i; dataCheck[k1+1]=j; k1+=2; break; } } } if(k!=k1) { continue; } if(check(data,dataCheck,k)==1) { printf("%d+%d=%d\n",aNum,bNum,cNum); } } return 0; }