Class not receiving correct values.

The matrix class is not receiving the correct values.The code compiles correctly but the [0][1] field value gets same as that of the [1][0] field.
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
#include<iostream>

using namespace std;
class matrix
{
    public:
    matrix(int dat[1][1])
        {
            data[0][0] = dat[0][0];
             data[1][0] = dat[1][0];
              data[0][1] = dat[0][1];
               data[1][1] = dat[1][1];
        }
    ~matrix(){}
    void setvalue(int dat[1][1])
    {
        data[0][0] = dat[0][0];
             data[0][1] = dat[0][1];
              data[1][0] = dat[1][0];
               data[1][1] = dat[1][1];
    }
    int getvalue(int x,short int y){return data[x][y];}
    int getdet(){return determinant();}
    private:
    int data[1][1];
    int determinant()
    {
        int x;
        x=data[0][0]*data[1][1]-data[0][1]*data[1][0];
        return x;
    }

};



int main()
{
    int a[1][1],c[1][1];
    a[0][0]=0;a[0][1]=0;a[1][0]=0;a[1][1]=0;
    matrix b(a);
    cout<<"Input the values\n";
    cin>>c[0][0];
    cin>>c[0][1];
    cin>>c[1][0];
    cin>>c[1][1];
    b.setvalue(c);
    cout<<"The given values are of the form:\n";
    cout<<"   "<<b.getvalue(0,0)<<"   "<<b.getvalue(0,1)<<"\n";
    cout<<"   "<<b.getvalue(1,0)<<"   "<<b.getvalue(1,1)<<"\n";
    cout<<"The determinant is:\n";
    cout<<b.getdet();
    cin.get();


}
closed account (D80DSL3A)
I see one problem which appears throughout the code. A 2x2 matrix should be declared as:
int dat[2][2]; not as int dat[1][1];. This applies not just to dat, but also to data, a and c. Good luck with the rest!
Topic archived. No new replies allowed.