Assign the values of one array to another or return it to the main().

How can I assign all the values of a two dimensional array to another empty one of the same order?
Just using new_array = old_array; does not work.
Should I use a pointer,if so, how?
Last edited on
Use a for loop.
And when they are in different functions?
Can you show us your code so we can see what you mean?
In the following program, I wish to write some functions which operate on two or more matrix(3x3) objects and return another object of the same category(or a 3x 3 array) , the values of which can be assigned to an array in the main().
Also it would be easier if I could write a function within the matrix class which returns the 'data[3][3]' private member, to be assigned to a variable in the main().
Finally could you suggest some alternatives to my use of 'goto' in some of my functions?

Here goes the code:( I have kept the main() empty for now, because for now I am using it for testing the other functions.)

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
#include<iostream>
#include<math.h>
using namespace std;
void ind_val_out(int mat[][3]);
void ind_val_in(int mat[][3]);
int cofactor(int data[][3],int x,int y);
class matrix_3x3;
void add(matrix_3x3 A,matrix_3x3 B);


class matrix_3x3
{
    public:
    void in_value(void)//For inputting the values
    {
        ind_val_in(data);
    };
    const void out_value(void)//For Console Output
    {
       ind_val_out(data);
    }
    const int getdet(void)//Returning the determinant
    {
        return det();
    }
    //Alternatives for the following 2 func.s needed
    //...for evaluating the entire data[3][3] at once
    int gid(int x,int y)//Getting the value of a single element
    {
        return data[x][y];
    }
    void sid(int x, int y, int z)//Setting the value of .....
    {
        data[x][y]=z;
    }


    private:
    int data[3][3];

    int det(void)//Calculating the Determinant
    {
        int determinant;
        int x,y,z;
        x=data[0][0]*cofactor(data,0,0);
        y=data[0][1]*cofactor(data,0,1);
        z=data[0][2]*cofactor(data,0,2);
        determinant=x+y+z;
        return determinant;
    }



};
int main()
{

    return 0;
}
//Would be better if the following 3 func.s could be done without goto
void ind_val_out(int mat[][3])//Total output of the matrix
{
    int x(0),y(0),z(0);
        abc:
            cout<<" "<<mat[x][y]<<" ";
            cout<<" "<<mat[x][y+1]<<" ";
            cout<<" "<<mat[x][y+2]<<"\n";
            z++;
            if (z<3)
            {
                x++;
                goto abc;
            }


}
void ind_val_in(int mat[][3])//Input........
{
    int x(0),y(0),z(0);
    cout<<"Input The values with respect to the rows:\n";

        abc:
            cin>>mat[x][y];
            cin>>mat[x][y+1];
            cin>>mat[x][y+2];
            z++;
            if (z<3)
            {
                x++;
                goto abc;
            }


}
int cofactor(int data[][3],int x,int y)//Calculating the cofactor
{
    int cofactor_v;
    int minor;
    int minor_mat[2][2];
    minor_mat[0][0]=data[(x+1)%3][(y+1)%3];
    minor_mat[1][1]=data[(x+2)%3][(y+2)%3];
    minor_mat[0][1]=data[(x+1)%3][(y+2)%3];
    minor_mat[1][0]=data[(x+2)%3][(y+1)%3];
    minor=minor_mat[0][0]*minor_mat[1][1]-minor_mat[1][0]*minor_mat[0][1];

    cofactor_v=pow((-1),(x+y+2)) * minor;//evaluating the sign
    if(y==1)//Only for the middle row
    {
        cofactor_v=0 - cofactor_v;
    }
    return cofactor_v;
}
//Would be better if the values could be returned instead of printed
void add(matrix_3x3 A,matrix_3x3 B)//Adding two matrices
{
    matrix_3x3 C;
     int x(0),y(0),z(0),v(0);
        abc:
            v=A.gid(x,y)+B.gid(x,y);
            C.sid(x,y,v);
            v=A.gid(x,(y+1))+B.gid(x,(y+1));
            C.sid(x,(y+1),v);
            v=A.gid(x,(y+2))+B.gid(x,(y+2));
            C.sid(x,(y+2),v);
            z++;
            if (z<3)
            {
                x++;
                goto abc;
            }
    C.out_value();
}

