[HELP] Error debugging with functions

Hi guys, I need your help again..

I am remaking this code for my gain, and it seems that I cant debug the error..

Are there any comments on the structure of the code?
Why are there errors in calling the functions?

Any suggestion will be appreciated. Thank you.


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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <conio.h>
#include<math.h>

using namespace std;

float determinant(float a[65534][65534],int matDim);
void cofactor(float a[65534][65534], int f);
void transpose(float a[65534][65534],float [65534][65534],int r);

//////////////////////////////////////////////
float determinant(float a[65534][65534], int matDim)
{
  	float s=1,det=0,b[65534][65534];
  	int i,j,m,n,c;
  	if (matDim==1)
  	{
    	return (a[0][0]);
    }
    
  	else
    {
     	det=0;
     	for (c=0;c<matDim;c++)
       	{
        	m=0;
        	n=0;
        	for (i=0;i<matDim;i++)
          	{
            	for (j=0;j<matDim;j++)
              	{
                	b[i][j]=0;
                	if (i != 0 && j != c)
                 	{
                   		b[m][n]=a[i][j];
                   		if (n<(matDim-2))
                    	n++;
                	}
                   else
                    {
                     	n=0;
                     	m++;
                    }
            	}
          	}
          det = det + s * (a[0][c] * determinant(b,matDim - 1));
          s = (-1) * s;
          }
    }
    return det;
}

/////////////////////////////////////////////
/////////////////////////////////////////////
void cofactor(float num[65534][65534], int f)
{
 float b[65534][65534],fac[65534][65534];
 int p,q,m,n,i,j;
 for (q=0;q<f;q++)
 {
   for (p=0;p<f;p++)
    {
     m=0;
     n=0;
     for (i=0;i<f;i++)
     {
       for (j=0;j<f;j++)
        {
          if (i != q && j != p)
          {
            b[m][n]=num[i][j];
            if (n<(f-2))
             	n++;
            else
             	{
               	n=0;
               	m++;
            	}
          }
        }
     }
      fac[q][p] = pow(-1, (q + p)) * determinant(b, (f-1));
    }
  }
  transpose(num,fac,f);
  return ;
}
////////////////////////////////////////////
////////////////////////////////////////////
void transpose(float num[65534][65534],float fac[65534][65534], int r)
{
  int i,j;
  float b[65534][65534],inverse[65534][65534],d;
 
  for (i=0;i<r;i++)
    {
     	for (j=0;j<r;j++)
       	{
        	b[i][j]=fac[j][i];
    	}
    }
  	d=determinant(num,r);
  	
	for (i=0;i<r;i++)
    {
		for (j=0;j<r;j++)
		{
        	inverse[i][j]=b[i][j] / d;
        }
    }
	cout << "\n\n\nThe inverse of matrix is : \n";
 
   for (i=0;i<r;i++)
    {
     	for (j=0;j<r;j++)
       	{
         	cout << "\t" << inverse[i][j];
    	}
    	cout << endl;
    }
     
    return ;
}

////////////////////////////////////////////

