Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,450 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,145 people online right now. Registration is fast and FREE... Join Now!




Matrix Determinate

 
Reply to this topicStart new topic

Matrix Determinate, Finding the Determinate of a Square Matrix

defaultAZN
14 Feb, 2008 - 06:37 PM
Post #1

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 4

Hi,
I'm taking Computer Science for the first time and I'm trying to code a program that accepts input from a file for a square matrix and finds the determinate. I ran into some errors while working on the "Get Input Function" part of the program. Specifically the ones in my build log here:

1>------ Build started: Project: Matrix Evaluator, Configuration: Debug Win32 ------
1>Compiling...
1>matrix evaluator.cpp
1>.\matrix evaluator.cpp(279) : error C2601: 'getInput' : local function definitions are illegal
1> .\matrix evaluator.cpp(197): this line contains a '{' which has not yet been matched
1>.\matrix evaluator.cpp(432) : fatal error C1075: end of file found before the left brace '{' at '.\matrix evaluator.cpp(289)' was matched
1>Build log was saved at "file://c:\Users\Kelan Ige\Documents\Visual Studio 2005\Projects\Matrix Evaluator\Matrix Evaluator\Debug\BuildLog.htm"
1>Matrix Evaluator - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

and here is the code:
CODE

// This program evaluates determinants of square matrices using minors.
//
// The program can handle up to nine by nine square matrices.
//
// This program reads input from a file and writes identical output to
// both the console and to a file.
//
// The program reads a matrix dimension and the required number of matrix
// elements, writes the matrix in columns, calculates the determinant,
// and writes the value of the determinant.  The process repeats until
// an end of file is encountered or an invalid dimension is specified.
//
// After each determinant value is written a blank line is written.
//
// After all processing is completed, the termination message "Done."
// is written.
//
// If the input or output file cannot be opened, the program terminates without
// any other action than displaying an error message.

// Examples
//
// DETERMINANT EVALUATOR
//
// Matrix dimension:2
//
// Input values:
// 4.0000
// -3.0000
// 2.0000
// 6.0000
//
// Matrix:
//     4.0000   -3.0000
//     2.0000    6.0000
//
// Determinant value:30.0000
//
// Matrix dimension:4
//
// Input values:
// 0.1000
// 1.3000
// -3.5000
// 2.8000
// 1.7000
// -3.5000
// 2.4000
// -4.1000
// 2.3000
// 0.0000
// -1.6000
// 3.9000
// 1.2000
// -2.7000
// -0.5000
// 1.0000
//
// Matrix:
//     0.1000    1.3000   -3.5000    2.8000
//     1.7000   -3.5000    2.4000   -4.1000
//     2.3000    0.0000   -1.6000    3.9000
//     1.2000   -2.7000   -0.5000    1.0000
//
// Determinant value:-72.8371
//
// Done.



//#define SHOW_DETERMINANTS


#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;




//  **************************************************************************
//  *                                                                        *
//  *                            GLOBAL CONSTANTS                            *
//  *                                                                        *
//  *************************************************************************

    const string INFILE_NAME  = "File4.In";
    const string OUTFILE_NAME = "File4.Out";

    const int MAX_DIM         = 9;        // Maximum matrix dimension
    const int PRECISION       = 4;        // Precision of double output
    const int OUTPUT_WIDTH    = 10;        // Width of double output




//  **************************************************************************
//  *                                                                        *
//  *                            GLOBAL VARIABLES                            *
//  *                                                                        *
//  **************************************************************************

    ifstream fin;                        // Input file
    ofstream fout;                        // Output file

    bool    ineof;                        // End of file indicator



//  **************************************************************************
//  *                                                                        *
//  *                              MAIN PROGRAM                              *
//  *                                                                        *
//  **************************************************************************

int main()
{
    // Function declarations
    bool    initialize();                // Initialize the program

    bool    getInput                    // Get the input matrix
            (double  mat[][MAX_DIM],    // Matrix
             int    &dim);                // Matrix dimension

    void    displayMatrix                // Display the matrix
            (double  mat[][MAX_DIM],    // Matrix
             int     dim);                // Matrix dimension

    double    evaluate                    // Evaluate a determinant
            (double  mat[][MAX_DIM],    // Matrix
             int     dim);                // Matrix dimension

    void    finish();                    // Terminate the program

    bool    process1;                    // Processing indicator
    bool    process2;                    // Processing indicator

    int     size;                        // Matrix dimension

    double  answer;                        // Value of determinant
    double  mat[MAX_DIM][MAX_DIM];        // Matrix



    // Initialize
    process1 = initialize();

    // If the files were opened successfully
    if (process1)
    {
        // Do
        do
        {
            // Get the input equations
            process2 = getInput(mat, size);

            // If the input is valid
            if (process2)
            {
                // Display the matrix
                displayMatrix(mat, size);

                // Evaluate the determinant
                answer = evaluate(mat, size);

                // Report the value of the determinant
                cout << "Determinant value:" << answer << endl;
                fout << "Determinant value:" << answer << endl;

                // Write a blank line
                cout << endl;
                fout << endl;
            }
        }
        // While the input is valid
        while (process2);

        // Finish
        finish();
    }

    // Return
    return 0;
}



//  **************************************************************************
//  *                                                                        *
//  *                          INITIALIZE FUNCTION                           *
//  *                                                                        *
//  **************************************************************************

