How to Write a C Program for distance vector algorithm to find suitable path for transmission in C Programming Language ?
Solution:
- #include<stdlib.h>
- #define nul 1000
- #define nodes 10
- int no;
- struct node
- {
- int a[nodes][4];
- }router[nodes];
- void init(int r)
- {
- int i;
- for(i=1;i<=no;i++)
- {
- router[r].a[i][1]=i;
- router[r].a[i][2]=999;
- router[r].a[i][3]=nul;
- }
- router[r].a[r][2]=0;
- router[r].a[r][3]=r;
- }
- void inp(int r)
- {
- int i;
- printf("\nEnter dist from the node %d to other nodes",r);
- printf("\nPls enter 999 if there is no direct route\n",r);
- for(i=1;i<=no;i++)
- {
- if(i!=r)
- {
- printf("\nEnter dist to the node %d:",i);
- scanf("%d",&router[r].a[i][2]);
- router[r].a[i][3]=i;
- }
- }
- }
- void display(int r)
- {
- int i,j;
- printf("\n\nThe routing table for node %d is as follows:",r);
- for(i=1;i<=no;i++)
- {
- if(router[r].a[i][2]>=999)
- printf("\n\t\t\t %d \t no link \t no hop",router[r].a[i][1]);
- else
- printf("\n\t\t\t %d \t %d \t\t d",router[r].a[i][1],router[r].a[i][2],router[r].a[i][3]);
- }
- }
- void dv_algo(int r)
- {
- int i,j,z;
- for(i=1;i<=no;i++)
- {
- if(router[r].a[i][2]!=999 && router[r].a[i][2]!=0)
- {
- for(j=1;j<=no;j++)
- {
- z=router[r].a[i][2]+router[i].a[j][2];
- if(router[r].a[j][2]>z)
- {
- router[r].a[j][2]=z;
- router[r].a[j][3]=i;
- }
- }
- }
- }
- }
- int main()
- {
- int i,j,x,y;
- char choice;
- printf("Enter the no. of nodes required (less than 10 pls):");
- scanf("%d",&no);
- for(i=1;i<=no;i++)
- {
- init(i);
- inp(i);
- }
- printf("\nThe configuration of the nodes after initialization is as follows:");
- for(i=1;i<=no;i++)
- display(i);
- for(i=1;i<=no;i++)
- dv_algo(i);
- printf("\nThe configuration of the nodes after computation of paths is as follows:");
- for(i=1;i<=no;i++)
- display(i);
- while(1)
- {
- printf("\n\nWanna continue (y/n):");
- scanf("%c",&choice);
- if(choice=='n')
- break;
- printf("\nEnter the nodes btn which shortest path is to be found:\n");
- scanf("%d %d",&x,&y);
- printf("\nThe length of the shortest path is %d",router[x].a[y][2]);
- }
- }
Output C Program for distance vector algorithm to find suitable path for transmission
[root@localhost ~]# ./a.out
Enter the no. of nodes required (less than 10 pls):4
Enter dist from the node 1 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 2:5
Enter dist to the node 3:3
Enter dist to the node 4:7
Enter dist from the node 2 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:5
Enter dist to the node 3:999
Enter dist to the node 4:6
Enter dist from the node 3 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:3\
Enter dist to the node 2:
Enter dist to the node 4:
Enter dist from the node 4 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:
Enter dist to the node 2:
Enter dist to the node 3:
The configuration of the nodes after initialization is as follows:
The routing table for node 1 is as follows:
1 0 1
2 5 2
3 3 3
4 7 4
The routing table for node 2 is as follows:
1 5 1
2 0 2
3 no link no hop
4 6 4
The routing table for node 3 is as follows:
1 3 1
2 no link no hop
3 0 3
4 no link no hop
The routing table for node 4 is as follows:
1 no link no hop
2 no link no hop
3 no link no hop
4 0 4
The configuration of the nodes after computation of paths is as follows:
The routing table for node 1 is as follows:
1 0 1
2 5 2
3 3 3
4 7 4
The routing table for node 2 is as follows:
1 5 1
2 0 2
3 8 1
4 6 4
The routing table for node 3 is as follows:
1 3 1
2 8 1
3 0 3
4 10 1
The routing table for node 4 is as follows:
1 no link no hop
2 no link no hop
3 no link no hop
4 0 4
Wanna continue (y/n):
Enter the nodes btn which shortest path is to be found:
^C
[root@localhost ~]# clear
[root@localhost ~]# ./a.out
Enter the no. of nodes required (less than 10 pls):4
Enter dist from the node 1 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 2:5
Enter dist to the node 3:3
Enter dist to the node 4:7
Enter dist from the node 2 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:5
Enter dist to the node 3:999
Enter dist to the node 4:6
Enter dist from the node 3 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:3
Enter dist to the node 2:999
Enter dist to the node 4:2
Enter dist from the node 4 to other nodes
Pls enter 999 if there is no direct route
Enter dist to the node 1:7
Enter dist to the node 2:6
Enter dist to the node 3:2
The configuration of the nodes after initialization is as follows:
The routing table for node 1 is as follows:
1 0 1
2 5 2
3 3 3
4 7 4
The routing table for node 2 is as follows:
1 5 1
2 0 2
3 no link no hop
4 6 4
The routing table for node 3 is as follows:
1 3 1
2 no link no hop
3 0 3
4 2 4
The routing table for node 4 is as follows:
1 7 1
2 6 2
3 2 3
4 0 4
The configuration of the nodes after computation of paths is as follows:
The routing table for node 1 is as follows:
1 0 1
2 5 2
3 3 3
4 5 3
The routing table for node 2 is as follows:
1 5 1
2 0 2
3 8 1
4 6 4
The routing table for node 3 is as follows:
1 3 1
2 8 1
3 0 3
4 2 4
The routing table for node 4 is as follows:
1 5 3
2 6 2
3 2 3
4 0 4
Wanna continue (y/n):
Enter the nodes btn which shortest path is to be found:
1 4
The length of the shortest path is 7
Wanna continue (y/n):
Enter the nodes btn which shortest path is to be found-
Learn More :
Algorithm
- C program generates a random number and uses an algorithm to generate 9 other numbers.
- C Program Fraction To Decimal
- C Program Bankers Algorithm Example
- C Program to find MST(Minimum Spanning Tree) using Prim's Algorithm
- C Program Dijkstra's Shortest Path Algorithm
- C Program to Implement Dijkstra's Algorithm
Find
- Find out the perfect number using c program
- Find g.c.d of two number using c program.
- Write a c program to find out NCR factor of given number
- How to Write a C program to find maximum between two numbers ?
- Write a C program to find maximum between three numbers ?
- C or C++ program to read marks of 4 subjects and find how many students are pass and fail with division
- C Program turnLEDOn to Drives Forward Until It Finds The Line
- Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.
- C Program to Find Random Number
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program to find LCM and HCF Of Two Number Using Recursion - 3
- C Program To Find LCM and HCF Of Two Number -1
- C Program To Find Length Of A String Including Blank Spaces, Tabs, And Other Special Characters
- C Program To Find Length Of String And Concatenate Two Strings
- C Program To Find Product Of Two No Using MACRO
- C Program To Find Reverse Of Any Digit Number
- C Program To Find Sum and Difference Of Two Matrices
- C Program To Find The Frequency Of A Number
- C Program To Find The Length, Reverse Of A String And To Check It Is Palindrome or Not
- C Program To Find The Maximum And Minimum Value In An Array
- C Program To Find Transpose Of A Matrix
- C Program To Find Union & Intersection Of Two Array
- C Program Finding the sum of Squares using Recursion
- C Program to Find NCR Of A Given Number Program
- C Program To Find Maximum Of Given Numbers
Vector
- Given a numerical value, check if at least one of the elements of the vector is equal to the numerical value if so, say where in the negative case, say that there is no.
- C program to Given a vector (integer or real), determine what is the maximum value of element what is the position in which this element is located
- C Program Friend & Operator: Vector Example
- Read 10 real numbers using a vector and show the least of them C Program
- C Program produsul scalar a doi vectori .
- C Program The dot product of two vectors