fork download
  1. #include<stdio.h>
  2.  
  3. void dist_vector(int n);
  4. void init(int n);
  5.  
  6. // Creating structure node
  7. struct node {
  8. unsigned int dist[20], nexthop[20];
  9. } route[10];
  10.  
  11. int i, j;
  12.  
  13. // Main function
  14. void main() {
  15. int n, i, j;
  16. printf("Enter the number of routers: ");
  17. scanf("%d", &n);
  18.  
  19. init(n);
  20.  
  21. printf("Enter the cost matrix (999 for no link):\n");
  22. for(i = 0; i < n; i++)
  23. for(j = 0; j < n; j++)
  24. scanf("%d", &route[i].dist[j]);
  25.  
  26. dist_vector(n);
  27.  
  28. printf("\n------------------------------------");
  29. printf("\nUpdated Distance Vector Table\n");
  30. printf("--------------------------------------\n");
  31.  
  32. for(i = 0; i < n; i++) {
  33. for(j = 0; j < n; j++) {
  34. }
  35. printf("\n");
  36. }
  37.  
  38. printf("%d\t", route[i].dist[j]);
  39. printf("\n----------------------------\n");
  40.  
  41. for(i = 0; i < n; i++) {
  42. printf("\nRouting table for node %c table\n", 65 + i);
  43. printf("-------------------------\n");
  44. printf("Desti\t Cost\t Next hop\n");
  45. printf("--------------------------\n");
  46.  
  47. for(int j = 0; j < n; j++)
  48. if(i != j)
  49. printf("%c\t%d\t%c\n", 65 + j, route[i].dist[j], 65 + route[i].nexthop[j]);
  50. }
  51. }
  52.  
  53. // Initialization
  54. void init(int n) {
  55. int i, j;
  56. for(i = 0; i < n; i++) {
  57. for(j = 0; j < n; j++) {
  58. if(i != j) {
  59. route[i].dist[j] = 999;
  60. route[i].nexthop[j] = -20;
  61. }
  62. route[i].dist[i] = 0;
  63. route[i].nexthop[j] = -20;
  64. }
  65. }
  66. }
  67.  
  68. void dist_vector(int n) {
  69. int count;
  70. do {
  71. count = 0;
  72. for (int i = 0; i < n; i++) {
  73. for (int j = 0; j < n; j++) {
  74. for (int k = 0; k < n; k++) {
  75. if ((route[i].dist[j]) > (route[i].dist[k] + route[k].dist[j])) {
  76. route[i].dist[j] = route[i].dist[k] + route[k].dist[j];
  77. route[i].nexthop[j] = k;
  78. count = 1;
  79. }
  80. }
  81. }
  82. }
  83. } while (count);
  84. }
  85.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Enter the number of routers: Enter the cost matrix (999 for no link):

------------------------------------
Updated Distance Vector Table
--------------------------------------
0	
----------------------------