473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Can someone check my code please?

Hello everyone, i am new to these forums and in fact new to C++. I am working on a project, and i would be most grateful if i could have my code checked by people who have a better understanding of C++ than i curently do.

What i intend this code to do is ask for a number off you, between 2 and 9, it then displays a matrix according to the number given and displays its determinant. I do not know how to program finding the determinant though, so i have got round this by simply finding the determinant elsewhere. I intend to go up a 9x9 matrix, although i am stuck on finding the determinant for 6x6 matrices upwards.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1.     if (n==2)
  2.     {
  3.         cout<<"A 2x2 matrix is:";
  4.  
  5.         {    int firstarray[] = {1, -2};
  6.              int secondarray[] = {-2, 1};
  7.              printarray (firstarray,2);
  8.              printarray (secondarray,2);
  9.              cout<<"The determinant of a 2x2 matrix is -3";
  10.         }
  11.     }
  12.     else if (n==3)
  13.     {
  14.         cout<<"A 3x3 matrix is:";
  15.  
  16.         { int firstarray[] = {1, -2 , 3};
  17.         int secondarray[] = {-2, 3, 1};
  18.         int thirdarray[] = {3, 1, -2};
  19.         printarray (firstarray,3);
  20.         printarray (secondarray,3);
  21.         printarray (thirdarray,3);
  22.         cout<<"The determinant of a 3x3 matrix is -38";
  23.         }
  24.     }
  25.  
  26.     else if (n==4)
  27.     { 
  28.         cout<<"A 4x4 matrix is:";
  29.  
  30.     { int firstarray[]= {1, -2, 3, -4};
  31.     int secondarray[]= {-2, 3, -4, 1};
  32.     int thirdarray[]= {3, -4, 1, -2};
  33.     int fourtharray[]= {-4, 1, -2, 3};
  34.     printarray (firstarray, 4);
  35.     printarray (secondarray, 4);
  36.     printarray (thirdarray, 4);
  37.     printarray (fourtharray, 4);
  38.     cout<<"The detereminant of a 4x4 matix is 160";
  39.     }
  40.  
  41.     }
  42.  
I hope you can see what i am trying to do. Checking my code and providing feedback will be most appreciated.Alternatively, if you know of an improved code and/or a code to find the determinant of a matrix then, in regulation with forum rules, if you could PM me.

Thanks agen,

Mark.
Aug 12 '08 #1
12 3620
JosAH
11,448 Expert 8TB
For any non-singular square matrix A it can be decomposed as L*U == P * A
where L is a lower triangular matrix with all ones on its diagonal, U is an upper
triangular matrix and P is a row permutation matrix.

