help need to get a program to upper and lower traingular matrix

i mangaged to get the code and this is c program
program
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c,i,j,a[20][20];
clrscr();
printf("\n neter the rows and colums of matrix respectively");
scanf("%d%d",&r,&c);
printf("\n enter the terms of matrix");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
printf("%3d",a[i][j]);

}
}
if(r==c);
{
printf("\n the lower traingular matrix is");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i!=j)
{
if(i<j)
{
printf("%3d",a[i][j]);
}
}
}
printf("\n the upper traingular matrix is");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i!=j)
{
if(i>j)
{
printf("%3d",a[i][j]);
}
}
}
}
}
}
getch();
}
output
enter the rows and columns 3 3
enter terms 1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
the lower triangular matrix is 2 3 // i need to get lower traingular matrix as 2 3 6
the upper traingular matrix is 4 7 8
PLEASE HELP
Here's a case where proper indentation helps to make things much clearer. Here's the code, unchanged except for the layout:
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
#include <stdio.h>
#include <conio.h>

void main()
{
    int r,c,i,j,a[20][20];
    clrscr();
    printf("\n neter the rows and colums of matrix respectively");
    scanf("%d%d",&r,&c);
    printf("\n enter the terms of matrix");
    
    for (i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    
    for(i=0;i<r;i++)
    {
        printf("\n");
        for(j=0;j<c;j++)
        {
            printf("%3d",a[i][j]);
        }
    }
    
    if (r==c);
    {
        printf("\n the lower traingular matrix is");
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                if(i!=j)
                {
                    if(i<j)
                    {
                        printf("%3d",a[i][j]);
                    }
                }
            }
            
            printf("\n the upper traingular matrix is");
            for(i=0;i<r;i++)
            {
                for(j=0;j<c;j++)
                {
                    if(i!=j)
                    {
                        if(i>j)
                        {
                            printf("%3d",a[i][j]);
                        }
                    }
                }
            }
        }
    }
    
    getch();
}

Now it becomes clear. At line 32 is the start of a for loop. The closing brace for that loop is at line 59. Nested within that loop is another for loop, starting at line 46 and ending at line 58.

I think all that is needed here is to disentangle these two loops. Oh and also remove the unwanted semicolon at line 29.
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
#include <stdio.h>
#include <conio.h>

int main()
{
    int r,c,i,j,a[20][20];
    clrscr();
    printf("\n enter the rows and colums of matrix respectively ");
    scanf("%d%d",&r,&c);
    printf("\n enter the terms of matrix ");

    for (i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }

    for(i=0;i<r;i++)
    {
        printf("\n");
        for(j=0;j<c;j++)
        {
            printf("%3d",a[i][j]);
        }
    }

    if (r==c)
    {
        printf("\n the lower triangular matrix is");

        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                if(i<j)
                {
                    printf("%3d",a[i][j]);
                }
            }
        }

        printf("\n the upper triangular matrix is");
        for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
                if(i>j)
                {
                    printf("%3d",a[i][j]);
                }
            }
        }

    }

    getch();
}


Edit:
Also there's no need to test both (i!=j) and (i<j). The i!=j can be omitted.
Last edited on
thanks
Topic archived. No new replies allowed.