# C++ determinant of diagonal matrix

• Comp Sci
• jinro
In summary, the conversation discusses creating a code for computing the determinant of an n by n diagonal matrix using Gauss elimination. The solution involves creating a variable "diagonal" and multiplying it by all diagonal elements in a loop. The conversation also mentions difficulties with creating a back elimination formula and expresses gratitude for the help provided.
jinro

## 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:
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.

## What is a diagonal matrix in C++?

A diagonal matrix in C++ is a square matrix where all the elements outside of the main diagonal (i.e. the diagonal from the top left to bottom right) are equal to zero. The main diagonal contains the non-zero elements of the matrix.

## How do you find the determinant of a diagonal matrix in C++?

To find the determinant of a diagonal matrix in C++, you simply multiply all the elements on the main diagonal. The determinant of a diagonal matrix with dimensions nxn is equal to the product of all the elements on the diagonal: det(A) = a11 * a22 * ... * ann.

## Is the determinant of a diagonal matrix always zero?

No, the determinant of a diagonal matrix is only zero when all the elements on the diagonal are equal to zero. If any of the elements on the diagonal are non-zero, then the determinant will also be non-zero.

## Can a diagonal matrix have negative elements?

Yes, a diagonal matrix can have negative elements as long as they are on the main diagonal. The other elements outside of the main diagonal must still be equal to zero.

## What is the importance of diagonal matrices in C++?

Diagonal matrices are important in C++ because they have special properties that make them easier to work with and manipulate. They are also used in many mathematical operations, such as solving systems of equations and calculating eigenvalues and eigenvectors.

Replies
7
Views
2K
Replies
18
Views
2K
Replies
18
Views
2K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
2
Views
7K
Replies
4
Views
525
Replies
2
Views
6K
Replies
13
Views
3K
Replies
10
Views
1K