0% found this document useful (0 votes)
13 views

All Programs

Uploaded by

pontinghassan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

All Programs

Uploaded by

pontinghassan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

/*implementing stop and wait Protocol*/

#include<stdio.h>
#include<conio.h>
void main()
{
char sen_data[30],rec_data[30];
int t,i,j,bit=0,flag,k,l,index[6],count;
clrscr();
printf("\nEnter a String ");
scanf("%s",sen_data);
l=strlen(sen_data);
for(i=0;i<l;i++)
{
index[i]='\0';
}
for(k=0,i=0;sen_data[i]!='\0';i++)
{
t=sen_data[i];
if(t=='b'|| t=='B')
{
flag=1;
index[k]=i;
k++;
}
}
count=k;
clrscr();
for(i=50;i>=0;i--)
{
clrscr();
if(i%2==0)
printf("\n\n***Working of Stop and Wait Protocol***\n\n");
else
printf("\n\n****Working of Stop and Wait Protocol***\n\n");
delay(10);
}
delay(1500);
printf("\n\nThe data to be Transmitted is : '%s'",sen_data);
printf("\n\n\nTransmission Starting ");
for(i=0;i<8;i++)
{
printf(". ");
delay(500);
}
//working logic
i=0;
k=0;
while(sen_data[i]!='\0')
{

delay(1500);
for(j=100;j>=0;j--)
{
clrscr();
printf("\n\n\nTransmitting F[%d] (i.e; '%c') ",bit,sen_data[i]);
printf("\n\nTimer : %d",j);
delay(40);
}
delay(1500);
clrscr();
label:
if(bit==0)
bit=1;
else
bit=0;
rec_data[i]=sen_data[i];
for(j=100;j>=0;j--)
{
clrscr();
printf("\n\n\n Waiting for Ack[%d] ",bit);
printf("\n\nTimer : %d",j);
delay(40);
}
//to make error
if(flag==1 && k<count)
{
if(index[k]==i)
{
if(bit==1)
bit=0;
else
bit=1;
printf("\n\nTimed out (that is data or ack lost) ...");
delay(3000);
for(j=100;j>=0;j--)
{
clrscr();
printf("\n\nRetransmitting F[%d] (that is '%c') :",bit,sen_data[i]);
printf("\n\n\nTimer : %d",j);
delay(30);
}
k++;
index[k]='\0';
goto label;
} //inner if block closing
} //error creating if block closing
i++;
}//while loop closing
printf("\n\nProtocol completed successfully ");
printf("\n\nData received at Receiver side is: '%s' ",rec_data);
getch();
}

===================================================================================
=======================================

//Go Back N
#include<stdio.h>
int main()
{
int windowsize,sent=0,ack,i;
printf("enter window size\n");
scanf("%d",&windowsize);
while(1)
{
for( i = 0; i < windowsize; i++)
{
printf("Frame %d has been transmitted.\n",sent);
sent++;
if(sent == windowsize)
break;
}
printf("\nPlease enter the last Acknowledgement received.\n");
scanf("%d",&ack);

if(ack == windowsize)
break;
else
sent = ack;
}
return 0;
}
===================================================================================
======================================
//Vector Routing
#include<stdio.h>
int dist[50][50],temp[50][50],n,i,j,k,x;
void dvr();
int main()
{
printf("\nEnter the number of nodes : ");
scanf("%d",&n);
printf("\nEnter the distance matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&dist[i][j]);
dist[i][i]=0;
temp[i][j]=j;
}
printf("\n");
}
dvr();
printf("enter value of i &j:");
scanf("%d",&i);
scanf("%d",&j);
printf("enter the new cost");
scanf("%d",&x);
dist[i][j]=x;
printf("After update\n\n");
dvr();
return 0;
}
void dvr()
{
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
for (k = 0; k < n; k++)
if (dist[i][k] + dist[k][j] < dist[i][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
temp[i][j] = k;
}

for(i=0;i<n;i++)
{
printf("\n\nState value for router %d is \n",i+1);
for(j=0;j<n;j++)
printf("\t\nnode %d via %d Distance%d",j+1,temp[i][j]+1,dist[i]
[j]);
}
printf("\n\n");

}
===================================================================================
===========================================
//Dijkstra's Shortest Path Algorithm
#include <stdio.h>
#define INFINITY 9999
#define MAX 10

void Dijkstra(int Graph[MAX][MAX], int n, int start);

void Dijkstra(int Graph[MAX][MAX], int n, int start) {


int cost[MAX][MAX], distance[MAX], pred[MAX];
int visited[MAX], count, mindistance, nextnode, i, j;

// Creating cost matrix


for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (Graph[i][j] == 0)
cost[i][j] = INFINITY;
else
cost[i][j] = Graph[i][j];

for (i = 0; i < n; i++) {


distance[i] = cost[start][i];
pred[i] = start;
visited[i] = 0;
}

distance[start] = 0;
visited[start] = 1;
count = 1;

while (count < n - 1) {


mindistance = INFINITY;

for (i = 0; i < n; i++)


if (distance[i] < mindistance && !visited[i]) {
mindistance = distance[i];
nextnode = i;
}

visited[nextnode] = 1;
for (i = 0; i < n; i++)
if (!visited[i])
if (mindistance + cost[nextnode][i] < distance[i]) {
distance[i] = mindistance + cost[nextnode][i];
pred[i] = nextnode;
}
count++;
}

// Printing the distance


for (i = 0; i < n; i++)
if (i != start) {
printf("\nDistance from source to %d: %d", i, distance[i]);
}
}
int main() {
int Graph[MAX][MAX], i, j, n, u;
n = 7;

Graph[0][0] = 0;
Graph[0][1] = 0;
Graph[0][2] = 1;
Graph[0][3] = 2;
Graph[0][4] = 0;
Graph[0][5] = 0;
Graph[0][6] = 0;

Graph[1][0] = 0;
Graph[1][1] = 0;
Graph[1][2] = 2;
Graph[1][3] = 0;
Graph[1][4] = 0;
Graph[1][5] = 3;
Graph[1][6] = 0;

Graph[2][0] = 1;
Graph[2][1] = 2;
Graph[2][2] = 0;
Graph[2][3] = 1;
Graph[2][4] = 3;
Graph[2][5] = 0;
Graph[2][6] = 0;

Graph[3][0] = 2;
Graph[3][1] = 0;
Graph[3][2] = 1;
Graph[3][3] = 0;
Graph[3][4] = 0;
Graph[3][5] = 0;
Graph[3][6] = 1;

Graph[4][0] = 0;
Graph[4][1] = 0;
Graph[4][2] = 3;
Graph[4][3] = 0;
Graph[4][4] = 0;
Graph[4][5] = 2;
Graph[4][6] = 0;

Graph[5][0] = 0;
Graph[5][1] = 3;
Graph[5][2] = 0;
Graph[5][3] = 0;
Graph[5][4] = 2;
Graph[5][5] = 0;
Graph[5][6] = 1;

Graph[6][0] = 0;
Graph[6][1] = 0;
Graph[6][2] = 0;
Graph[6][3] = 1;
Graph[6][4] = 0;
Graph[6][5] = 1;
Graph[6][6] = 0;

u = 0;
Dijkstra(Graph, n, u);

return 0;
}

You might also like