Matrix Problem

closed account (owCSLyTq)
Hello, i'm having trouble with the cofactors while compiling. can you tell me the problem?
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include<iostream>
using namespace std;

int main()
{
int add(int matA[3][3],int matB[3][3]);
int sub(int matA[3][3],int matB[3][3]);
int mul(int matA[3][3],int matB[3][3]);
int Tr(int matA[3][3]);
int det(int matA[3][3]);
int Trans(int matA[3][3]);
int cofactor(int mat[3][3]);
int inv(int matA[3][3]);
int solve(int matA[3][3],int b[3]);

int x,A[3][3],B[3][3],b[3],C[3][3];
cout<<"This program allows you to perform certain matrix and vector operations on a 3x3 matrix"<<endl;
cout<<"If you wish to add two matrices, subtract one matrix from another one, multiply two matrices, calculate the trace, the determinant, transpose, adjoint, inverse or solve a linear system of equations, press 1,2,3,4,5,6,7,8 or 9, respectively";

cin>>x;
switch(x){

case 1:
	{
cout<<"enter matrix 1";
cin>>A[3][3];
cout<<"enter matrix 2";
cin>>B[3][3];
cout<<"the sum is "<<add(A,B);
	break;
	}
case 2:
	{
cout<<"enter matrix 1";
cin>>A[3][3];
cout<<"enter matrix 2";
cin>>B[3][3];
cout<<"the difference is"<<sub(A,B);
	break;
	}
case 3:
	{
cout<<"enter matrix 1";
cin>>A[3][3];
cout<<"enter matrix 2";
cin>>B[3][3];
cout<<"the product is"<<mul(A,B);
	break;
	}
case 4:
	{
cout<<"enter your matrix";
cin>>A[3][3];
cout<<"the trace is "<<Tr(A);
	break;
	}
case 5:
	{
cout<<"enter your matrix";
cin>>A[3][3];
cout<<"the determinant is"<<det(A);
	break;
	}
case 6:
	{
cout<<"Enter your matrix";
cin>>A[3][3];
cout<<"The transpose is"<<Trans(A);
	break;
	}
case 7:
	{
cout<<"Enter your matrix";
cin>>A[3][3];
cout<<"The adjoint is"<<Trans(cofactor(A));
	break;
	}
case 8:
	{
cout<<"Enter your matrix";
cin>>A[3][3];
cout<<"The inverse is"<<inv(A);
	break;
	}
case 9:
	{
cout<<"Do you want to solve an equation in the form Ax=b?" <<endl;
cout<<"Enter your matrix A";
cin >>A[3][3];
cout<<"Enter your vector b";
cin>>b[3];
cout<<"The solution is"<<inv(A)*b[3];
	break;
	}

default:
	{
cout<<"Choose a number between 1 and 9 inclusive";
	}
}


int add(int matA[3][3],int matB[3][3]);
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
return A[i][j]+B[i][j];
}
}
}
	
int sub(int matA[3][3],int matB[3][3]);
{
for (int i=0;i<3;i++)
{
for(int j =0;j<3;j++)
{
return A[i][j]-B[i][j];
}
}
}

int mul (int matA[3][3],int matB[3][3],int matC[3][3]);
{
	int i;int j;int k;
	
for (int i=0; i<3;i++)
for (int j=0;j<3;j++)
for (int k=0;k<3;k++)
C[i][j]=0;
C[i][j]+=A[i][k]*B[k][j];
{
return C[i][j]; 
}
}

int Tr(int matA[3][3]);
{ return A[0][0]+A[1][1]+A[2][2];

}

int det(int matA[3][3]);
{
	return  (A[0][0]*((A[1][1]*A[2][2])-(A[1][2]*A[2][1])))-(A[0][1]*((A[1][0]*A[2][2])-(A[1][2]*A[2][0]))+(A[0][2]*((A[1][0]*A[2][1])-(A[1][1]*A[2][0]))));

}

int Trans(int matA[3][3]);
{
int i;int j;
for (int i=0;i<3;i++)
 for(int j=0;j<3;j++)
B[i][j]=A[j][i];
return B[i][j];
}


int cofactor(int matB[3][3]);
{
	return;
	{

	C[0][0]=B[1][1]*B[2][2]-B[2][1]*B[1][2];
	C[0][1]=-(B[1][0]*B[2][2]-B[2][0]*B[1][2]);
	C[0][2]=B[1][0]*B[2][1]-B[2][0]*B[1][1];
	C[1][0]=-(B[0][1]*B[2][2]-B[2][1]*B[0][2]);
	C[1][1]=B[0][0]*B[2][2]-B[2][0]*B[0][2];
	C[1][2]=-(B[0][0]*B[2][1]-B[2][0]*B[0][1]);
	C[2][0]=B[0][1]*B[1][2]-B[1][1]*B[0][2];
	C[2][1]=-(B[0][0]*B[1][2]-B[1][0]*B[1][2]);
	C[2][2]=B[0][0]*B[1][1]-B[1][0]*B[0][1];

}

}
int inv(int matA[3][3]);
{if (det<=0)
	cout<<"the inverse does not exist";
	else return (Trans(cofactor(A)))/det;
}


int solve(int matA[3][3],int vectb[3]);
{
	return inv(A)*b[3];
}
system("PAUSE");
}
Does return; in your cofactor method make sense? SHould their be a semicolon after return if you're trying to return a value?
Last edited on
i'm having trouble while compiling

A compiler error? What error?
closed account (owCSLyTq)
cout<<"The adjoint is"<<Trans(cofactor(A));

1>g:\matrix project.cpp(75): error C2664: 'Trans' : cannot convert parameter 1 from 'int' to 'int [][3]'


the return; in cofactor and the int main() above are causing the errors when debugging and i don't see how to solve it.
Last edited on
You claim that cofactor() returns an int. It probably should not. Define it as:
void cofactor( int matB[3][3], int matC[3][3] )

Then you obviously need a int [3][3] that you pass to cofactor() and Trans().

Do not declare functions inside main(). Do it before main().
closed account (owCSLyTq)
what do you mean that "Then you obviously need a int [3][3] that you pass to cofactor() and Trans()."

Thanks :)
Lets ignore that for now.

Given that you have int A[3][3];, what does this piece of your code do?
cin>>A[3][3];
closed account (owCSLyTq)
It's a 3x3 array to find matrix A.
The cin>>A[3][3]; ?

That statement extracts one integer from input stream object 'cin' and stores it into memory location at A+9. Too bad the matrix referred to by name 'A' covers only locations from A to A+8. We have no idea what is in A+9, i.e. what does that statement accidentally overwrite.

These Fora have several recent threads about reading data into (2D) arrays. Browse some.
closed account (owCSLyTq)
I wrote another program today. But now I can find a solution to how to solve the linear problem. It keep saying break continue.
Topic archived. No new replies allowed.