Click here to Skip to main content
15,894,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Ok so Ive got 2 class, 1 that creates a Safe array without vectors and one that is a big number calculator. Independently my safe array class works fine. But my bigint calculator(which does compile) is giving me some seriously wonky values. My assign string and int functions seem to work sort of ok(except the int one prints the values backwards, and my assign string function prints out a bunch of wierd numbers after the correct ones) but my add function is giving me serious trouble. Any help? Thanks

C++
#include <iostream>
        using namespace std;
        
        
        template<typename Element>
        class SafeArray
        {
            int size;
            Element*Array;
            Element def;
            
        public:
            SafeArray()                         //default constructor(with no parameter)
            {
                Array = new Element[size];
                size = 10;
            }
            SafeArray(int value = NULL)         //constructor with one int
            {
                Array = new Element[value];
                size = 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)      //set method
            { if (pos<0)
            {
                cout<<"error";
            }
                if(pos>=size)
                { resize(pos+1); }
                Array[pos] = val; }
            
            void resize(int new_size)           //resize function
            {
                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;
            }
            //Element get_default()
            // {
            //     return def;
            // }
            int get_size()                       //get size
            {
                return size;
            }
        };
        
        
        static int size = 20;
        class bigint
        {
            
          
            SafeArray<int> *arr;
        public:
            bigint()
           {
               arr = new SafeArray<int>(size);
                for(int i =0;i<size;i++)
                    arr->set(i,0);
                
                
            }
            void assign(int num)
            {
                
                for(int i=size-1; i>=0;i--)
                { arr->set(i,num%10);
                    num /=10;
                    if(num!=0)
                        cout<<arr->get(i);}
            }
            void assign(string num)
            {
              //  int [size];
                for(int i = 0; i<=num.size();i++)
                {
                    arr->set(i,num[i]-48);
                }
                for(int i =0;i<size;i++)
                {
                    //if(num!=0)
                    cout<<arr->get(i);
                }
            }
            void addd(const bigint &A, const bigint &B)
            {
                int i;
                int result[size];
                for(i=0;i<=size;i++)
                {
                    result[i] = arr->get(i)+arr->get(i);
                }
                for(i=0;i<size-1;i++)
                {
                    int ca=result[i] / 10;
                    result[i]=result[i]%10;
                    result[i+1] = result[i+1]+ca;
                    if(result[i]!=0)
                    cout<<result[i+1];
                    
                }
                
            }
            void subtract(const bigint &A, const bigint &B)
            {
                int i;
                int result[size];
                for(i=0;i<=size;i++)
                {
                    result[i] = arr->get(i)-arr->get(i);
                }
                
            }
        
        };
        int main()
        {
            bigint A,B,C;
            A.assign(12345678);
            //B.assign(1234);
            cout<<endl;
            C.assign("12345678");
            cout<<endl;
            A.addd(A,C);
            return 0;
        }
Posted

I can see an immediate problem with your SafeArray class, the parameterless constructor will most likely throw an exception when you try to allocate using an unspecified size value. You also need to explain clearly what you mean by seriously wonky values, a bunch of wierd numbers, and serious trouble.

I would suggest you simplify this by using one of the standard collection classes for your number array, and using a BYTE type rather than int for your digits, as I suggested in your previous question on this subject. You should also spend some time with paper and pen, designing exactly how each calculation needs to operate. You can then turn those instructions into code much more easily. A set of rules for two digit numbers will scale up to any size without much effort.
 
Share this answer
 
Comments
nv3 8-Feb-15 6:11am    
My 5.
Obviously you have not taken on the advice I gave you in your last question. And you should take Richard's advice in solution 1 serious and work through your code either by pencil and paper or by running it in a debugger. Then you would have noticed that your function addd has some serious problems:

1) It does not refer to the parameters A and B at all! How could it possibly work.
In fact you are just adding the object's value to itself.

2) It does not return anything. Where are you planning to deposit the result.

3) The local variable result is not allocated in the correct size:
C++
static int size = 20;

    void addd(const bigint &A, const bigint &B)
    {
        int i;
        int result[size];
        ...

For one you should get a compiler warning, as size is not a const int. The size you are using here is a compile time constant. But your bigint class allows objects of different sizes. If you are working on a bigint with a larger size, this code will fail!

4) Your test output with cout only shows digits that are not equal to 0.
 
Share this answer
 
v2
Comments
Richard MacCutchan 8-Feb-15 6:30am    
Have a 5 back. :)

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