Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I have written a code to subtract 2 large integers stored in arrays! But the problem is, its not working when the minuend is smaller than the subtrahend, i.e, when the answer would be negative. I am taking 2 char arrays as parameters, converting them to int arrays, working with them, storing the result in a char array & returning it! Can somebody please point out the optimizations needed in the code!
C++
#include<iostream.h>
#include<conio.h>
#include<string.h>

class Sub{
 public:
 char *subArray(char a[], char b[])
 {
  int i, j, k, diff[255], borr, size1=0, size2=0, x[255], y[255];
  char temp[255], *p;
  cout<<"\nNuM1: "  ;
  for(i=0; a[i]!=NULL; i++)
  {
    x[i]=a[i]-'0';                 //conversion into int array 
    size1++;                     
    cout<<x[i];
  }
  cout<<"\nNuM2: "  ;
  for(i=0; b[i]!=NULL; i++)
  {
    y[i]=b[i]-'0';                //conversion into int array 
    size2++;
    cout<<y[i];
  }
  if(size1<size2)                
  {
	p=subArray(b, a);
        p[0]=-p[0];               //not printing the correct value
	return p;
  }
  else
  {
	 for(j=0; j<size2; j++)
	 {
	  y[j]=-y[j];
	 }
	 k=borr=0;

	 for(i=size1-1, j=size2-1;i>=0 && j>=0; i--, j--, k++)
	 {
	  diff[k]=x[i]+y[j]-borr;
	 if(diff[k]<0)
	 {
	  diff[k]+=10;
	  borr=1;
	 }
	 else
	 borr=0;
	 }
	 if(i>=0)
	{
	 for(;i>=0;i--,k++)
	 {
	  diff[k]=x[i]-borr;
	  if(diff[k]<0)
	  {
		diff[k]+=10;
		borr=1;
	  }
	  else
	  borr=0;
	}
  }

  i=j=0;
  for(i=k-1; i>=0; i--)
  {
	 if(diff[i]<0)
	 {
	  temp[j++]='-';
	  temp[j++]=-(diff[i])+'0';
	 }
	 else
	 temp[j++]=diff[i]+'0';
  }
  temp[j]='\0';
  return temp;
  }
 }
};
void main()
{
 Sub s;
 char num1[255], num2[255], *diff;
 cout<<"NuM1: ";
 cin>>num1 ;
 cout<<"\nNuM2: ";
 cin>>num2;
 diff = s.subArray(num1, num2);
 cout<<"\nDifference: ";
 while(*diff != NULL)
 {
  cout<<*diff;
  diff++;
 }
}
Posted

1 solution

The biggest problem: You are returning a pointer to the temp array that allocated on the stack. Once you return from your function, that array goes out of existence and it is pure luck if the calling function can see anything in that array.

Solution: Allocate the result array by the caller and pass a pointer to that array as parameter to your function:
C++
char diff[255];
...
Sub (res, num1, num2);


There are a couple of things that you have solved a little clumsy. Checking for the length of both arguments is basically useless, because any of them might contain leading zeros.

I would handle the sign of the arguments and the result in a separate variable and that will save you the recursive call to your function.
 
Share this answer
 
Comments
Titas93 15-Aug-13 3:58am    
Thanks for replying!
Understood your 1st point, and have applied that in my code!
Regarding the last line of your answer, could you please clarify it a bit! The basic problem I am facing is that, when the length of the minuend & the subtrahend would be equal, and the subtrahend would be greater than the minuend, I am getting an incorrect answer!
nv3 15-Aug-13 10:50am    
I would return the sign of the result in a separate bool variable (passed in a argument by a pointer to bool). The sign of the result is negative if your borr variable is true after the subtraction. And hence you don't need the nested call to your function anymore.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900