Since det(L*U) == det(P * A) and det(P) is +- 1; it is negative if an odd number
of row swaps have been performed, positive otherwise. The determinant of a
triangular matrix is the product of its elements on the diagonal. det(L) == 1
(there are only ones on its diagonal, so det(A) == det(U)*s where s == +-1.

The decomposition is called a 'lower-upper decomposition with partial pivoting'.
If the decomposition fails (all zeros on the partial diagonal), the determinant of
matrix A is zero.

kind regards,

Jos
Aug 12 '08 #2
arnaudk
424 256MB
For any non-singular square matrix A it can be decomposed as L*U == P * A
where L is a lower triangular matrix with all ones on its diagonal, U is an upper
triangular matrix and P is a row permutation matrix.
This still leaves you with the problem of finding the LU decomposition in the first place. I don't know how easy that would be. I would just use a cofactor expansion (a.k.a. expansion by minors) to reduce the determinant of the large matrix into a sum of determinants successively smaller matrices (minors) multiplied by their cofactor. Eventually, you end up with a sum of 2x2 matrix determinants.
Aug 12 '08 #3
JosAH
11,448 Expert 8TB
This still leaves you with the problem of finding the LU decomposition in the first place. I don't know how easy that would be.
It's extremely easy: just Gaussian elimination of the lower triangular matrix is
enough. Swapping two rows only changes the sign of the determinant.

kind regards,

Jos
Aug 12 '08 #4
arnaudk
424 256MB
Ah yes, I see. Thanks.
Aug 12 '08 #5
I am still relatively confused, i have read the wikipedia page on "lower-upper decomposition" provided by JosAH, although i still cannot quite grasp it.

I understand how to solve matrices by reducing a matrix down to a 2x2. But the processes you have described, are more of a challenge to me.

If you could provide an example for a "how-to", on, say, the following matrix it would help clarify things for me. I understand things when it's a step-by-step process.

1 -2 3 -4 5 -6
-2 3 -4 5 -6 1
3 -4 5 -6 1 -2
-4 5 -6 1 -2 3
5 -6 1 -2 3 -4
-6 1 -2 3 -4 5

Thanks agen,

Mark.
Aug 12 '08 #6
arnaudk
424 256MB
First of all, use a matrix class, that will greatly simplify the various matrix operations you will need to do. The one in GSL implements matrix operations for you. Then, the LU method Jos suggested is actually much faster than a naive cofactor expansion and can be implemented using the Doolittle algorithm. If you want to check your answers, I get:
Expand|Select|Wrap|Line Numbers
  1. det( 1 -2)= 1
  2.     -2  1
  3.  
  4.      1 -2  3
  5. det(-2  3 -1)=-3
  6.      3  1  2
  7.  
  8.      1 -2  3 -4
  9. det(-2  3 -4  1)=-18
  10.      3 -4  1 -2
  11.     -4  1 -2  3
  12.  
  13.      1 -2  3 -4  5
  14.     -2  3 -4  5 -1
  15. det( 3 -4  5 -1  2)=160
  16.     -4  5 -1  2 -3
  17.      5 -1  2 -3  4
  18.  
  19.      1 -2  3 -4  5 -6
  20.     -2  3 -4  5 -6  1
  21. det( 3 -4  5 -6  1 -2)=1875
  22.     -4  5 -6  1 -2  3
  23.      5 -6  1 -2  3 -4
  24.     -6  1 -2  3 -4  5
  25.  
the next few determinants are: -27216, -470596, 9437184, 215233605. It's much easier to understand the general case as explained in wikipedia rather than a specific case. GSL also has functions which compute the LU decomposition (and hence the determinant) for you.

By the way, instead of 'listing' the different matrices as you do, you can compute the matrix of any size m using the formula a_ij = pow(-1,i+j) * ( (i+j-2) % m + 1) where a_ij is the element on the i'th column and j'th row of the matrix.
Aug 12 '08 #7
JosAH
11,448 Expert 8TB
If you could provide an example for a "how-to", on, say, the following matrix it would help clarify things for me. I understand things when it's a step-by-step process.

1 -2 3 -4 5 -6
-2 3 -4 5 -6 1
3 -4 5 -6 1 -2
-4 5 -6 1 -2 3
5 -6 1 -2 3 -4
-6 1 -2 3 -4 5
Move over the diagonal: add the first row *2 to the second row; subtract the
first row *3 from the third row; add the first row *4 to the fourth row etc.

Now the first column below the diagonal contains all zeros. Do the same for the
next elements on the diagonal and don't touch the previous rows anymore.

At the end you end up with an upper triangular matrix U. If an element on the
diagonal happens to be zero change that row with a row below it so that a non-
zero element ends up on the diagonal and flip a sign variable: sign= -sign.

The determinant of the matrix is the product of the elements on the diagonal
times that sign variable. It is linear algebra 101 actually and it's an O(n**3)
process.

kind regards,

Jos
Aug 12 '08 #8
Ah ok, thank you. I will have a go at using your suggested method.

arnaudk, i am very grateful of you for finding those determinants although i believe the answers you have given for the determinants are incorrect.

For my 2x2 matrix, i found the determinant to be -3. For my 3x3, -38. 4x4 comes to 160 and 5x5 equals 4983. Thus i believe your answers for 6x6 to the 9x9 matrices are also incorrect, i may be wrong however.

Is it possible to have someone else check these values, and provide their results too? It would be most helpful as then i can check where i have gone wrong, if indeed arnaudak is right.

On another note, back to my code, could someone review it for me? I am using Microsoft Visual C++ Express Edition 2008.

Thanks again for all your help and support,

Mark.
Aug 13 '08 #9
JosAH
11,448 Expert 8TB
On another note, back to my code, could someone review it for me? I am using Microsoft Visual C++ Express Edition 2008.
I get the following values (using that LUP decomposition):

Expand|Select|Wrap|Line Numbers
  1. 2:    -3.0
  2. 3:    -28.0
  3. 4:    160.0
  4. 5:    1875.0
  5. 6:    -27216.0
  6.  
kind regards,

Jos
Aug 13 '08 #10
arnaudk
424 256MB
arnaudk, i am very grateful of you for finding those determinants although i believe the answers you have given for the determinants are incorrect.
It seems there are some typos in the matrices (and the 3x3 result should be 2x2 result, etc) but I checked the determinants in Mathematica and they are correct:
Expand|Select|Wrap|Line Numbers
  1. 1
  2. -3
  3. -18
  4. 160
  5. 1875
  6. -27216
  7. -470596
  8. 9437184
  9. 215233605
  10.  
So Jos' 3: -28 is probably a typo; you can check this by hand:
Expand|Select|Wrap|Line Numbers
  1.  
  2. .    1 -2  3
  3. det(-2  3 -1)= 1*3*2 + -2*-1*3 + 3*-2*-1 - 1*-1*-1 - -2*-2*2 - 3*3*3
  4.      3 -1  2 = 6     +  6      + 6       - 1       -  8      -  27 = -18             
  5.  
But I think your 3x3 matrix seems inconsistent with the rest of the matrices. Check your minus signs. If I use your 3x3 matrix I get your answer
Expand|Select|Wrap|Line Numbers
  1. .    1 -2  3
  2. det(-2  3  1)= -38
  3.      3  1 -2 
  4.  
Aug 13 '08 #11
arnaudk
424 256MB
Ah, it seems you are using the rule "all even numbers are negative", to determine the sign of the matrix elements. In that case, I get the following answer:
Expand|Select|Wrap|Line Numbers
  1. 1
  2. -3
  3. -38
  4. 160
  5. 4983
  6. -27216
  7. -1401628
  8. 9437184
  9. 686342045
  10.  
As for reviewing the code, it would be a much better idea to determine your matrices algorithmically, rather than just listing them in the code. And it would be a really good idea to use the right tool for the job: a matrix class.
Aug 13 '08 #12
JosAH
11,448 Expert 8TB
It seems there are some typos in the matrices (and the 3x3 result should be 2x2 result, etc) but I checked the determinants in Mathematica and they are correct:
Expand|Select|Wrap|Line Numbers
  1. 1
  2. -3
  3. -18
  4. 160
  5. 1875
  6. -27216
  7. -470596
  8. 9437184
  9. 215233605
  10.  
So Jos' 3: -28 is probably a typo; you can check this by hand:
Expand|Select|Wrap|Line Numbers
  1.  
  2. .    1 -2  3
  3. det(-2  3 -1)= 1*3*2 + -2*-1*3 + 3*-2*-1 - 1*-1*-1 - -2*-2*2 - 3*3*3
  4.      3 -1  2 = 6     +  6      + 6       - 1       -  8      -  27 = -18             
  5.  
But I think your 3x3 matrix seems inconsistent with the rest of the matrices. Check your minus signs. If I use your 3x3 matrix I get your answer
Expand|Select|Wrap|Line Numbers
  1. .    1 -2  3
  2. det(-2  3  1)= -38
  3.      3  1 -2 
  4.  
I took this 3x3 matrix from reply #7:

Expand|Select|Wrap|Line Numbers
  1.  1 -2  3
  2. -2  3 -1
  3. 3  1  2
  4.  
And that 2x2 matrix definitely results in 1x1 - -2*-2 == -3 ;-)
I guess there were some typos in the matrixes themselves.