int main()
{
	int i, j, k=0, m, iterNum = 1, iter, tempdim; //row, column, 
	int matDim;
	
	cout << "Enter dimensions: ";
	cin >> matDim;
	
	//input value using text file deliminated by white-space
	float tempo[matDim*matDim], d; // for file transfer
	
	float x[matDim], a[matDim][matDim], b[matDim], c, guess[matDim];
	float temp[matDim][matDim], tester[matDim], prod, xtemp[matDim];
	float error[matDim], err=0.0, errortolerance = 0.0000001;
	float posDef[3][matDim], posDef_1[matDim], sumposdef, posdefvalue[3]; // trial value..
	
	ifstream inputdata;

	inputdata.open("C:/Users/Engr. Jimmy Neutron/Documents/C++/Linear Methods/data.txt");	// opens the file

	if(!inputdata)			// file couldn't be opened
	{
		cerr << "Error: file not found or could not be opened" << endl;
		exit(1);
	}
	
	while (!inputdata.eof())		// keep reading until end-of-file
	{
		
		for (i=0; i<(matDim*matDim); i++)
			inputdata >> tempo[i];
	}

	inputdata.close();
	
	//output..

	//Data Process convert to an array of 

		for(i=0; i<matDim; i++)
		{
			for(j=0; j<matDim; j++)
			{	
				a[i][j]=tempo[k];
				k++;
			}
		}
		
		d = determinant(a,matDim);
		
		cout << "Determinant of the Matrix = " << d;
		
		if (d==0)
   			cout << "\nInverse of Entered Matrix is not possible\n";
  		else
	   		{
	   		cofactor(a,matDim);
	   		}
	/*//Testing file for output..
	for(i=0; i<matDim; i++)
	{
		for(j=0; j<matDim; j++)
		{
			cout << a[i][j] << endl;		
		}
	} */
	
	//Data process end

	//positive definite check..
	for(i=0; i<=2; i++)
	{
		
		for (j=0; j<matDim; j++)
		{
			if(i==0)
			posDef[i][j]= j;
			
			else if(i==1)
			posDef[i][j]= -(j+1);
			
			else if(i==2)
			{
				if( j%2 == 0)
				{
					posDef[i][j] = -j;
				}	
				else
				{
					posDef[i][j] = j;	
				}
			}
		}
	}
	
	cout << "\n\nEnter number of iterations: ";
	cin >> iter;
	
	
    //**** Check if positive definite matrix *****
    //********************************************
	
	for(i=0; i<matDim; i++)
	{
		sumposdef = 0.0;
		for(j=0; j<matDim; j++)
		{
			sumposdef = sumposdef + ( posDef[i][j] * a[j][i] );
		}
		posDef_1[i]  = sumposdef;
	}
	
	for(i=0; i<=2; i++)
	{
		for(j=0; j<matDim; j++)
		{
		posdefvalue[i] = posdefvalue[i] + ( posDef_1[i] * posDef[i][j] );
		}
	}
	//*************end Check*********************
	//*******************************************
    
    
    cout << "\nEnter the right hand side constants: \n";

	for(i=0; i<matDim ; i++)
	{
		cout << endl << "b[" << i + 1 << "]: ";
		cin >> b[i];
	}
	
    for(i=1; i<=3; i++)
    {
    	cout << "\n\nPositive-Definite Value: " << posdefvalue[i];
	}
    
    for(i=0; i<matDim ; i++)
	{
		if(a[i][i] == 0)
		{
			//Error checking - to do..
		}
	}
    
    cout << "\nEnter guess: \n";
	for(i=0; i<matDim ; i++)
	{
		cout << endl << "Guess[" << i + 1 << "]: ";
		cin >> guess[i];
	}
	
	do   //start an infinite loop..
	{
		for(i=0; i<matDim; i++)
		{
			// put 1 in all tester except the diagonal
			for(j=0; j<matDim; j++)  
			{
				tester[j]=1;
			}
			tester[i]=0;  
			//*******************************************
			
			prod = 0.0;
			
			for(j=0; j<matDim; j++)
			{
				prod = prod + (tester[j] * a[i][j] * guess[j]);
	        }
	        
	        x[i] = ( b[i] - prod) / a[i][i];	          
		}
		
		/*for (i=0; i<matDim; i++)
		{
			// if(x[i] == 0) {err = err;}
			error[i] = abs((x[i]-guess[i])/(x[i]+0.0000000001))*100.0;
			err = 	max(error[i], err);
			
		}
		if (err < errortolerance)
		{
			break; //end the loop if tolerance is met.
		}*/
		
		for (j=0; j<matDim; j++)
		{
			guess[j]=x[j];
		}
		iterNum++;  // count number of iterations
	} while(iter > iterNum);
	
	//} while (err > errortolerance);
	
	for (i=0; i < matDim; i++)
	{
		cout << std::fixed << std::setprecision(15);
		cout << endl << "x[" << i+1 << "] = " << x[i] << endl;
	}
	
	return 0;
}


here it is..

the error code is

178	27	[Error] cannot convert 'float (*)[(((sizetype)(((ssizetype)matDim) + -1)) + 1)]' to 'float (*)[65534]' for argument '1' to 'float determinant(float (*)[65534], int)'

186	24	[Error] cannot convert 'float (*)[(((sizetype)(((ssizetype)matDim) + -1)) + 1)]' to 'float (*)[65534]' for argument '1' to 'void cofactor(float (*)[65534], int)'
[/code]


"a" is declared as float a[matDim][matDim] as well as the 'd' variable.

I am using Orwell Dev C++
Last edited on
closed account (28poGNh0)
d = determinant(a,matDim); //ERROR GOES HERE, WHY????

Thats because you didnt declare d ,try float d;

cofactor(a,matDim); //ERROR GOES HERE? why?

same a is undeclared

Also you are overflowing the stack by declaring variable with a large size try to minimize it

Good look
Last edited on
Thanks for the reply Techno01.

actually, i have declared those at the start of in main function.
I just didn't write it down. :)
The error the compiler shows is something like

"Cannot convert float [blahh blahh] to float [blah blahhh]"

I am overflowing those stack because I don't know how to declare global variables. E.g
in the main function, the program will ask the user for the dimension of the matrix (ex. 6 x 6)

then declare the functions using that values. Is there a way to do that?

closed account (28poGNh0)
The whole code please
edited the question. :) any Suggestion?
Last edited on
Use std::vector<> http://www.mochima.com/tutorials/vectors.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
float determinant( const std::vector< std::vector<float> >& a );
// etc.

int main()
{
	int matDim;

	cout << "Enter dimensions: ";
	cin >> matDim;

	std::vector<float> tempo(matDim*matDim) ;

	std::vector<float> x(matDim), b(matdim), guess(matdim) ;
        
        std::vector< std::vector<float> > a( matDim, std::vector<float>(matDim) );

       // etc.
}

Thanks JLBorges. :)

You never cease to amaze me. :P
Topic archived. No new replies allowed.