Addition of two matrices

I have many errors in the following code.Could you suggest a better way to overload '+' in this class.My way doesn't work at all.The other functions are nicely doing what they are meant to do.

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
class matrix;
int matrix::*v = &matrix.value;
class matrix
{
    
    int x;
    int y;


    int * value;


    void allocate()
    {

        cout<<"Input the order of the matrix:\n";
        cin>>x>>y;
        value = new int[x*y];
        order[0]=x;
        order[1]=y;
    }
   public:
    int order[2];

    void set_values()
    {
        int z(0);
        allocate();
        cout<<"Now put the values:\n>";
        for(z=0;z<(x*y);z++)
        {
            cin>>*(value+z);
            cout<<">";
        }

    }
    void out_value()
    {
        int m(0),z(0),count(0);
        cout<<"The value are..\n";
        for(z=0;z<x;z++)
        {
            for(m=0;m<y;m++)
            {
                cout<<*(value+count)<<"\t";
                count++;
            }
            cout<<endl;
        }
    }
    matrix operator+(matrix a)
    {
        matrix b;int z;
        if (order[0]==a.order[0]&&order[1]==a.order[1])
        {
                for(z=0;z<(x*y);z++)
                {
                    b->*v = a->*v + *v;
                }
        return b;
        }
        else {cout<<"Not conformable for addition!\n";}
    }

};
Last edited on
Get rid of line 2.

operator+ has to essentially do what allocate() does for b, except you know the dimensions
you need; you shouldn't ask the user.

That will then change your line 58.
In line 58 , I want to add all the data in *value of the argument object to the current matrix. What has allocate() got to do with it? I am pretty novice at using pointers, could you write the example code for line 58?
Each instance of matrix has a "value" member which is a pointer to an array of x*y elements.
You need to allocate that memory inside the "b" instance in operator+. Your allocate() method
does that (and unfortunately it also asks the user for input).

Once you do that, line 58 becomes b.value[ z ] = a.value[ z ] + value[ z ];
It shows a "segmentation fault" at runtime.
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<iostream>
using namespace std;
class matrix;
//int matrix::*v = &matrix.value;
class matrix
{

    int x;
    int y;


    int * value;


    void allocate()
    {

        cout<<"Input the order of the matrix:\n";
        cin>>x>>y;
        value = new int[x*y];
        order[0]=x;
        order[1]=y;
    }
   public:
    int order[2];

    void set_values()
    {
        int z(0);
        allocate();
        cout<<"Now put the values:\n>";
        for(z=0;z<(x*y);z++)
        {
            cin>>*(value+z);
            cout<<">";
        }

    }
    void out_value()
    {
        int m(0),z(0),count(0);
        cout<<"The value are..\n";
        for(z=0;z<x;z++)
        {
            for(m=0;m<y;m++)
            {
                cout<<*(value+count)<<"\t";
                count++;
            }
            cout<<endl;
        }
    }
    matrix operator+(matrix a)
    {
        matrix b;int z;
        if (order[0]==a.order[0]&&order[1]==a.order[1])
        {
                for(z=0;z<(x*y);z++)
                {
                    b.value[z]=a.value[z]+value[z];
                }
        return b;
        }
        else {cout<<"Not conformable for addition!\n";}
    }

};





when used with::
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "matrix_c.cpp"

#include <iostream>
using namespace std;
int main()
{
    matrix a,b,c;
    a.set_values();
    b.set_values();
    c=a+b;
    c.out_value();

}
You only did the second part of what I said. You have to do both parts. Read my
previous post again.
Only the last element of the array is added:
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
class matrix
{

public:
    matrix(int,int);
    matrix(){x=0;y=0;};
    int x;
    int y;
    int order[2];
    int * value;
    void allocate();
    void allocate(int,int);
    //    public:
    void set_values();
    void out_value();
    //int get_i(int z);
    //void set_i(int v,int z);
    matrix operator+(matrix a);//Whatever I try, this does not
    //seem to work
};

void matrix::allocate(int a,int b)
{
    value = new int[a*b];
    order[0]=a;
    order[1]=b;
    x=a;
    y=b;
}
void matrix::allocate()
{
    cout<<"Input the order of the matrix:\n";
    cin>>x>>y;
    value = new int[x*y];
    order[0]=x;
    order[1]=y;
}

matrix matrix::operator+(matrix a)
    {

       
        matrix c;
       
        int z;
        if (order[0]==a.order[0] && order[1]==a.order[1])
        {
                for(z=0;z<(x*y);z++)
                {

                   c.allocate(x,y);
                   c.value[z] = a.value[z] + value[z];
                }
        return c;
        }
        else {cout<<"Not conformable for addition!\n";}
    }


..Skipped the other parts..
Last edited on
Topic archived. No new replies allowed.