Matrix Determinate Calculation

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 1
Reputation: defaultAZN is an unknown quantity at this point 
Solved Threads: 0
defaultAZN defaultAZN is offline Offline
Newbie Poster

Matrix Determinate Calculation

 
0
  #1
Feb 19th, 2008
hey,
I'm trying to code a program that will accept a text file input for the dimension and elements of a matrix and store that to an array. Then the program will calculate the determinate of the matrix and output it to both the console and a file. I'm having problems specifically with storing the text file to an array and calculating the determinate.

here is what i have so far:
  1. // This program evaluates determinants of square matrices using minors.
  2. //
  3. // The program can handle up to nine by nine square matrices.
  4. //
  5. // This program reads input from a file and writes identical output to
  6. // both the console and to a file.
  7. //
  8. // The program reads a matrix dimension and the required number of matrix
  9. // elements, writes the matrix in columns, calculates the determinant,
  10. // and writes the value of the determinant. The process repeats until
  11. // an end of file is encountered or an invalid dimension is specified.
  12. //
  13. // After each determinant value is written a blank line is written.
  14. //
  15. // After all processing is completed, the termination message "Done."
  16. // is written.
  17. //
  18. // If the input or output file cannot be opened, the program terminates without
  19. // any other action than displaying an error message.
  20.  
  21. // Examples
  22. //
  23. // DETERMINANT EVALUATOR
  24. //
  25. // Matrix dimension:2
  26. //
  27. // Input values:
  28. // 4.0000
  29. // -3.0000
  30. // 2.0000
  31. // 6.0000
  32. //
  33. // Matrix:
  34. // 4.0000 -3.0000
  35. // 2.0000 6.0000
  36. //
  37. // Determinant value:30.0000
  38. //
  39. // Matrix dimension:4
  40. //
  41. // Input values:
  42. // 0.1000
  43. // 1.3000
  44. // -3.5000
  45. // 2.8000
  46. // 1.7000
  47. // -3.5000
  48. // 2.4000
  49. // -4.1000
  50. // 2.3000
  51. // 0.0000
  52. // -1.6000
  53. // 3.9000
  54. // 1.2000
  55. // -2.7000
  56. // -0.5000
  57. // 1.0000
  58. //
  59. // Matrix:
  60. // 0.1000 1.3000 -3.5000 2.8000
  61. // 1.7000 -3.5000 2.4000 -4.1000
  62. // 2.3000 0.0000 -1.6000 3.9000
  63. // 1.2000 -2.7000 -0.5000 1.0000
  64. //
  65. // Determinant value:-72.8371
  66. //
  67. // Done.
  68.  
  69.  
  70.  
  71. //#define SHOW_DETERMINANTS
  72.  
  73.  
  74. #include <iostream>
  75. #include <fstream>
  76. #include <iomanip>
  77. #include <string>
  78. #include <stdlib.h>
  79. #include <conio.h>
  80. #include <math.h>
  81. using namespace std;
  82.  
  83.  
  84. // **************************************************************************
  85. // * *
  86. // * GLOBAL CONSTANTS *
  87. // * *
  88. // *************************************************************************
  89.  
  90. const string INFILE_NAME = "File4.In";
  91. const string OUTFILE_NAME = "File4.Out";
  92.  
  93. const int MAX_DIM = 9; // Maximum matrix dimension
  94. const int PRECISION = 4; // Precision of double output
  95. const int OUTPUT_WIDTH = 10; // Width of double output
  96.  
  97.  
  98. // **************************************************************************
  99. // * *
  100. // * GLOBAL VARIABLES *
  101. // * *
  102. // **************************************************************************
  103.  
  104. ifstream fin; // Input file
  105. ofstream fout; // Output file
  106. bool ineof; // End of file indicator
  107. int dimension;
  108. int det;
  109. int row, column;
  110. double minor;
  111.  
  112.  
  113.  
  114. // Function declarations
  115. bool initialize(); // Initialize the program
  116.  
  117. bool getInput // Get the input matrix
  118. (double mat[][MAX_DIM], // Matrix
  119. int &dim); // Matrix dimension
  120.  
  121. void displayMatrix // Display the matrix
  122. (double mat[][MAX_DIM], // Matrix
  123. int dim); // Matrix dimension
  124.  
  125. double evaluate // Evaluate a determinant
  126. (double mat[][MAX_DIM], // Matrix
  127. int dim); // Matrix dimension
  128.  
  129. void finish(); // Terminate the program
  130.  
  131. bool process1; // Processing indicator
  132. bool process2; // Processing indicator
  133.  
  134. int size; // Matrix dimension
  135.  
  136. double answer; // Value of determinant
  137. double mat[MAX_DIM][MAX_DIM]; // Matrix
  138.  
  139. int calcminor(double mat[][MAX_DIM], minor, int row, int column);
  140.  
  141. int calcdet(det *a) {
  142. int result=0;
  143. matrix minor;
  144.  
  145. if(dimension < 1)
  146. return 0;
  147. if(dimension == 1) { // stopping condition
  148. return mat[0][0]; //returns 0,0 element
  149. }
  150. for(int i=0; i < dimension; i++){
  151. if(!calcminor(a,&minor,0,i))
  152. return 0;
  153. result += ( pow(-1,i) * mat[0][i] * calcdet(&minor));
  154. }
  155. return result;
  156. }
  157.  
  158. // **************************************************************************
  159. // * *
  160. // * MAIN PROGRAM *
  161. // * *
  162. // **************************************************************************
  163.  
  164. int main()
  165. {
  166. // Initialize
  167. process1 = initialize();
  168.  
  169. // If the files were opened successfully
  170. if (process1)
  171. {
  172. // Do
  173. do
  174. {
  175. // Get the input equations
  176. process2 = getInput(mat, size);
  177.  
  178. // If the input is valid
  179. if (process2)
  180. {
  181. // Display the matrix
  182. displayMatrix(mat, size);
  183.  
  184. // Evaluate the determinant
  185. answer = evaluate(mat, size);
  186.  
  187. // Report the value of the determinant
  188. cout << "Determinant value:" << answer << endl;
  189. fout << "Determinant value:" << answer << endl;
  190.  
  191. // Write a blank line
  192. cout << endl;
  193. fout << endl;
  194. }
  195. }
  196. // While the input is valid
  197. while (process2);
  198.  
  199. // Finish
  200. finish();
  201. }
  202.  
  203. // Return
  204. return 0;
  205. }
  206.  
  207.  
  208.  
  209. // **************************************************************************
  210. // * *
  211. // * INITIALIZE FUNCTION *
  212. // * *
  213. // **************************************************************************
  214.  
  215. bool initialize() // Initialize program
  216. {
  217. bool success; // Initialization success indicator
  218.  
  219.  
  220. // Set the success indicator false
  221. success = false;
  222.  
  223. // Initialize the end of file false
  224. ineof = false;
  225.  
  226. // Open the input file
  227. fin.open(INFILE_NAME.c_str(), ios::in);
  228.  
  229. // If the file was not opened successfully
  230. if (!fin)
  231. {
  232. // Set the end of file indicator true
  233. ineof = true;
  234.  
  235. // Display an error message
  236. cout << "ERROR: Unable to open input file \""
  237. << INFILE_NAME << "\"" << endl;
  238. }
  239.  
  240. // Else
  241. else
  242. {
  243. // Open the output file
  244. fout.open(OUTFILE_NAME.c_str(), ios::out);
  245.  
  246. // If the output file was not opened successfully
  247. if (!fout)
  248. {
  249. // Set the end of file indicator true
  250. ineof = true;
  251.  
  252. // Close the input file
  253. fin.close();
  254.  
  255. // Display an error message
  256. cout << "ERROR: Unable to open output file \""
  257. << OUTFILE_NAME << "\"" << endl;
  258. }
  259. }
  260. // If there is valid input
  261. if (!ineof)
  262. {
  263. // Set the success indicator true
  264. success = true;
  265.  
  266. // Set the output format
  267. cout.setf(ios::fixed);
  268. cout.setf(ios::showpoint);
  269. cout.precision(PRECISION);
  270. fout.setf(ios::fixed);
  271. fout.setf(ios::showpoint);
  272. fout.precision(PRECISION);
  273.  
  274. // Display a title
  275. cout << "DETERMINANT EVALUATOR" << endl;
  276. fout << "DETERMINANT EVALUATOR" << endl;
  277.  
  278. // Display a blank line
  279. cout << endl;
  280. fout << endl;
  281. }
  282.  
  283. // Return the initialization success
  284. return success;
  285. }
  286.  
  287.  
  288. // **************************************************************************
  289. // * *
  290. // * GET INPUT FUNCTION *
  291. // * *
  292. // **************************************************************************
  293.  
  294. bool getInput // Get the input matrix
  295. (double mat[][MAX_DIM], // Matrix
  296. int &dim) // Matrix dimension
  297. {
  298. bool valid; // Valid input indicator
  299. int r; // Row number
  300. int c; // Column number
  301.  
  302. valid = false;// Set the valid input indicator false
  303. fin >> dimension;// Get the matrix dimension
  304. if (dimension <= 9) {// If the input is good
  305. cout << "Matrix dimension:" << dimension << endl;// Display a header for the matrix dimension
  306. // Echo the input
  307. cout << endl;// Display a blank line
  308. }
  309. if (dimension < 2) {// If the dimension is less than 2
  310. cerr << "Error: Matrix dimension too small." << endl;// Display an error message
  311. }// Display a blank line
  312. else if (dimension > 9) {// Else if the dimension is too large
  313. cerr << "Error: Matrix dimension too large." << endl;// Display an error message
  314. }// Display a blank line
  315. else {// Else
  316. cout << "Input values:";// Display a header for the input values
  317. for (r = 0; r < (dimension -1); r++) {
  318. for (c = 0; c < (dimension - 1); c++) {// Enter the data for each row
  319. fin >> mat[r][c];// Enter the data for one row
  320. // Get one matrix element
  321. // If the input is good
  322. cout << mat[r][c];// Echo the value
  323. }
  324. }
  325. // If the input is all good
  326. valid = true;// Set the valid input indicator true
  327. cout << endl;// Display a blank line
  328. }
  329. // Return the result
  330. return valid;
  331. }
  332.  
  333.  
  334.  
  335. // **************************************************************************
  336. // * *
  337. // * DISPLAY MATRIX FUNCTION *
  338. // * *
  339. // **************************************************************************
  340.  
  341. void displayMatrix // Display the matrix
  342. (double mat[][MAX_DIM], // Matrix
  343. int dim) // Matrix dimension
  344. {
  345. int r; // Row number
  346. int c; // Column number
  347.  
  348.  
  349. cout << "Matrix:" << endl;// Display a matrix header
  350. for (r = 0; r < dimension; r++) {// Display all rows
  351. for (c = 0; c < dimension; c++) {// Display one row
  352. cout << mat[r][c];// Display one matrix element
  353. cout << endl;// End the line
  354. }
  355. }
  356. cout << endl;// Display a blank line
  357. }
  358.  
  359.  
  360.  
  361. // **************************************************************************
  362. // * *
  363. // * EVALUATE DETERMINANT FUNCTION *
  364. // * *
  365. // **************************************************************************
  366.  
  367. double evaluate // Evaluate a determinant
  368. (double mat[][MAX_DIM], // Matrix
  369. int dim) // Matrix dimension
  370. {
  371. int result=0;
  372. matrix;
  373. minor;
  374. int i, j;
  375.  
  376. if(dimension < 1)
  377. return 0;
  378.  
  379. if(dimension == 1) // stopping condition
  380. return mat[0][0]; //returns 0,0 element
  381.  
  382. for(int i = 0; i < dimension; i++) {
  383. if(!calcminor(det, &minor, 0, i))
  384. return 0;
  385. result+=( pow(-1,i)*mat[0][i]*calcdet(&minor));
  386. }
  387.  
  388. return result;
  389. }
  390.  
  391.  
  392.  
  393. // **************************************************************************
  394. // * *
  395. // * FINISH FUNCTION *
  396. // * *
  397. // **************************************************************************
  398.  
  399. void finish() // Terminate the program
  400. {
  401. // Display a termination message
  402. cout << "Done." << endl;
  403. fout << "Done." << endl;
  404.  
  405. // Close the input file
  406. fin.close();
  407.  
  408. // Close the output file
  409. fout.close();
  410. }
  411.  
  412.  
  413.  
  414. // **************************** SUPPORT FUNCTION ****************************
  415.  
  416.  
  417. // **************************************************************************
  418. // * *
  419. // * DISPLAY DETERMINANT FUNCTION *
  420. // * *
  421. // **************************************************************************
  422.  
  423. void displayDeterminant // Display a determinant
  424. (double mat[][MAX_DIM], // Matrix
  425. int dim, // Matrix dimension
  426. double det) // Value of determinant
  427. {
  428. int r; // Row number
  429. int c; // Column number
  430.  
  431. // Display a matrix header
  432. cout << "Determinant:" << endl;
  433. fout << "Determinant:" << endl;
  434.  
  435. // Display all rows
  436. for (r = 0; r < dim; r++)
  437. {
  438. // Display an initial bar
  439. cout << "| ";
  440. fout << "| ";
  441.  
  442. // Display one row
  443. for (c = 0; c < dim; c++)
  444. {
  445. // Display one matrix element
  446. cout << setw(OUTPUT_WIDTH) << mat[r][c];
  447. fout << setw(OUTPUT_WIDTH) << mat[r][c];
  448. }
  449.  
  450. // Display a terminal bar
  451. cout << " |";
  452. fout << " |";
  453.  
  454. // End the line on the console
  455. cout << endl;
  456. fout << endl;
  457. }
  458.  
  459. // Display a blank line
  460. cout << endl;
  461. fout << endl;
  462.  
  463. // Display the determinant value
  464. cout << "Subdeterminant value:" << det << endl;
  465. fout << "Subdeterminant value:" << det << endl;
  466.  
  467. // Display a blank line
  468. cout << endl;
  469. fout << endl;
  470. }
  471.  
  472.  
  473. calcminor(mat, minor, int& row,int& column) {
  474. int p = 0,q = 0;
  475. if(dimension <= 1) {
  476. return 0;
  477. }
  478. if(!create(minor, dimension - 1)) {
  479. return 0;
  480. }
  481. for(int i=0; i < dimension; i++)
  482. if(i!= row){
  483. q=0;
  484. for(int j=0; j < dimension;j++)
  485. { if(j!=column)
  486. minor->mat[p][q]=mat[i][j];
  487. q++;
  488. }
  489. p++;
  490. }
  491. return 1;
  492. }