kind regards,

Jos
Aug 13 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: What-a-Tool | last post by:
I'm going out out of my mind trying to get this to work with no luck. The error message I get is at the bottom. Can someone please tell me what I'm doing wrong here. I've tried this a million...
8
by: Sue | last post by:
Hello! I am back with another question. Remember I am a new JavaScript student and I am aware that this code does not check for all the possibilities and that as a "NEW" JavaScript student I am...
1
by: sparks | last post by:
I have done a LOT of databases in access 97 but I am new to access 2000. We are S L O W L Y converting over. Yesterday I had a 97 database that I had to run the converter to 2000 and everything...
4
by: BinWang85 | last post by:
The following is the code: #include <iostream.h> #include <math.h> //needed for decimal calculations int main() { int employee_num; double FetchHours(void);
3
by: Marek | last post by:
Hello gurus! I wrote a code in VBS, that will check, that current user is in one from three groups. But i don't know how asimilate it with asp.net. This page will be a bridge between 2 - main...
8
by: Sandy | last post by:
I have two tables, tblPost - (Cols PostID, TopicID, UserID, Question, PostMsg and PostDT) and tblTopic - (Cols TopicID, Topic). I am trying to get tblPost.Question and tblPost.PostDT into...
1
by: scprosportsman | last post by:
Please help guys, i am trying to set up a database here at work and im fairly new to access in terms of writing functions and queries and stuff. I have 2 different places on my design that will...
4
by: garyusenet | last post by:
The following code was supplied by a kind poster as a solution to a problem i was having. But it's not quite working. I have commented the code myself below. Can you please read my comments to make...
1
by: td0g03 | last post by:
Hello, I am new to C and I am new to English. I not sure what palindromes mean. I don't know exactly what my teacher wants me to do. If someone could explain it to me in a different way that would be...
3
by: shapper | last post by:
Hello, I have two tables: Polls and Options: Poll PollID, Question Options OptionID, PollID, Answer I want to select a Poll given its ID and all Options associated to it. Options should...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.