creating a triangular matrix

i am struggling with producing the right output through this code which should take any normal matrix and convert it into an upper triangular matrix.
here is my current code:

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
#include<iostream>
using namespace std;

int main()
{
int n,i,j;
double a[10][10]={0};

//inputing matrix
cout << "enter the number of elements in the rows: ";
cin >> n;
cout << "enter elements:" << endl;
for (i=0; i <= n; i++)
   for(j=0; j <= n; j++)
	cin >> a[i][j];

cout << "augmented matrix:" << endl;
for (i=0; i <= n; i++)
{
     for (j=0; j<= n; j++)
	cout << a[i][j] <<"    ";
	cout << endl;
}

//triangular matrix

for(j=0; j < n; j++){
	      i=0;
	if (a[0][0]!=1){
		a[i][j]= a[i][j]/a[0][0];
			}	  
		  }

for(j=0; j < n; j++){
	       i=1;
	if (a[1][0]!=0){
	  	a[i][j]= (a[i][j])-(a[1][0] * a[0][j]);
			}	  
		  }

for(j=0; j < n; j++){
	       i=2;
	if (a[2][0]!=0){
	 	 a[i][j]= a[i][j]-(a[2][0] * a[0][j]);
			}	  
		  }

for(j=0; j < n; j++){
	        i=1;
	if (a[1][1] != 1){
	 	 a[i][j]= a[i][j]/a[1][1];
			 }	  
		  }

for(j=0; j < n; j++){
	         i=2;
	if (a[2][1]!=0){
		  a[i][j]= a[i][j]-(a[2][1]*a[1][j]);
			}	  
		  }
cout << "triangular matrix" << endl;
for (i=0; i <= n; i++)
{
     for (j=0; j <= n; j++)
	cout << a[i][j] <<"    ";
	cout << endl;

}
 

 return 0;
}




the output comes out as:
augmented matrix:
5 5 6
2 3 2
2 3 2
triangular matrix
1 5 6
0 1 2
0 0 2

please help.
Last edited on
What output are you expecting?

PS : Please use code tags
it has to do row operations to create a matrix with zeros under the main diagonal but it seems my code only changes one particular element within the row. for example the first loop is supposed to take the first element in the matrix and divide the whole first row in order to make it one but it does not carry out the whole row operation. the other elements remain unchanged
1
2
3
4
5
6
for(j=0; j < n; j++){
	      i=0;
	if (a[0][0]!=1){
		a[i][j]= a[i][j]/a[0][0];
			}	  
		  }
You still didn't gave me the output you are expecting... Just give me that so that I can understand the logic of your program
And to add to what programmer first said. Also edit your original post and put everything between code tags like you did in your second post @muthavhatsindi.
for a given matrix:

3 6 9
6 5 1
2 1 2

it should cout a matrix (through row operation) of:

1 2 3
0 1 2.43
0 0 3.29
and whats the pattern/logic/calculation here?
oh sorry for that, but yeah its basically Gaussian elimination so from the first row it should take the element 3 and divide the whole row by three to change the first element in the firs row to 1 and the other elements to the appropriate outputs after dividing. after getting a one then we use the first row to change the other rows by multiplying the number which is the first element in each row to row one then subtracting row one to that particular row being the 2nd and third successively. after that operation we should be having the first column having one at the top and zeros bellow it. i hope that clears it up a bit more.
I dont now much about Gaussain elemination but when I googled I found this

http://computingforbeginners.blogspot.in/2012/12/gauss-elimination-cc-program.html
oh i found my problem. the loop was taking the reference number from my array after it had changed so that is why the rest of my elements in the row were not changing. i had to declare a variable that held the reference numbers constant until the end of the loop. thanx for the help.
Topic archived. No new replies allowed.