Here I have written a program to find the center of tree .It will tell
you the vertex number which is the center of tree. Tree has been
represented
here by using adjacency matrix. In line no 109 i am using an array
of pointers to store the addresses of elements of two dimensional
array.
It showing me segmentation fault in line no 119. I want to know why i
am not able to store the addresses of elements of two dimensional
array.
Or is there another way to storing the address of those elements??
Please also explain what it means when compiler is showing
segmentation fault.
//Program to find center of any general tree
1 #include<stdio.h>
2 #include<stdlib.h>
3 int count(int g[][50], int n);
4 void merge(int g[][50], int n, int m);
5 main()
6 {
7 int g[50][50],a[50],i,j,n,m ;
8 printf("Enter no of vertex in graph\n");
9 scanf("%d",&n);
10 m = n;
11 printf("VALUE OF m = %d \n",m);
12 for(i=0;i<n;i++)
13 {
14
15 for(j=0;j<n;j++)
16 {
17 printf("Enter 1 if there is edge between vertex %d and %d else
enter 0\n",i+1,j+1);
18 scanf("%d",&g[i][j]);
19
20
21 }
22 }
23 merge(g,n,m);
24 }
25 int count(int g[][50], int n)
26 {
27 int i, j, c=0,s;
28 for(i=0;i<n;i++)
29 {
30 s = 0;
31 for(j=0;j<n;j++)
32 {
33 printf("%d ",g[i][j]);
34 s = s + g[i][j];
35 //if(g[i][j] == 1)
36 //k = j;
37 }
38 printf("\n");
39 if(s==1)
40 {
41 c++;
42 //p[i] = k;
43 }
44 }
45 printf("VALUE OF C= %d\n",c);
46 return c;
47 }
48 void merge(int g[][50],int n, int m)
49 {
50 int k,i,j,ver[1],s,sum,l,b[20],q=0;
51 int *a[20];
52 static int p=0;
53 k = count(g, n);
54 printf("VALUE OF K = %d\n",k);
55
56 if(k == m-1)
57 {
58 for(i=0;i<n;i++)
59 {
60 s=0;
61 for(j=0;j<n;j++)
62 {
63 s = s + g[i][j];
64 }
65 if(s > 1)
66 {
67 printf("center of tree = %d\n",i);
68 }
69 }
70 exit(0);
71 }
72 else if(k == m)
73 {
74 for(i=0;i<n;i++)
75 {
76 for(j=0;j<n;j++)
77 {
78 printf("%d ",g[i][j]);
79 if(g[i][j]==1)
80 ver[p++] = i;
81 else
82 continue;
83 }
84 printf("\n");
85 }
86 printf("Vertex %d and %d are center of the
tree",ver[0],ver[1]);
87 exit(0);
88 }
89
90 else
91 // exit(0);
92 {
93 for(i=0;i<n;i++)
94 {
95 sum =0;
96 for(j=0;j<n;j++)
97 {
98 printf("%d ",g[i][j]);
99 sum = sum + g[i][j];
100 if(g[i][j] == 1)
101 l = j;
102 }
103 printf("\n");
104 printf("VALUE OF SUM = %d\n",sum);
105 if(sum == 1)
106 {
107
108 g[i][l] = 0;
109 a[i] = &g[l][i];
110
111 p++;
112 m = m -1;
113 printf("VALUE OF M = %d\n",m);
114 }
115
116 }
117 for(i=0;i<=n;i++)
118 {
119 *(a[i]) = 0;
120 printf("%d",*(a[i]));
121 }
122
123 merge(g,n,m);
124 }
125 }
--
comp.lang.c.moderated - moderation address:
cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.