Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Can someone check my huge integer add function please and the negative add? I am trying to add 99+999 but it is giving me 98

Can someone check my huge integer add function please and the negative add?

I am trying to add 99+999 but it is giving me 98 instead of 1098

public HugeInteger add(HugeInteger h) { int checkRun = this.flag; int checkRun2 = h.flag; int bigNeg=0; if(h.flag==1) { h.flag = this.flag; h.size = h.size-1; bigNeg = this.compareTo(h); this.flag = checkRun; h.flag = checkRun2; h.size+=1; } else { this.flag = h.flag; this.size = this.size-1; ///pronlem bigNeg = this.compareTo(h); this.flag = checkRun; h.flag = checkRun2; this.size+=1; } //this ensures we use the bigger of the two strings to make a new hugeInteger int newLength = 0; newLength = this.size; int smallLength = h.size; if(h.size > this.size) { newLength = h.size; smallLength = this.size; } else if(h.size == this.size) { newLength = h.size+1; smallLength = this.size+1; } //new integer HugeInteger newHuge = new HugeInteger(newLength); //check sign int run= this.compareTo(h); switch (run) { case 1: newHuge.size = this.size; break; case -1: newHuge.size = h.size; break; default: newHuge.size = (this.size+h.size)/2; break; } //hold is the sum of the two strings elemets int hold = 0; //since we create a random string we need to fix it by zeroing it otherwise it ends up adding to the hold sum for(int i = 0 ; i-1))|| (this.multOn ==1)) { if(i>=smallLength && this.size=smallLength && this.size>h.size) { if(this.multOn==1) { hold=h.arrayHolder[i]+this.arrayHolder[i]; } else { hold =this.arrayHolder[i]; } } else { hold = h.arrayHolder[i] +this.arrayHolder[i]; } //we check if we need carry if(hold>9) { newHuge.arrayHolder[i] = hold%10+newHuge.arrayHolder[i]; newHuge.arrayHolder[i+1] =newHuge.arrayHolder[i+1]+ 1; } else if(hold+newHuge.arrayHolder[i]==10) { newHuge.arrayHolder[i+1] =newHuge.arrayHolder[i+1]+ 2; newHuge.arrayHolder[i] = 0; } //this is to stop from using the below else statement else if(hold == 0) {} //this will be the negative add //this will be the normal case where it just adds the two tofether else { newHuge.arrayHolder[i] = newHuge.arrayHolder[i]+ hold; } } else if(this.flag==1 || h.flag ==1) { if(bigNeg>-1) { //System.out.printf("this big h") ; if(i>=smallLength && this.size=smallLength && this.size>h.size) { hold =this.arrayHolder[i]; } else { hold = -h.arrayHolder[i] +this.arrayHolder[i]; } if(hold>0) { newHuge.arrayHolder[i]=newHuge.arrayHolder[i]+hold; } else if(hold==0) { newHuge.arrayHolder[i]=newHuge.arrayHolder[i]+hold; } else { newHuge.arrayHolder[i]=newHuge.arrayHolder[i]+hold+10; newHuge.arrayHolder[i+1]=newHuge.arrayHolder[i+1]-1; } newHuge.flag = this.flag; } else if(bigNeg==-1) { //System.out.printf("h big this") ; if(i>=smallLength && this.size=smallLength && this.size>h.size) { hold =this.arrayHolder[i]; } else { hold = h.arrayHolder[i] -this.arrayHolder[i]; } if(hold>=0) { newHuge.arrayHolder[i]=newHuge.arrayHolder[i]+hold; } else if(hold==0) { newHuge.arrayHolder[i]=newHuge.arrayHolder[i]+hold; } else { newHuge.arrayHolder[i]=newHuge.arrayHolder[i]+hold+10; newHuge.arrayHolder[i+1]=newHuge.arrayHolder[i+1]-1; } newHuge.flag= h.flag; } } } return newHuge; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

More Books

Students explore these related Databases questions