"Top triangle" determine the source code (improved version, added special processing in several special cases)

Source: Internet
Author: User

# Include <iostream. h>
# Include <fstream. h>
# Include <cmath>
Void main ()
{
// Start with the input deciding factor
Int n, I, j, a [20] [20], T [20], max [20], B [20], k, q, p, f = 2, u = 0;
Float t [20] [20], c, sum = 1, w [20];
Cout <"order :";
Cin> n;
Ifstream infile; // defines the input file class.
Ofstream outfile; // defines the output file class.
Infile. open ("D: \ exam question .txt"); // open an input file: exam question .txt"
For (I = 1; I <= n; I ++)
{
For (j = 1; j <= n; j ++)
{
Infile> a [I] [j]; // input the ten integer numbers in the "exam question .txt" to a [I ].
}
}
Infile. close (); // close the input file
// Enter the rank of the determinant
For (j = 1; j <= n; j ++) // start from the first column.
{
T [j] = 0; // initializes the counter to 0
For (I = 1; I <= n; I ++)
{
If (a [I] [j] = 0) // count every 0 found in column j
{
T [j] ++; // counts the number of zeros in column j.
}
}
}
Outfile. open ("D: \ .txt"); // open an output file and click ".txt"
// After the loop ends, the number of zeros in each column is stored in the array T [j ].
// Start to compare the number of zeros in each column and arrange them
For (I = 1; I <= n; I ++) // sort T [j] in ascending order after n cycles
{
Max [I] =-100;
For (j = 1; j <= n; j ++)
{
Max [I] = (max [I]> T [j])? Max [I]: T [j];
}
// Find the maximum I value
For (j = 1; j <= n; j ++) // find the number of columns corresponding to the maximum I value through a loop j is the corresponding number of Columns
{
If (max [I] = T [j])
{
T [j] =-200; // set T [j] to a smaller number.
// Outfile <"th" <j <"column:" <max [I] <"0" <endl;

B [I] = j;
Break;
}
}
}
Outfile <"------------------------------------ determine the answer -------------------------------------" <endl;
// Print New Determinant
Outfile <"Step 1: Change columns:" <endl;
For (I = 1; I <= n; I ++)
{
For (j = 1; j <= n; j ++)
{
T [I] [j] = float (a [I] [B [j]);
// Determine the number of change Columns
If (I = 1 & j! = B [j])
{
U ++;
}
Outfile <t [I] [j] <"";
}
Outfile <endl;
}
// Solution to the special case where the first number is 0
If (t [1] [1] = 0)
{
For (I = 2; I <= n; I ++)
{
If (t [I] [1]! = 0)
{
For (j = 1; j <= n; j ++)
{
W [j] = t [I] [j];
T [I] [j] = t [1] [j];
T [1] [j] = w [j];
}
Break;
}
}
U ++;
}
// After changing the column, store the new determinant in a new array t [] []
For (j = 1; j <n; j ++) // Column
{
For (k = j + 1; k <= n; k ++) // The row whose column j needs to be converted to 0
{
If (t [k] [j]! = 0)
{
For (I = j; I <= n; I ++) // line
{
If (t [I] [j]! = 0 & I! = K)
{
P = 0;
C = float (t [k] [j]/t [I] [j]);
For (q = 1; q <= n; q ++) // Column
{
T [k] [q] = t [k] [q]-c * t [I] [q];
If (abs (t [k] [q]) <0.0001 & k! = Q)
{
T [k] [q] = 0;
}
P ++;
}
// Print the intermediate process
Outfile <"Step" <f ++ <: r ["<k <"]-"<c <" r ["<I <"]: "<endl;
For (int s = 1; s <= n; s ++)
{
For (int d = 1; d <= n; d ++)
{
Outfile <t [s] [d] <"";
}
Outfile <endl;
}
If (p = n)
{
Break;
}
}
}
}
}
}
For (I = 1; I <= n; I ++)
{
Sum = t [I] [I] * sum;
}
Outfile <"no." <f ++ <"step:" <endl;
If (u! = 0)
{
U = U-1;
}
Outfile <"final result =" <sum * pow (-1, u) <endl;
Outfile. close (); // close the output file

 

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.