I am using Visual Studio C++ 2005 express edition. Here is the build log:
1>------ Build started: Project: Matrix Evaluator, Configuration: Debug Win32 ------
1>Compiling...
1>matrix evaluator.cpp
1>.\matrix evaluator.cpp(139) : error C2061: syntax error : identifier 'minor'
1>.\matrix evaluator.cpp(141) : error C2065: 'a' : undeclared identifier
1>.\matrix evaluator.cpp(141) : error C2448: 'calcdet' : function-style initializer appears to be a function definition
1>.\matrix evaluator.cpp(372) : error C2065: 'matrix' : undeclared identifier
1>.\matrix evaluator.cpp(383) : error C2660: 'calcminor' : function does not take 4 arguments
1>.\matrix evaluator.cpp(385) : error C2668: 'pow' : ambiguous call to overloaded function
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\math.h(575): could be 'long double pow(long double,int)'
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\math.h(527): or 'float pow(float,int)'
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\math.h(489): or 'double pow(double,int)'
1> while trying to match the argument list '(int, int)'
1>.\matrix evaluator.cpp(385) : error C3861: 'calcdet': identifier not found
1>.\matrix evaluator.cpp(473) : error C2062: type 'int' unexpected
1>.\matrix evaluator.cpp(473) : error C2143: syntax error : missing ';' before '{'
1>.\matrix evaluator.cpp(473) : error C2447: '{' : missing function header (old-style formal list?)
1>Matrix Evaluator - 10 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Thanks in advance for any help.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Matrix Determinate Calculation

 
0
  #2
