Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define read freopen("input.txt","r",stdin)
- #define write freopen("output.txt","w",stdout)
- #define dArray A[100][100]
- using namespace std;
- // print 3x4 matrix A
- void printMatrix(double dArray, int r, int c) {
- for (int i = 0; i < r; i++) {
- for (int j = 0; j < c; j++) {
- cout << A[i][j] << "\t";
- }
- cout << endl;
- }
- cout << "====================" << endl << endl;
- }
- void divideRow(double dArray, int c, int R, double P) {
- for (int j = 0; j < c; j++) {
- A[R][j] /= P;
- }
- }
- void addRow(double dArray, int c, int R1, int R2, double V) {
- for (int j = 0; j < c; j++) {
- A[R2][j] += V * A[R1][j];
- }
- }
- void rowReduce(double dArray, int r, int c) {
- int R = 0;
- for (int j = 0; j < c; j++) {
- bool found = false;
- for (int i = R; i < r; i++) {
- if (A[i][j] != 0) {
- found = true;
- if (i != R) {
- for (int k = 0; k < c; k++) {
- swap(A[R][k],A[i][k]);
- }
- }
- double P = A[R][j];
- divideRow(A, c, R, P);
- for (int k = R + 1; k < r; k++) {
- double V = A[k][j];
- addRow(A, c, R, k, -V);
- }
- R++;
- break;
- }
- }
- if (!found) {
- // all elements in column j are zero
- continue;
- }
- if (R == 3) {
- break;
- }
- }
- }
- // main function
- int main() {
- read;
- //write;
- int r, c;
- //int a[100][100];
- //vector<vector<double> > A;
- double A[100][100];
- cin>>r>>c;
- for (int i = 0; i < r; i++){
- for (int j = 0; j < c; j++){
- cin>>A[i][j];
- }
- }
- /*
- double A[r][c];
- A[0][0] = 2; A[0][1] = 1; A[0][2] = 0; A[0][3] = 5;
- A[1][0] = 3; A[1][1] = 6; A[1][2] = 1; A[1][3] = 1;
- A[2][0] = 5; A[2][1] = 7; A[2][2] = 1; A[2][3] = 8;
- p34(A);
- */
- printMatrix(A, r, c);
- rowReduce(A, r, c);
- printMatrix(A, r, c);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement