Help with passing array through function

I'm trying to get the inverse of a matrices. I pass the array through the function to get the inverse. Then I would like to print out that matrix. I having problems and I am lost on what is wrong especially conceptually.
I think two things are wrong, I don't think I properly returning inverse array back and I don't think I have printing of the array in main function correctly.
Thanks for any help





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
#include <iostream>
using namespace std;


//double pass[3][3]={{1,2,3},{0,1,4}, {5,6,0}} ;// the matrix that is entered by user

float inverse(float A[][3])

{
	 //float A[][3];// the matrix that is entered by user
     float B[3][3];//the transpose of a matrix A
     float C[3][3];//the adjunct matrix of transpose of a matrix A not adjunct of A
     float X[3][3];//the inverse
     int i,j;
     double x,n=0;//n is the determinant of A

     //cout<<"========== Enter matrix A =============================================\n";
     //for(i=0;i<3;i++)
     //{     cout<<endl;
     //     for(j=0;j<3;j++)
     //     {
     //          cout<<" A["<<i<<"]["<<j<<"]= ";
     //          cin>>A[i][j];
     //          B[i][j]=0;
     //          C[i][j]=0;
     //     }
     //}


          for(i=0,j=0;j<3;j++)
          {
               if(j==2)
               n+=A[i][j]*A[i+1][0]*A[i+2][1];
               else if(j==1)
               n+=A[i][j]*A[i+1][j+1]*A[i+2][0];
               else
               n+=A[i][j]*A[i+1][j+1]*A[i+2][j+2];
          }
          for(i=2,j=0;j<3;j++)
          {
               if(j==2)
               n-=A[i][j]*A[i-1][0]*A[i-2][1];
               else if(j==1)
               n-=A[i][j]*A[i-1][j+1]*A[i-2][0];
               else
               n-=A[i][j]*A[i-1][j+1]*A[i-2][j+2];
          }


    // cout<<"\n========== The matrix A is ==========================================\n";
     for(i=0;i<3;i++)
     {
          cout<<endl;
          for(j=0;j<3;j++)
          {
           A[i][j];   // cout<<" A["<<i<<"]["<<j<<"]= "<<A[i][j];
          }
     }
     cout<<endl<<endl;

     //cout<<"=====================================================================\n"<<endl;
     //cout<<"The determinant of matrix A is "<<n<<endl<<endl;
     //cout<<"====================================================================="<<endl;

     if(n!=0) x=1.0/n;
     else
     {
          cout<<"Division by 0, not good!\n";
          cout<<"=====================================================================\n"<<endl;
          return 0;
     }
     //cout<<"\n========== The transpose of a matrix A ==============================\n";
     for(i=0;i<3;i++)
     {
          //cout<<endl;
          for(j=0;j<3;j++)
          {

               B[i][j]=A[j][i];
              B[i][j];// cout<<" B["<<i<<"]["<<j<<"]= "<<B[i][j];

          }
     }
   //  cout<<endl<<endl;


     C[0][0]=B[1][1]*B[2][2]-(B[2][1]*B[1][2]);
     C[0][1]=(-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]=(-1)*(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]=(-1)*(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]=(-1)*(B[0][0]*B[1][2]-B[1][0]*B[0][2]);
     C[2][2]=B[0][0]*B[1][1]-B[1][0]*B[0][1];


     //cout<<"\n========== The adjunct matrix of transpose of the matrix A ==========\n";
     for(i=0;i<3;i++)
     {
          cout<<endl;
          for(j=0;j<3;j++)
          {
             C[i][j];//  cout<<" C["<<i<<"]["<<j<<"]= "<<C[i][j];

          }
     }
    // cout<<endl<<endl;


     for(i=0;i<3;i++)
     {
          for(j=0;j<3;j++)
          {
               X[i][j]=C[i][j]*x;

          }
     }



     //cout<<"\n========== The inverse matrix of the matrix you entered!!! ==========\n";
     for(i=0;i<3;i++)
     {     //cout<<endl;
          for(j=0;j<3;j++)
          {
            
			  X[i][j];// cout<<" X["<<i<<"]["<<j<<"]= "<<X[i][j];

          }
     }
     //cout<<endl<<endl;


return float X[3][3];
}
float main()
{
int i,j;
float pass[][3] = {{1,2,3},{0,1,4},{5,6,0}}; // the matrix that is entered by user
float * test;
test[][] = inverse(pass);
  cout<<"\n========== The inverse matrix of the matrix you entered!!! ==========\n";
     for(i=0;i<3;i++)
     {     
		 cout<<endl;
          for(j=0;j<3;j++)
          {
            cout<<" test["<<i<<"]["<<j<<"]= "<<endl;

          }
     }
}
float ?
float main() should return a code at the end (int main is standard). u know, as a way to describe how the program exited...

just some syntax help here as i'm not a fan of matrices :p

line 144 doesnt make sense. line 143 declares test as a pointer to a float and not a 2d array. even if test were a 2d array u also can't store whatever comes out from 'inverse' into an arbitrary element [][] (compile errors?)

line 137 returns exactly 1 value which is the element at X[3][3] - did u want the whole array ?

but u'r in luck ! pass is itself a 2d array and probably what u want to return (after being adjusted by that fun matrix math) from inverse() anyway.
so u can actually forget about test and just cout that pass[][] at the end instead then change the return type in that inverse function to bool for failure checking :D

(and cross ur fingers that it's the inverse matrix)

Last edited on
Topic archived. No new replies allowed.