Professional Documents
Culture Documents
Delhi-110042
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
#include<iostream>
#include<string>
using namespace std;
int main() {
int a[20], b[30], i, j, k, count, n;
cout<<"Enter frame size:";
cin>>n;
cout<<"Enter the frame in the form of 0 and 1 :";
for(i=0; i<n; i++)
cin>>a[i];
i=0;
count=1;
j=0;
while(i<n){
if(a[i]==1){
b[j]=a[i];
for(k=i+1; a[k]==1 && k<n && count<5; k++){
j++;
b[j]=a[k];
count++;
if(count==5){
j++;
b[j]=0;
}
i=k;
}
}
else{
b[j]=a[i];
}
i++;
j++;
}
cout<<"After Bit Stuffing :";
for(i=0; i<j; i++)
cout<<b[i];
return 0;
}
#include <iostream>
using namespace std;
int main() {
string str;
cout << "Enter the input Data having 0s and 1s only:";
cin >> str;
string stuff = "";
string add = "01111110";
stuff += add;
int i = 0;
while (i < str.length()) {
if (str.length() - i >= 8 && str.substr(i, 8) == "01111110"){ stuff +=
str.substr(i, 8);
stuff += add;
i = i + 8;
}
else
stuff += str[i++];
}
stuff += add;
cout << "The stuffed bit string is: " << stuff << endl;
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
int i, j, n, p, count = 0;
char a[30], b[4] = "dle";
printf("Enter the string: ");
scanf("%s", a);
n = strlen(a);
printf("length is %d \n", n);
printf("\nframe after stuffing:\n");
printf("dlestx");
for (i = 0; i < n; i++){
count = 0;
p = i;
for (int j = 0; j < 3; j++){
if (a[i] == b[j]){
count = count + 1;
i++;
} }
if (count != 3){
i = p;
}
if (count == 3){
printf("dledle");
}
else{
printf("%c", a[i]); } }
printf("dleetx\n");
return 0;
}
#include <iostream>
using namespace std;
int main() {
int w, i, f, frames[50];
cout << "Enter window size: ";
cin >> w;
cout << "\nEnter number of frames to transmit: ";
cin >> f;
cout << "\nEnter " << f << " frames: ";
for (i = 1; i <= f; i++)
cin >> frames[i];
cout << "\nWith sliding window protocol the frames will be sent in the
following manner (assuming no corruption of frames)\n\n";
cout << "After sending " << w << " frames at each stage sender waits
for acknowledgement sent by the receiver\n\n";
for (i = 1; i <= f; i++) {
if (i % w == 0) {
cout << frames[i] << "\n";
cout << "Acknowledgement of above frames sent is received by
sender\n\n";
}
else
cout << frames[i] << " ";
}
if (f % w != 0)
cout << "\nAcknowledgement of above frames sent is received by
sender\n";
return 0;
}
#include<bits/stdc++.h>
using namespace std;
class timer {
private:
unsigned long begTime;
public:
void start() {
begTime = clock();
}
unsigned long elapsedTime() {
return ((unsigned long)clock() - begTime) / CLOCKS_PER_SEC;
}
bool isTimeout(unsigned long seconds) {
return seconds >= elapsedTime(); };
int main() {
int frames[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
unsigned long seconds = 5;
srand(time(NULL));
timer t;
cout << "Sender has to send frames : ";
for (int i = 0; i < 10; i++)
cout << frames[i] << " ";
cout << endl;
int count = 0;
bool delay = false;
cout << endl << "Sender\t\t\t\t\tReceiver" << endl;
do {
bool timeout = false;
cout << "Sending Frame : " << frames[count];
cout.flush();
cout << "\t\t";
t.start();
if (rand() % 2) {
int to = 24600 + rand() % (64000 - 24600) + 1;
for (int i = 0; i < 64000; i++)
for (int j = 0; j < to; j++) { } }
if (t.elapsedTime() <= seconds) {
cout << "Received Frame : " << frames[count] << " ";
if (delay) {
cout << "Duplicate";
delay = false; }
cout << endl;
count++; }
else {
cout << "---" << endl;
cout << "Timeout" << endl;
timeout = true; }
t.start();
if (rand() % 2 || !timeout) {
int to = 24600 + rand() % (64000 - 24600) + 1;
for (int i = 0; i < 64000; i++)
for (int j = 0; j < to; j++) { }
if (t.elapsedTime() > seconds) {
cout << "Delayed Ack" << endl;
count--;
delay = true; }
else if (!timeout)
cout << "Acknowledgement : " << frames[count] - 1 <<
endl; } }
while (count != 10);
return 0; }
#include<bits/stdc++.h>
using namespace std;
int main(){
string data,gen;
cout<<"Enter data:";
cin>>data;
cout<<"Enter Key:";
cin>>gen;
string temp=data;
for(int i=0;i<gen.length()-1;i++)
temp+='0';
string checksum;
checksum=xorem(temp,gen);
cout<<"Encoded data:";
cout<<data+checksum;
cout<<"\nCheck Sum:";
cout<<checksum;
cout<<"\n-------------------------------------Reciever Side---------------------
------------------\n";
}
#include <bits/stdc++.h>
using namespace std;
struct node {
unsigned dist[20];
unsigned from[20];
} rt[10];
int main() {
int costmat[20][20];
int nodes, i, j, k, count = 0;
cout << "\nEnter the number of nodes : ";
cin >> nodes;
cout << "\nEnter the cost matrix : \n";
for (i = 0; i < nodes; i++) {
for (j = 0; j < nodes; j++) {
cin >> costmat[i][j];
costmat[i][i] = 0;
rt[i].dist[j] = costmat[i][j];
rt[i].from[j] = j; } }
do {
count = 0;
for (i = 0; i < nodes; i++) {
for (j = 0; j < nodes; j++) {
for (k = 0; k < nodes; k++){
if (rt[i].dist[j] > costmat[i][k] + rt[k].dist[j]) {
rt[i].dist[j] = rt[i].dist[k] + rt[k].dist[j];
rt[i].from[j] = k;
count++; } }}}}
while (count != 0);
for (i = 0; i < nodes; i++) {
cout << "\n For router" << i + 1 << " ";
for (j = 0; j < nodes; j++)
cout << "\n Node " << j + 1 << " via " << rt[i].from[j] + 1 << "
Distance"<<rt[i].dist[j]; }
cout << ("\n\n");
return 0; }
#include <bits/stdc++.h>
using namespace std;
int main() {
int count, src_router, i, j, k, w, v, min;
int cost_matrix[100][100], dist[100], last[100];
int flag[100];
cout<<"Enter the number of routers: ";
cin>>count;
cout<<"Enter the cost matrix values: ";
for (i = 0; i < count; i++) {
for (j = 0; j < count; j++) {
cout<<"\n"<<i<<" -> "<<j<<" : ";
cin>>cost_matrix[i][j];
if (cost_matrix[i][j] < 0)
cost_matrix[i][j] = 1000; }}
cout<<"Enter the source router:";
cin>>src_router;
for (v = 0; v < count; v++){
flag[v] = 0;
last[v] = src_router;
dist[v] = cost_matrix[src_router][v]; }
flag[src_router] = 1;
for (i = 0; i < count; i++) {
min = 1000;
for (w = 0; w < count; w++) {
if (!flag[w])
if (dist[w] < min) {
v = w;
min = dist[w];
}
}
flag[v] = 1;
for (w = 0; w < count; w++) {
if (!flag[w])
if (min + cost_matrix[v][w] < dist[w]) {
dist[w] = min + cost_matrix[v][w];
last[w] = v;
}
}
}
for (i = 0; i < count; i++) {
cout<<"\n"<<src_router<<" -> "<<i<<" : Path taken : "<<i;
w = i;
while (w != src_router) {
cout<<"\n<- "<<last[w];
w = last[w]; }
cout<<"\n Shortest path cost is: "<<dist[i];
}
}