+ 1

Please help me

#include <stdio.h> int g[10][10], vertex[10], visited[10]; void DFS(int i); int main() { int n; printf("Enter the number of vertices: "); scanf("%d", &n); printf("Enter the value of vertices: "); for (int i = 0; i < n; i++) { scanf("%d", &vertex[i]); } printf("Enter the adjacency matrix:\n"); for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { scanf("%d", &g[j][k]); } } // Initialize the visited array for (int i = 0; i < n; i++) { visited[i] = 0; } printf("DFS starting from vertex 0:\n"); DFS(0); return 0; } void DFS(int i) { printf(" %d", vertex[i]); visited[i] = 1; for (int j = 0; j < n; j++) { if (g[i][j] == 1 && !visited[j]) { DFS(j); } } }

8th Jan 2024, 4:33 PM
Vikas Pal
Vikas Pal - avatar
9 ответов
+ 2
Pass n as an argument to DFS void DFS(int i, int n)
9th Jan 2024, 12:04 AM
Chris Coder
Chris Coder - avatar
+ 1
Why do you limit your matrix to 10 when you give the value of n
10th Jan 2024, 3:48 PM
toumi toufik
toumi toufik - avatar
0
Hy
10th Jan 2024, 1:04 PM
Shiva Tharoo
Shiva Tharoo - avatar
0
Could u please provide the corrected code
10th Jan 2024, 3:55 PM
Vikas Pal
Vikas Pal - avatar
0
#include <stdio.h> int g[10][10], vertex[10], visited[10]; void DFS(int i, int n); int main() { int n; printf("Enter the number of vertices: "); scanf("%d", &n); printf("Enter the value of vertices: "); for (int i = 0; i < n; i++) { scanf("%d", &vertex[i]); } printf("Enter the adjacency matrix:\n"); for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { scanf("%d", &g[j][k]); } } // Initialize the visited array for (int i = 0; i < n; i++) { visited[i] = 0; } printf("DFS starting from vertex 0:\n"); DFS(0, n); return 0; } void DFS(int i, int n) { printf(" %d", vertex[i]); visited[i] = 1; for (int j = 0; j < n; j++) { if (g[i][j] == 1 && !visited[j]) { DFS(j, n); } } }
10th Jan 2024, 4:59 PM
toumi toufik
toumi toufik - avatar
0
Still I'm not getting the output...please help me
10th Jan 2024, 5:10 PM
Vikas Pal
Vikas Pal - avatar
0
What compiler are you using?
11th Jan 2024, 3:30 AM
Chris Coder
Chris Coder - avatar
0
C
11th Jan 2024, 7:55 AM
toumi toufik
toumi toufik - avatar
0
Vs code
11th Jan 2024, 8:05 AM
Vikas Pal
Vikas Pal - avatar