Feb 20th, 2008
Start at the top. Fix the first error. Compile. Lather, rinse, repeat until no errors or warnings.
*** {BD Software Proxy c++ v3.43a for gcc} STL Message Decryption is ON! ***
main.cpp:139: error: `minor' is not a type
main.cpp:139: error: ISO C++ forbids declaration of `parameter' with no type
main.cpp:141: error: `a' was not declared in this scope
main.cpp:141: error: expected `, ' or `;' before '{
' token
main.cpp: In function `double evaluate(double (*)[9], int)':
main.cpp:372: error: `matrix' undeclared (first use this function)
main.cpp:383: error: invalid conversion from `int' to `double (*)[9]'
main.cpp:383: error: initializing argument 1 of
`int calcminor(double (*)[9], int, int, int)'
main.cpp:383: error: invalid conversion from `double *' to `int'
main.cpp:383: error: initializing argument 2 of
`int calcminor(double (*)[9], int, int, int)'
main.cpp:385: error: `calcdet' cannot be used as a function
main.cpp: At global scope:
main.cpp:473: error: expected constructor, destructor, or type conversion
before '(
' token
main.cpp:473: error: expected `, ' or `;' before '(
' token
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Matrix Determinate Calculation

 
0
  #3
Feb 20th, 2008
And, save yourself and others who read the code a lot of time and confusion by not putting in redundant, uninformative comments:
  1. valid = false;// Set the valid input indicator false
  2. fin >> dimension;// Get the matrix dimension
  3. if (dimension <= 9) {// If the input is good
When you use good, descriptive variable and function names (as you mostly have), such comments serve no purpose, take up time, make editing harder, and create visual clutter.

Specific to the problem you stated, reading in the data, your code says:
  1. for (r = 0; r < (dimension -1); r++) {
  2. for (c = 0; c < (dimension - 1); c++) {// Enter the data for each row
  3. fin >> mat[r][c];// Enter the data for one row
  4. // Get one matrix element
You are stopping one short of the number of rows and columns. If the dimension value is 4, then you need to read into rows 0, 1, 2, 3, and into columns 0, 1, 2, 3. Stopping at r < ( dimension -1 ) only gets row/columns 0, 1, 2.
Correct version would be:
  1. for ( r = 0; r < dimension ; r++ )
  2. {
  3. for ( c = 0; c < dimension ; c++ )
  4. {
  5. fin >> mat[r][c];
  6. cout << mat[r][c];
  7. }
  8. }
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC