Got a working addition algorithm, but must tweak it... help?

Hi everybody!

Long story short:
1) Have assignment.
2) Need to make program to add numbers up to 40 digits long.
3) Made a class called HugeInteger, where numbers are stored in arrays. One digit per element.
4) Got my addition algorithm to work!

I show it to my professor, and she doesn't want me to use *this.

My addition algorithm resides at the very end of the source file.
So here we go!


HugeInteger.h
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
43
44
45
46
#ifndef HUGEINTEGER_H
#define HUGEINTEGER_H

#include <iostream>
#include <string>

using namespace std;

class hugeInteger 
{ 
// global out and in for overloading

    friend istream &operator>> (istream&, hugeInteger&); //input
    friend ostream &operator<< (ostream&, const hugeInteger&); //out

public:
//constructors
    hugeInteger();  // defualt constructor 
    hugeInteger (int [], int); // copy constructor takes in a array
			       // and int type

// addition and multiply functions
    hugeInteger add (const hugeInteger&); // add member function,
					  // which takes two
					  // hugeInterger type([]) and
					  // adds them together
    //hugeInteger mult (const hugeInteger&); 

// input and print functions
    hugeInteger input (char); // user input member function
    hugeInteger print(); // basic print function 

// operator overloading functions 
    bool operator> (const hugeInteger&a)const;
    bool operator< (const hugeInteger&a)const;
    bool operator== (const hugeInteger&a)const;
    bool operator!= (const hugeInteger&a)const;
    const hugeInteger operator+ (const hugeInteger&);
    const hugeInteger operator* (const hugeInteger&);
    
private:
    const static int maxSize = 40;// the max sz data member
    int array[maxSize + 1];// the array data member
    int size;// the size of the array data member
};
#endif 


HugeInteger.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "hugeInteger.h"
#include <iostream>
#include <cstdlib> 
#include <iomanip> 

using namespace std;

hugeInteger::hugeInteger() 
{
    for (int i = 0; i < maxSize; i++)
	array[i]= 0;  // defualt constuctor which initializes the
		      // array to 0
}

hugeInteger:: hugeInteger(int arr[], int b): size(b) // copy constructor
{
    if (size > maxSize)
	size = maxSize;  // if the size is greater than 40, it sets
			 // size to equal maxSize.
    for (int i = 0; i < size; i++)
	array[i] = arr[i]; 

    for (int i = size; i < maxSize; i++)
	array[i] = 0;  

    for (int j = maxSize - 1; j >= maxSize - size; j--)
	array[j] = array[j - maxSize + size];

    for (int j = 0; j< maxSize - size; j++)
	array[j] = 0;
}



const hugeInteger hugeInteger:: operator+(const hugeInteger &a)// overload +
{
    return (add (a)); 
}
	
hugeInteger hugeInteger::print() 
{ 
    bool term = false;
    int count;

    //  check for 0's and omit using a for loop
    for ( int i = 0; i < maxSize; i++ ){
	if (array[i] != 0){ 
	    term = true;
	    count = i;
	    break;
	} 
    }
    for (int i = count; i < maxSize; i++)
	cout << array[i];
}

hugeInteger hugeInteger::add(const hugeInteger &a)
{
    for (int i = maxSize - 1; i >= 0; i--){
	array[i] = array[i] + a.array[i];

	if (array[i] >= 10){
	    int carry = array[i]/10;
	    int remain = array[i]%10;
	    array[i] = remain;
	    array[i-1] = array[i-1] + carry;
	}
    }   
    return(*this);
}


driver.cpp
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
#include "hugeInteger.h"
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{
    int aop1[5] = {3,7,1,3,7};
    int aop2[5] = {1,5,9,3,4};
    hugeInteger sum; // makes an object using the default contructor
		     // from .h file, which will eventually hold the
		     // objects op1, and op2
    hugeInteger op1(aop1, 5) , op2(aop2, 5); // two objects using copy contruct

    op1.print();
    cout << endl;
    op2.print();
    cout << endl;
    //sum = op1.add(op2); // will add objects op1, op2 together 

    sum = op1 + op2; // after overloading operator +, sum contains
		     // op1,op2
    sum.print(); 
    cout << endl;
}



Now for the juicy part:

When I run this code I get the following output:

37137
15934
53071

It works correctly! :)



However, to abide by my professor's request, I've changed the addition algorithm to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
hugeInteger hugeInteger::add(const hugeInteger & a)
{
    hugeInteger result;

    for (int i = maxSize - 1; i >= 0; i--){
	result.array[i] = array[i] + a.array[i];

	if (array[i] >= 10){
	    int carry = result.array[i]/10;
	    int remain = result.array[i]%10;
	    result.array[i] = remain;
	    result.array[i-1] = array[i-1] + carry;
	}
    }   
    return(result);
}


But this time, I get the following (incorrect) output!

37137
15934
41210611

What is the reason for this? I don't believe I have any logic errors. The only difference between the two algorithms, is that in the first (correctly working) algorithm, I only had two arrays. In this modified one, I store the sum values in a third array. But I get a different output this way.

What gives?
Last edited on
This hugeInteger(int arr[], int b): size(b) // copy constructor is not a copy constructor. The declaration should be hugeInteger(const hugeInteger&);
Last edited on
Topic archived. No new replies allowed.