Bigint Operations

Here I've got 2 classes, one is finished(a safeArray that I use for the second class which is what I have the problem with) In my bigint class I have add and subtract methods that work perfectly(i didnt include them below to save space.) But what I can't get to work is my multiplication method. The program compiles but doesnt return what I want. Any help would be greatly appreciated. Thanks





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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
using namespace std;


template<typename Element>
class SafeArray
{
    int size;
    Element*Array;
    Element def;
    
public:
    SafeArray()                         //default constructor(with no parameter)
    {
                size = 10;
        Array = new Element[size];

        
    }
    SafeArray(int value)         //constructor with one int
    {
        size = value;
        Array = new Element[value];
       
    }
    ~SafeArray() {
        delete [] Array;
    }                                       //destructor
    Element get(int pos)                    //get method
        { if (pos<0)
        {cout<<"error";}
    
            if(pos>=size)
            { set_default(def);}
            return Array[pos]; }

    void set(int pos, Element val)
    {
        if (pos<0)
        {
            cout<<"error";
            return;
        }
        if(pos>=size)
        {
            resize(pos+1);
        }
        Array[pos] = val;
    }
    void resize(int new_size)
    {
        Element*temp=new Element[new_size];
        for(int i = 0; i<size;i++)
        {temp[i]=Array[i];}
        delete[]Array;
        Array = temp;
        size=new_size;
    }
    void set_default(Element d)        //set_default
    {
        def=d;
    }
        int get_size()                       //get size
    {
        return size;
    }
};

int size = 100;

class bigint
{
    SafeArray<int> *arr;
public:
    bool sign;
bigint()                                                   //initializes to zero
    {
        arr = new SafeArray<int>;
        for(int i =0;i < size; i++)
            arr->set(i,0);
    }
    
void print()                                               //prints numbers without zeroes in front
    {
        bool start_num=false;
        for(int i = 0;i <arr->get_size() ;i++)
        {
            if(arr->get(i)!=0 && start_num==false )
            {start_num=true;
                cout << arr->get(i);}
         else if(start_num==true)
             cout<<arr->get(i);
            
        }
        
       cout<<endl;
    }
    
void assign(const bigint &A)                             //
    {
        for(int i=0;i<arr->get_size();i++)
        {                                                            //Ways to initialize stuff
            arr->set(i,A.arr->get(i));
        }
    
    }
    
void assign(int num)                                     //
    {
        for(int i = arr->get_size()- 1; i >= 0; i--)
        {
            arr->set(i,num%10);
            num /=10;
        }
       
        
    }
    
void assign(string num)                                  //
    {
        long len = num.length();
        int j=arr->get_size()-1;
        for(long i=len-1;i>=0;i--)
        {
            arr->set(j,num[i]-48);
            j--;
        }
    }
void multiply(const bigint &A)
    {
        bigint temp1; // bigint with value 0
        
        int carry = 0;
        int shift = 0;
         bigint temp2;
        for(int j = size-1; j >= 0; j--) //no member size in bigint
        {
            for(int i=size-1; i>=0; i--)
            {
                // bigint with value 0 and size: size + A.size
                int result = (arr->get(i)*A.arr->get(j)+carry);
                
                if(size - shift - (i - size-1) >= 0)
                    temp2.arr->set(size - shift - (i - size-1), result%10);
                else
                    break;

                carry=result/10;
            }
            shift++;
            temp1.add_pos(temp2);
        }
        this->assign(temp1);
    }


int main()
{
 bigint a,b;
a.assign("1234");
b.assign("5678");
a.multiply(b);
a.print();

return 0;
}
Last edited on
On line 131, you are not beginning with the value 0. A newly created bigint variable has unspecified values stored in this->arr.

Why is arr a pointer? Why is the def member of SafeArray never used outside of set_default? Why do the constructors not set it to some reasonable value?
Topic archived. No new replies allowed.