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();
}
|