One way to remove the goto:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Would be better if the values could be returned instead of printed
void add(matrix_3x3 A,matrix_3x3 B)//Adding two matrices
{
    matrix_3x3 C;

     for(int x(0),y(0),z(0),v(0); z < 3; ++z, ++x)
     {
            v=A.gid(x,y)+B.gid(x,y);
            C.sid(x,y,v);
            v=A.gid(x,(y+1))+B.gid(x,(y+1));
            C.sid(x,(y+1),v);
            v=A.gid(x,(y+2))+B.gid(x,(y+2));
            C.sid(x,(y+2),v);
     }
    C.out_value();
}


You can return the data by passing a third matrix to the function that you create before calling add():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Adding two matrices returning data in the third
// NOTE you need to create matrix C before calling the function
// NOTE: you need to pass matrix_3x3 C by reference! &
void add(matrix_3x3 A,matrix_3x3 B,matrix_3x3& C)
{
     for(int x(0),y(0),z(0),v(0); z < 3; ++z, ++x)
     {
            v=A.gid(x,y)+B.gid(x,y);
            C.sid(x,y,v);
            v=A.gid(x,(y+1))+B.gid(x,(y+1));
            C.sid(x,(y+1),v);
            v=A.gid(x,(y+2))+B.gid(x,(y+2));
            C.sid(x,(y+2),v);
     }
}


EDIT:

Or, indeed, you could simply return matrix C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
matrix_3x3 add(matrix_3x3 A,matrix_3x3 B)
{
     matrix_3x3 C;
     for(int x(0),y(0),z(0),v(0); z < 3; ++z, ++x)
     {
            v=A.gid(x,y)+B.gid(x,y);
            C.sid(x,y,v);
            v=A.gid(x,(y+1))+B.gid(x,(y+1));
            C.sid(x,(y+1),v);
            v=A.gid(x,(y+2))+B.gid(x,(y+2));
            C.sid(x,(y+2),v);
     }
    return C;
}


Last edited on
There is an 'if' between the z++ and x++, so they are not incremented simultaneously.x++; is conditional(only 3 times of the total 9 run-times). That is the complication.

The returning works well. I did not think of declaring the function to the type of the class. I was trying to return arrays without a good knowledge of pointers; instead.
manasij7479 wrote:
There is an 'if' between the z++ and x++, so they are not incremented simultaneously.x++; is conditional(only 3 times of the total 9 run-times). That is the complication.


But the if(z < 3) statement always returns true except for the last time when z = 3. So x will always get incremented along with z except for the last time. But on the last time the loop terminates.

So the fact that x is incremented one less time does not affect anything because the loop does not happen in that event.
manasij7479 wrote:
The returning works well. I did not think of declaring the function to the type of the class. I was trying to return arrays without a good knowledge of pointers; instead.


Because you declared the array in your class as an array rather than a pointer all its elements will get copied when an object of your class gets copied. In fact because you pass your matrix objects into the function by value, they are already being copied with no ill effects.

However you could inprove performance a little by passing your parameters by reference using the & in your declaration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Now neither of the parameters will result in the 
// array being copied, the passed in versions will
// be used directly
matrix_3x3 add(matrix_3x3& A,matrix_3x3& B)
{
     matrix_3x3 C;
     for(int x(0),y(0),z(0),v(0); z < 3; ++z, ++x)
     {
            v=A.gid(x,y)+B.gid(x,y);
            C.sid(x,y,v);
            v=A.gid(x,(y+1))+B.gid(x,(y+1));
            C.sid(x,(y+1),v);
            v=A.gid(x,(y+2))+B.gid(x,(y+2));
            C.sid(x,(y+2),v);
     }
    return C;
}


It is better to make your reference parameters const if at all possible. To do this you will need to make sure any member functions that are called on const parameters are themselves const. That's probably something for a later time. But being what we call "const correct" is ultimately very important.
Last edited on
Topic archived. No new replies allowed.