Comp Sci C++ determinant of diagonal matrix

Click For Summary
To compute the determinant of an n by n diagonal matrix in C++, the formula is simply the product of its diagonal elements. The user struggled with implementing this in code, particularly with using a for loop to accumulate the product of the diagonal elements. A suggestion was made to declare a variable to store the product, initializing it to 1.0, and then multiplying it by each diagonal element within the loop. This approach simplifies the calculation and corrects previous errors in the elimination formula. The discussion highlights the importance of understanding matrix properties for efficient coding.
jinro
Messages
4
Reaction score
0

Homework Statement


well, my assignment was to make a gauss elimination, so now i need to compute the determinant of an n by n diagonal matrix

variable rows = number of equations
variable i = random integer
matrix A[100][100]
dummy matrix A2[100][100]

Homework Equations



det[A] = A[0][0]*A[1][1]*A[2][2]*...*A[n][n]

The Attempt at a Solution


i made the code for pivoting and forward elimination, but i spent 2 hours or so and i couldn't figure out how to make a code for this using for loop

for (i=0;i<rows;i++){
A2 = A*A[i+1][i+1]
}
(this will only ever give me a product of 2 numbers in the diagonal though)

i thought it was going to be easy but i need to find a formula so it stores the products of all the elements in the diagonal of the matrix

PS; there is only so much attempts i can show, since i have no clue whatsoever to go about this.




@borek

thanks a lot i got the answer u helped a lot, i also realized my back elimination formula was wrong and your method helped me a lot once again
 
Last edited:
Physics news on Phys.org
jinro said:
det[A] = A[0][0]*A[1][1]*A[2][2]*...*A[n][n]

Declare variable "diagonal", init it with 1.0, multiply it in loop by all diagonal elements.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 4 ·
Replies
4
Views
724
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 13 ·
Replies
13
Views
3K