Skip to content

Commit d546bf8

Browse files
authored
Add files via upload
1 parent e403bc7 commit d546bf8

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

Matrix/02 Lower Tringular matrix.c

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
struct Matrix
4+
{
5+
int *A;
6+
int n;
7+
};
8+
void Set(struct Matrix *m,int i,int j,int x)
9+
{
10+
if(i>=j)
11+
m->A[m->n*(j-1)+(j-2)*(j-1)/2+i-j]=x; // using column major formula
12+
}
13+
int Get(struct Matrix m,int i,int j)
14+
{
15+
if(i>=j)
16+
return m.A[m.n*(j-1)+(j-2)*(j-1)/2+i-j];
17+
else
18+
return 0;
19+
}
20+
void Display(struct Matrix m)
21+
{
22+
int i,j;
23+
for(i=1;i<=m.n;i++)
24+
{
25+
for(j=1;j<=m.n;j++)
26+
{
27+
if(i>=j)
28+
printf("%d ",m.A[m.n*(j-1)+
29+
(j-2)*(j-1)/2+i-j]);
30+
else
31+
printf("0 ");
32+
}
33+
printf("\n");
34+
}
35+
}
36+
int main()
37+
{
38+
struct Matrix m;
39+
int i,j,x;
40+
41+
printf("Enter Dimension\n");
42+
scanf("%d",&m.n);
43+
m.A=(int *)malloc(m.n*(m.n+1)/2*sizeof(int));
44+
printf("enter all elements");
45+
for(i=1;i<=m.n;i++)
46+
{
47+
for(j=1;j<=m.n;j++)
48+
{
49+
scanf("%d",&x);
50+
Set(&m,i,j,x);
51+
}
52+
}
53+
printf("\n\n");
54+
Display(m);
55+
56+
57+
return 0;
58+
}

0 commit comments

Comments
 (0)