Placing largest number in pivot position

I'm trying to make a code that will place the largest number in a column in a matrix in the pivot column. So in (1,1) it will compare all numbers below that point step by step and place the biggest number in the pivot point. Likewise with (2,2) it will compare step by step the numbers below the point in the column. After that there will be a gauus reduction. It's just the pivot problem i'm having trouble with. I think I'm almost there I just can't get it to wotk properly. The code is in the first part of the gauss-function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void GetMatrix(double A[100][100], double b[100], int &n);
void FormMatrix(double TM[100][101],double A[100][100], double b[100], int &n);
void ShowMatrix(double TM[100][101],int &n);
void Gauss(double TM[100][101],int &n);


int main(){

	int n,j;
	double A[100][100],b[100],TM[100][101];

GetMatrix(A,b,n);
FormMatrix(TM,A,b,n);
Gauss(TM,n);
ShowMatrix(TM,n);

return 0;
}

void GetMatrix(double A[100][100], double b[100], int &n){
ifstream Fil;
Fil.open("Uge49opg5b.txt");
Fil>>n;
for(int i=0;i<n;i++){
	for(int j=0;j<n;j++) Fil>>A[i][j];
	Fil>>b[i];
}
Fil.close();
}

void FormMatrix(double TM[100][101],double A[100][100], double b[100], int &n){

	for(int i=0;i<n;i++){

			for(int j=0;j<n+1;j++)
				TM[i][j]=A[i][j];
			    TM[i][n]=b[i];
		}

}



void Gauss(double TM[100][101],int &n){
int i,j,k;
double factor;

for(int i=0; i<n-1;i++){
for(int j=0;j<n-1;j++){

for(int i=j+1; i<n;i++){

         if(TM[j][j]>TM[i][j]){

         }
         else{

        	 for(int k=0;k<n+1;k++){


        	            	            		 swap(TM[j][k],TM[i][k]);

        	            	 }
         }

		}
}
}

for(j=0;j<=n-2;j++)
{
	for(i=j+1;i<=n-1;i++)
{
factor = -TM[i][j]/TM[j][j];
TM[i][j]=0;
for(k=j+1;k<=n;k++) TM[i][k]=TM[i][k]+factor*TM[j][k];
	}
}

}

void ShowMatrix(double TM[100][101],int &n){
	for(int i=0;i<n;i++){
		for(int j=0;j<n+1;j++) cout<<TM[i][j]<<" ";

		cout<<endl;
	}
}



So in (1,1) it will compare all numbers below that point step by step and place the biggest number in the pivot point. ... It's just the pivot problem i'm having trouble with.
You've described the process in words pretty well. Why is stopping you from coding that? You have most of the code there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void BuildPivotTable(const double data[100][100], double pivot_table[100], int n)
{
    // for each row
    for (int i = 0; i < n; ++i)
    {
        // initialize pivot table value
        pivot_table[j] = data[i][0]);

        for (int j = 1; j < n; ++j)
        {
            // compare max pivot table with column
            pivot_table[i] = std::max(pivot_table[i], data[i][j]);
        }
    }
}


Your indentation and use of blank lines make your code difficult to check. How do you know you've not made a scope error when you've done that?
Last edited on
Yeah I'm trying to code it but something goes wrong. I would like when switching values in a column for the whole rows to be switched. I'm having difficulty understanding your code. Wouldn't be pivot_table[i] = data[i][0]); instead of pivot_table[j] = data[i][0]);

Topic archived. No new replies allowed.