Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Routing Protocols Using Border Gateway Protocol(BGP) in C

#include <stdio.h>
#include<conio.h>
int main()
{
int n;
int i,j,k;
int a[10][10],b[10][10];
printf("\n Enter the number of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\n Enter the distance between the host %d - %d:",i+1,j+1);
scanf("%d",&a[i][j]);
}}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>a[i][k]+a[k][j])
{
a[i][j]=a[i][k]+a[k][j];
}}}}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
b[i][j]=a[i][j];
if(i==j)
{
b[i][j]=0;
}
}}
printf("\n The output matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
getch();
}


Output:
[univ10 @ www univ10]$ cc bgp.c
[univ10 @ www univ10]$ ./a.out

Enter the number of nodes:4

Enter the distance between the host 1 - 1:5
Enter the distance between the host 1 - 2:9
Enter the distance between the host 1 - 3:6
Enter the distance between the host 1 - 4:4
Enter the distance between the host 2 - 1:2
Enter the distance between the host 2 - 2:1
Enter the distance between the host 2 - 3:8
Enter the distance between the host 2 - 4:3
Enter the distance between the host 3 - 1:6
Enter the distance between the host 3 - 2:1
Enter the distance between the host 3 - 3:4
Enter the distance between the host 3 - 4:2
Enter the distance between the host 4 - 1:5
Enter the distance between the host 4 - 2:1
Enter the distance between the host 4 - 3:8
Enter the distance between the host 4 - 4:2

5 9 6 4
2 1 8 3
6 1 4 2
5 1 8 2

The output matrix:
0 5 6 4
2 0 8 3
3 1 0 2
3 1 8 0