1
\$\begingroup\$

This program is supposed to calculate the determinants of the data entered. I believe it does. Within the guidelines of this exercise we were not to break this down to functions but all out of main. We will be breaking it down into functions in part a and b of this overall exercise.

However, I don't know enough about matrices and determinants yet to know if I am using the equation given to me correctly. I know this out puts the data given to the screen correctly and the formula given was:

determinant = (AEI) + (BFG) + (CDH) - (BDI) - (AFH) - (CEG)

which equates to the array elements:

00 * 11 * 22 + 01 * 12 * 20 + 02 * 10 * 21- 01 * 10 * 22 - 00 * 12 * 21 - 02 * 11 * 20

Given the 3x3 block where the first row is ABC, second row of DEF and third row of GHI.

I am asking that it be analyzed and critiqued.

# include <iostream>
# include <stdio.h>
# include <iomanip>

using namespace std;

int main()
{
    int x,y,z,;
    char ch;
    float  My3DMatrix[3][3];
    int mReader[9];
    for (x = 0 ; x <= 8 ; x++)//This array is for accessing the Matrix elements in
    {                     //the prescribed manner to calculate the determ.
        y = x;
        if( x == 3 || x == 8 )y = 1;
        if(x == 4 || x ==6 )y = 2;
        if(x == 5 || x == 7 )y = 0;
        mReader[x] = y;
    }
    do  //This initiates the main loop
    {
        cout<<"Calculation of  3 x 3 Determinant"<<endl<<endl;
        for(x = 0; x<=2 ;x++) // This loop querries the user for 
        {                     //input and feeds the My3DMatrix
            cout<<"What is row "<<x+1<<" of the matrix? ";
            for (y = 0 ; y <=2 ; y++)
            {
                cin>>My3DMatrix[x][y];
            }   //The matrix is now fed
        }
        cout<<"\nThe 3 x 3 Matrix entered is: "<<endl; //Now the Raw
        cout<<setprecision(3)<<fixed; //Matrix data is displayed
        for(x = 0 ; x<=2 ; x++)
        {
            cout<<My3DMatrix[x][0]<<"\t"<<My3DMatrix[x][1]<<"\t"
                <<My3DMatrix[x][2]<<endl;
        }
        float Determinant[6]; //This array is for storing the 6
        y = -1 ; z = 0; //products from each row of the matrix
        for(x = 0 ; x <= 8 ; x++)// There are 9 indexes to create 3 products from
        {   //This loop pulls the correct indexes from My3DMatrix then  creates
            if (z>=3) z = 0; //and stores the products into Determinant[x] 0 - 2
            if ((x % 3) == 0 || x == 0)
            {
                y = y + 1;
                Determinant[y] = 1;
            }
            Determinant[y] =  Determinant[y] * My3DMatrix[z][mReader[x]];
            z = z + 1;
        }
        y = 2 ; z = 0;
        for(x = 9 ; x >= 1 ; x--)// There are 9 indexes to create 3 products from
        {       //This loop pulls the correct indexes from My3DMatrix then creates
            if (z>=3) z = 0;//and stores the products into Determinant[x] 3 - 5
            if ((x % 3) == 0 || x == 0)
            {
                y = y + 1;
                Determinant[y] = 1;
            }
            Determinant[y] =  Determinant[y] * My3DMatrix[z][mReader[x - 1]];
            z = z + 1;
        }
        float myAnswer;
        myAnswer = 0;//Here with Determinant[6] loaded with the correct index products:
        myAnswer = Determinant[0] + Determinant[1] + Determinant[2] ; //The deternm.
        myAnswer = myAnswer -  Determinant[3] - Determinant[4] - Determinant[5]; //and simple calculation is plain 
            cout<<"The determinant of the above matrix is: "<< myAnswer<< endl;
        cout<<"Would you like to Do another Matrix (Y/N)? ";

        cin>>ch ;
    }while(ch == 'Y' || ch == 'y');

    cin.get();

    return 0;
}
\$\endgroup\$
3
\$\begingroup\$

General comments:

Use one line per variable.

int x,y,z,;

Prefer to use smaller than than smaller than or equal. This is a bit nit-picky but when working with array bounds you can then use the same number in the test as in the size of the array, which makes it slightly easier to read.

int mReader[9];
for (x = 0 ; x <= 8 ; x++)

// try
int mReader[9];
for (x = 0 ; x < 9 ; x++) // easy to see correlation between x and mReader

The variables x/y/z are not used to pass information between different loops. So rather than declare them at the top declare them as close to the point of use as possible. Your loops should look more like this:

for(int x = 0; x<=2 ;x++)
 // ^^^ x is local to the loop

Do not fall in to the mental trap of thinking that you are saving space for re-use. The compiler is quite capable of doing that all by itself.

This test seems a bit redundant:

if ((x % 3) == 0 || x == 0)
     //          ^^^^^^^^^  Why? if x == 0 then (x % 3) == 0 thus the second part of the
     //                          test will never be evaluated.

Are you sure this is correct?

for(x = 9 ; x >= 1 ; x--)

Seems like x never reaches 0. As it turns out it is correct even if it looks wrong. But you need to read the rest of the code to make sure it is being used correctly. Writing code were you need to scan ahead to determine correctness is not a good idea. I would loop the same way as all the other loops (using a different variable name) then adjust the usage.

for(int reverseX = 0 ; reverseX < 9 ; ++reverseX)
{
 ..... 
     Determinant[y] =  Determinant[y] * My3DMatrix[z][mReader[9 - 1 - reverseX]];

Does this work?

cin.get();

Looks like you are trying to pause the application before termination. But to me it looks like it will try and swallow the '\n' from the last time you hit enter, and thus do nothing.

\$\endgroup\$
3
  • \$\begingroup\$ x < 9 ; x++) as opposed to x<=8 and the redundancy of-> , || x == 0) . It may sound odd but we're supposed to use Dev C++ and I've been having problems with the IDE/Compiler making the conditional statements in my loops even work. I tried using its debugger to check values but it started crashing. I had to do reinstalls of dlls to my sytem and reinstall dev C++ after using the debugger. That thing is way bad. I have an amd 64 quad core running win764 would a 32 macine be better? \$\endgroup\$
    – rpshwin
    Nov 16 '11 at 8:23
  • \$\begingroup\$ One thing I was looking for was whether or not to use the amount of loops and array as I did. I was told to re write it and only use one array, typing out like (My2DMatrix[0][1] * My2DMatrix[1][2] * My2DMatrix[2][0]) + (My2DMatrix[0][2] * ... etc I'm new to c++ and don't know any better yet. Is that how its supposed to be done if in the work world? Or is it okay bypassing all the typing and doing things more...programatically? \$\endgroup\$
    – rpshwin
    Nov 16 '11 at 8:31
  • \$\begingroup\$ @RSherwin: Using loops or typing it out? 6 of one half a dozen of the other. I mean the array is so small that it may be worth typing it out, but personally I would use a loop but either way is fine for an array this size. \$\endgroup\$ Nov 16 '11 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.