trouble with big int addition, need assistance

hey im writing a class of bigint but im having trouble doing the addition for it. can anyone help me? the addition works ok but it is buggy. for example, anytime there are 3 numbers in a row that are the same, it won't peform a carry. it also gets buggy when adding a 9 to 9. please help me if you have any suggestion.

ONE OF THE BIGGEST problem is that i stored the value of each number in a array and add up each element in reverse other. however the problem is that if one array has more numbers than the other, the one with less gets buggy.

for ex. say int john[4] stored {1,2,3,4} and int tom[2] stored {1, 2}
when it does 1234
+ 12
--------
it gets buggy because for ex john[2] + tom[0] = 4
but when it gets to john[1], there is no more element of tom for john[1] to add on and so it takes negative elements for tom like tom[-1] and adds that to john[1] and i get some weird numbers


plase help, heres the function in my class defintion:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//function that performs addition on the two BigInts
string BigInt::plus(BigInt passedv1)
{	
	lengthCheck(passedv1.getValue());	//compare length of the two operands		
	int carry = 0; //carry over from addition
	int result[max.length()];  //creates an empty array to store result of operation	
	cout << "addition: "; //tells user what operation it performed
	for (int i = 0; i < max.length(); i++)
	{			
		/* takes the numeric value of the each element of max and min, starting in reverse order
			 from the end, and adds them up. the -'0' is to convert the ascii value to integer value */			
		result[(max.length()-1)-i] = (max[(max.length()-1)-i]-'0') + (min[(min.length()-1)-i]-'0') + carry;	
		if(result[(max.length()-1)-i] > 9) // if sum of elements are > 10, there is carry over
			{
				result[(max.length()-1)-i] = result[(max.length()-1)-i] - 10;
				carry = 1;
			}
		else
			{
				result[(max.length()-1)-i]  = result[(max.length()-1)-i];	
				carry = 0;			
			}		
		/*if there are more numbers in max than in min, then max[n-i element] will be the value at the n-i element,
		but valueof min[n-i] element does not exist and so it takes on a negative value. */
		if((min[(min.length()-1)-i]-'0') < 0) //if there are no more elements of min to add to max
			{											
				
					result[(max.length()-1)-i] = (max[(max.length()-1)-i]-'0') + carry;
					carry = 1;
							
			}			
		
		
	}
	for (int i = 0; i < max.length(); i++)
	{
		cout << result[i];
	}	
	cout << endl;
			
	return "";
}
Topic archived. No new replies allowed.