Home > Blockchain >  Static Multiplication of Vector Using C , Program not working
Static Multiplication of Vector Using C , Program not working

Time:01-03

I have got this assignment but this program is not working and output is not getting properly. This program compiles successfully, but it gives error - Segmentation fault (core dumpped). I am not getting why this is happening. Please tell me what is the problem in the below code -

    #include<iostream>
    using namespace std;
    class Vector
    {
        int *V;
        int size;
        public:
        Vector(int m)
        {
            V = new int[size=m];
            for(int i=0; i< size; i   )
            {
                V[i] = 0;
            }

        }
        Vector(const int *a)
        {
            for(int i=0;i<size;i  )
            {
                V[i] = a[i];
            }
        }
        int operator * (Vector &y)
        {
            int sum = 0;
            for(int i=0;i<size;i  )
            {
                sum = this->V[i] * y.V[i];
            }
            return sum;

        }
        void printVector()
        {
            for(int i=0;i<size;i  )
            {
                printf("%d\t",this->V[i]);
            }
        }
    };
    int main()
    {
        int x[3] = {1,2,3};
        int y[3] = {4,5,6};

        Vector V1(3);
        Vector V2(3);
        V1.printVector();
        V2.printVector();
        V1 = x;
        V2 = y;
        V1.printVector();
        V2.printVector();
        int r = V1*V2;
        cout<<endl<<"r = "<<r<<endl;
    }

CodePudding user response:

Vector(const int *a) does not initialize Vector::V, replace it with Vector(const int *a, int size) : Vector(size)

but you will have to replace

        V1 = x;
        V2 = y;

with

        V1 = Vector(x,3);
        V2 = Vector(y,3);

This code will result in a memory leak btw.

CodePudding user response:

Initialization of vector is should not be done in that way.

        V1 = x; // V1 is a Vector object, x  is an Integer array
        V2 = y;

These lines do not initialize a vector.

Here is the code after making the above changes:


    #include<iostream>
    using namespace std;
    class Vector
    {
        int *V;
        int size;
        public:
        Vector(const int *a, int m)    
        {
            V = new int[size=m];
            for(int i=0; i< size; i   )
            {
                V[i] = a[i];
            }

        }
        int operator * (Vector &y)
        {
            int sum = 0;
            for(int i=0;i<size;i  )
            {
                sum = this->V[i] * y.V[i];
            }
            return sum;

        }
        void printVector()
        {
            for(int i=0;i<size;i  )
            {
                printf("%d\t",this->V[i]);
            }
            cout << endl;
        }
    };
    int main()
    {
        int x[3] = {1,2,3};
        int y[3] = {4,5,6};

        Vector V1(x, 3);
        Vector V2(y, 3);
        V1.printVector();
        V2.printVector();
        int r = V1*V2;
        cout<<endl<<"r = "<<r<<endl;
    }

This should work.

Happy coding!

  • Related