Advertisement
gggamergg

Distance vector

Oct 7th, 2021
1,213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int n, m;
  6.     char d;
  7.     cout << "Enter the number of routers : ";
  8.     cin >> n;
  9.     cout << "\nFind new routing table for : ";
  10.     cin >> d;
  11.     cout << "\nEnter the number of neighbours : ";
  12.     cin >> m;
  13.  
  14.  
  15.     char neighbours[m];
  16.     for (int i = 0 ; i < m ; i++) {
  17.         cout << "\nEnter the neighbour of " << d << " : ";
  18.         cin >> neighbours[i];
  19.     }
  20.  
  21. //initialize the matrix:
  22.     int arr[50][50];
  23.     for (int i = 0 ; i < n ; i++) {
  24.         for (int j = 0 ; j < m ; j++) {
  25.             arr[i][j] = -1;
  26.         }
  27.     }
  28.  
  29. //array of vertex names:
  30.     char ch[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  31.  
  32. //Input for matrix:
  33.     for (int i = 0 ; i < n ; i++) {
  34.         for (int j = 0 ; j < m; j++) {
  35.             if (ch[i] == neighbours[j]) {
  36.                 arr[i][j] = 0;
  37.             }
  38.             if (arr[i][j] == -1) {
  39.                 cout << "\nEnter distance between " << ch[i] << " - " << neighbours[j] << " : ";
  40.                 cin >> arr[i][j];
  41.             }
  42.         }
  43.     }
  44.  
  45. //Display above table:
  46.     for (int i = 0 ; i < m ; i++) {
  47.         cout << " " << neighbours[i] << "\t";
  48.     }
  49.     cout << endl;
  50.     for (int i = 0; i < n ; i++) {
  51.         for ( int j = 0 ; j < m ; j++) {
  52.             cout << ch[i] << "\t" << arr[i][j] << "\t";
  53.         }
  54.         cout << "\n";
  55.     }
  56.  
  57. //Ask for delay time:
  58.     int t[m];
  59.     for (int i = 0 ; i < m ; i++) {
  60.         cout << "\n Enter delay time for " << d << neighbours[i] << " : ";
  61.         cin >> t[i];
  62.     }
  63.  
  64. //Initialize array for new routing table
  65.     int R[50];
  66.     for (int i = 0 ; i < n ; i++) {
  67.         R[i] = 0;
  68.     }
  69.     int p = 0;
  70.  
  71. //find minimum and add value to new routing table
  72.     for (int i = 0; i < n; i++) {
  73.         int min = 999;
  74.         for (int j = 0 ; j < m; j++) {
  75.             if (arr[i][j] < min && arr[i][j] != -1) {
  76.                 min = arr[i][j];
  77.                 p = j;
  78.  
  79.             }
  80.         }
  81.         R[i] = min + t[p];
  82.  
  83.     }
  84.  
  85. //Display the new routing table
  86.     for (int i = 0 ; i < n ; i++) {
  87.         if (ch[i] == d) {
  88.             R[i] = 0;
  89.             cout << R[i] << endl;
  90.         }
  91.         else {
  92.             cout << R[i] << endl;
  93.         }
  94.     }
  95.     return 0;
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement