Basic Algorithms for C language 35-product of the upper triangle of an array the product of the primary diagonal and the product of the secondary diagonal, 35 diagonal
// Array algorithm
/*
========================================================== ==================================
Question: Calculate the product of the upper triangle of the fourth-order matrix, the product of the primary diagonal, and the product of the secondary diagonal:
For example:
8 3 6 5
0 4 3 2
0 6 1 5
7 0 0 2
The product of the upper triangle is: 172800
The product of the primary diagonal is: 64
Product of negative diagonal: 630
========================================================== ==================================
*/
# Include <stdio. h>
Void main ()
{
Int I, j;
Int ssj = 1, zdj = 1, fdj = 1;
Int a [4] [4];
Printf ("input 4*4 matrix: \ n ");
For (I = 0; I <4; I ++)
For (j = 0; j <4; j ++)
Scanf ("% d", & a [I] [j]);
Printf ("output matrix: \ n ");
For (I = 0; I <4; I ++)
{
For (j = 0; j <4; j ++)
Printf ("% 3d", a [I] [j]);
Printf ("\ n ");
}
Printf ("product of the output upper triangle :");
For (I = 0; I <4; I ++)
For (j = I; j <4; j ++)
Ssj * = a [I] [j];
Printf ("% d \ n", ssj );
Printf ("product of the main diagonal :");
For (I = 0; I <4; I ++)
Zdj * = a [I] [I];
Printf ("% d \ n", zdj );
Printf ("product of the sub-diagonal :");
For (I = 3; I> = 0; I --)
Fdj * = a [I] [3-i];
Printf ("% d \ n", fdj );
}
/*
========================================================== ==================================
Rating:
The upper triangle is the triangle above the main diagonal line. The condition should be: the row ranges from 0 to n-1, and the column ranges from the corresponding number of rows to n-1; that is
(I = 0; I <= n-1; I ++) (j = I; j <= n-1; j ++ ); the primary diagonal is a line from a [0] [0] to a [n-1] [n-1]. The condition is:
Rows from 0 to n-1, columns = rows, (I = 0; I <n-1; I ++) correspond to the product a [I] [I]; likewise, the negative diagonal is from a [n-1] [0]
A [0] [n-1] is connected by the following conditions: rows from n-1 to 0, columns = n-1-rows, (I = n-1; I> = 0; I --), the product item is
A [I] [n-1-i]; it is easy to solve this problem after analyzing it clearly! In fact, this algorithm can be extended to any square matrix, read
If you are interested, you can do it yourself!
========================================================== ==================================
*/
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.