bool    initialize()                    // Initialize program
{
    bool    success;                    // Initialization success indicator


    // Set the success indicator false
    success = false;

    // Initialize the end of file false
    ineof  = false;

    // Open the input file
    fin.open(INFILE_NAME.c_str(), ios::in);

    // If the file was not opened successfully
    if (!fin)
    {
        // Set the end of file indicator true
        ineof = true;

        // Display an error message
        cout << "ERROR:  Unable to open input file \""
             << INFILE_NAME << "\"" << endl;
    }

    // Else
    else
    {
        // Open the output file
        fout.open(OUTFILE_NAME.c_str(), ios::out);

        // If the output file was not opened successfully
        if (!fout)
        {
            // Set the end of file indicator true
            ineof = true;

            // Close the input file
            fin.close();

            // Display an error message
            cout << "ERROR:  Unable to open output file \""
                 << OUTFILE_NAME << "\"" << endl;
        }

    // If there is valid input
    if (!ineof)
    {
        // Set the success indicator true
        success = true;

        // Set the output format
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(PRECISION);
        fout.setf(ios::fixed);
        fout.setf(ios::showpoint);
        fout.precision(PRECISION);

        // Display a title
        cout << "DETERMINANT EVALUATOR" << endl;
        fout << "DETERMINANT EVALUATOR" << endl;

        // Display a blank line
        cout << endl;
        fout << endl;
    }

    // Return the initialization success
    return success;
}



//  **************************************************************************
//  *                                                                        *
//  *                           GET INPUT FUNCTION                           *
//  *                                                                        *
//  **************************************************************************

bool    getInput                        // Get the input matrix
        (double  mat[][MAX_DIM],        // Matrix
         int    &dim)                    // Matrix dimension
{
    bool    valid;                        // Valid input indicator

    int        r;                            // Row    number
    int        c;                            // Column number
    int        dimension;                    // Matrix Dimension

    valid = false;// Set the valid input indicator false
    cout << "Matrix dimension:"// Get the matrix dimension
    cin >> dimension;
    if (dimension <= 9) {// If the input is good
        cout << "Matrix dimension:" << dimension << "." << endl;// Display a header for the matrix dimension
        // Echo the input
        // Display a blank line
        if (dimension < 2) {// If the dimension is less than 2
            cerr << "Error: Matrix dimension too small." << endl;// Display an error message
        }// Display a blank line
        else if (dimension > 9) {// Else if the dimension is too large
            cerr << "Error: Matrix dimension too large." << endl;// Display an error message
        }// Display a blank line
        else {// Else
            cout << "Input values:";// Display a header for the input values
            // Enter the data for each row
                // Enter the data for one row
                    // Get one matrix element
                    // If the input is good
                        // Echo the value
            // If the input is all good
                valid = true;// Set the valid input indicator true
            // Display a blank line
    // Return the result
    return valid;
}



//  **************************************************************************
//  *                                                                        *
//  *                        DISPLAY MATRIX FUNCTION                         *
//  *                                                                        *
//  **************************************************************************

void    displayMatrix                    // Display the matrix
        (double  mat[][MAX_DIM],        // Matrix
         int     dim)                    // Matrix dimension
{
    int        r;                            // Row    number
    int        c;                            // Column number


    // Display a matrix header
    // Display all rows
        // Display one row
            // Display one matrix element
        // End the line
    // Display a blank line
}



//  **************************************************************************
//  *                                                                        *
//  *                      EVALUATE DETERMINANT FUNCTION                     *
//  *                                                                        *
//  **************************************************************************

double    evaluate                        // Evaluate a determinant
        (double  mat[][MAX_DIM],        // Matrix
         int     dim)                    // Matrix dimension
{
    return 0.0;
}



//  **************************************************************************
//  *                                                                        *
//  *                            FINISH FUNCTION                             *
//  *                                                                        *
//  **************************************************************************

void    finish()                        // Terminate the program
{
    // Display a termination message
    cout << "Done." << endl;
    fout << "Done." << endl;

    // Close the input file
    fin.close();

    // Close the output file
    fout.close();
}



//  **************************** SUPPORT FUNCTION ****************************


//  **************************************************************************
//  *                                                                        *
//  *                      DISPLAY DETERMINANT FUNCTION                      *
//  *                                                                        *
//  **************************************************************************

void    displayDeterminant                // Display a determinant
        (double  mat[][MAX_DIM],        // Matrix
         int     dim,                    // Matrix dimension
         double  det)                    // Value of determinant
{
    int        r;                            // Row    number
    int        c;                            // Column number

    // Display a matrix header
    cout << "Determinant:" << endl;
    fout << "Determinant:" << endl;

    // Display all rows
    for (r = 0; r < dim; r++)
    {
        // Display an initial bar
        cout << "| ";
        fout << "| ";

        // Display one row
        for (c = 0; c < dim; c++)
        {
            // Display one matrix element
            cout << setw(OUTPUT_WIDTH) << mat[r][c];
            fout << setw(OUTPUT_WIDTH) << mat[r][c];
        }

        // Display a terminal bar
        cout << " |";
        fout << " |";

        // End the line on the console
        cout << endl;
        fout << endl;
    }

    // Display a blank line
    cout << endl;
    fout << endl;

    // Display the determinant value
    cout << "Subdeterminant value:" << det << endl;
    fout << "Subdeterminant value:" << det << endl;

    // Display a blank line
    cout << endl;
    fout << endl;
}


If possible could you also point me in the right direction as far as reading input for the values of a matrix from a text file. I'm having a hard time with file manipulation and reading elements into an array so that I can evaluate and find the determinate of the matrix. I know I haven't come close to a working program yet, but any thing you can tell me that would point me in the right direction for any of the code would be greatly appreciated.
Thanks in advanced.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Matrix Determinate
14 Feb, 2008 - 11:21 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,850



Thanked: 159 times
Dream Kudos: 1375
My Contributions
You should be declaring function prototypes at the top of your code rather then inside main(). Create variables that act as parameters to pass to your functions to determine your output, etc...
Instead of:
CODE

int main()
{
    // Function declarations
    bool    initialize();                // Initialize the program

    bool    getInput                    // Get the input matrix
            (double  mat[][MAX_DIM],    // Matrix
             int    &dim);                // Matrix dimension

    void    displayMatrix                // Display the matrix
            (double  mat[][MAX_DIM],    // Matrix
             int     dim);                // Matrix dimension

    double    evaluate                    // Evaluate a determinant
            (double  mat[][MAX_DIM],    // Matrix
             int     dim);                // Matrix dimension
//more code
}//end of main

Declare functions here:
CODE

#include <stuff>
using namespace std;

//FUNCTION DECLARATIONS/PROTOTYPES
bool initialize();
bool getInput(double deminsion, int &dm); //why is getting input returning a true or false anyways?
void displayMatrix(parameters);
double evaluate(parameters);

int main(){

//code
}//end of main

Function() {
//function code
}//end of functions


Why is your input function returning a boolean? Shoiuldn't it be returning the input for the program to use (as well as a valid result)?
--kya

This post has been edited by KYA: 14 Feb, 2008 - 11:26 PM
User is online!Profile CardPM
+Quote Post

defaultAZN
RE: Matrix Determinate
17 Feb, 2008 - 11:03 AM
Post #3

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 4

First off thanks for the suggestion about declaring the functions.
QUOTE
Why is your input function returning a boolean? Shoiuldn't it be returning the input for the program to use (as well as a valid result)?

My Professor gave me a "skeleton" code that had all the function declarations and the basic format that the code was going to follow. I'm not sure why it's returning a boolean rather than an input. I'm trying to understand what my Professor is doing. However, I need to fill it out, but if you have any suggestions as to improve the code that would be greatly appreciated. I'm basically trying to fill out all the functions to make the program read a matrix from a input file and and compute the determinate and output to both the console and a file.
Thanks again.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 01:02PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month