COLab File
COLab File
COLab File
CO-306
Professor 2K21/EC/249
1
EXPERIMENT-1
CODE-
#include <iostream>
#include <vector>
using namespace std;
vector<int> bitStuffing(const vector<int> &data) {
vector<int> stuffedData;
int count = 0;
for (int bit : data) {
stuffedData.push_back(bit);
if (bit == 1) {
count++;
if (count == 5) {
stuffedData.push_back(0);
count = 0; // Reset count after stuffing
}
} else {
count = 0; // Reset count if bit is 0
}
}
return stuffedData;
}
int main() {
vector<int> data = {1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0};
vector<int> stuffedData = bitStuffing(data);
cout << "Original Data: ";
for (int bit : data)
cout << bit;
cout << endl;
cout << "Stuffed Data: ";
for (int bit : stuffedData)
cout << bit;
cout << endl
return 0;
}
OUTPUT-
CODE-
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char a[50], b[100] = "", sd, ed;
int size, j = 0;
cout << "\n Enter input string :";
cin >> a;
size = strlen(a);
cout << " \n Enter starting delimiter : ";
cin >> sd;
cout << " \n Enter ending delimiter : ";
cin >> ed;
b[j++] = sd;
for (int i = 0; i < size; i++) {
if (a[i] == sd) {
b[j++] = sd;
}
b[j++] = a[i];
}
b[j++] = ed;
cout << "\n After Character Stuffing :";
cout << b << endl;
return 0;
}
OUTPUT-
CODE-
#include <iostream>
#include <string>
using namespace std;
string xor1(string a, string b) {
string result = "";
int n = b.length();
for (int i = 1; i < n; i++) {
if (a[i] == b[i])
result += "0";
else
result += "1";
}
return result;
}
string mod2div(string divident, string divisor) {
int pick = divisor.length();
string tmp = divident.substr(0, pick);
int n = divident.length();
while (pick < n) {
if (tmp[0] == '1')
tmp = xor1(divisor, tmp) + divident[pick];
else
tmp = xor1(string(pick, '0'), tmp) + divident[pick];
pick += 1;
}
if (tmp[0] == '1')
tmp = xor1(divisor, tmp);
else
tmp = xor1(string(pick, '0'), tmp);
return tmp;
}
void encodeData(string data, string key) {
int l_key = key.length();
string appended_data = (data + string(l_key - 1, '0'));
string remainder = mod2div(appended_data, key);
string codeword = data + remainder;
cout << "Remainder : " << remainder << "\n";
cout << "Encoded Data (Data + Remainder) :" << codeword << "\n";
}
int main() {
string data;
cout << "Enter data : ";
cin >> data;
string key;
cout << "Enter key : ";
cin >> key;
encodeData(data, key);
return 0;
}
OUTPUT-
CODE-
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <time.h>
#include <unistd.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;
}
OUTPUT-
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main() {
int nf, N;
int no_tr = 0;
srand(time(NULL));
cout << "Enter the number of frames : ";
cin >> nf;
cout << "Enter the Window Size : ";
cin >> N;
int i = 1;
while (i <= nf) {
int x = 0;
for (int j = i; j < i + N && j <= nf; j++) {
cout << "Sent Frame " << j << endl;
no_tr++;
}
for (int j = i; j < i + N && j <= nf; j++) {
int flag = rand() % 2;
if (!flag) {
cout << "Acknowledgment for Frame " << j << endl;
x++;
} else {
cout << "Frame " << j << " Not Received" << endl;
cout << "Retransmitting Window" << endl;
break;
}
}
cout << endl;
i += x;
}
cout << "Total number of transmissions : " << no_tr << endl;
return 0;
}
OUTPUT-
#include <iostream>
#include <cstdlib>
using namespace std;
int simulate(int);
int receiver(int);
int negack(int);
int main() {
int tmp1, tmp2, tmp3, tmp4 = 0, tmp5 = 0, i, windowsize = 4, noofPacket,
morePacket;
for (int i = 0; i < 10; i++)
rand();
noofPacket = rand() % 10;
cout << "Number of frames is: " << noofPacket << endl;
morePacket = noofPacket;
while (morePacket > 0) {
tmp1 = simulate(windowsize);
windowsize -= tmp1;
tmp4 += tmp1;
if (tmp4 > noofPacket)
tmp4 = noofPacket;
for (i = noofPacket - morePacket + 1; i <= tmp4; i++)
cout << "Sending Frame " << i << endl;
tmp2 = receiver(tmp1);
tmp3 += tmp2;
if (tmp3 > noofPacket)
tmp3 = noofPacket;
tmp2 = negack(tmp1);
tmp5 += tmp2;
if (tmp5 != 0) {
cout << "No acknowledgement for the frame " << tmp5 << endl;
cout << "Retransmitting frame " << tmp5 << endl;
}
morePacket -= tmp1;
if (windowsize <= 0)
windowsize = 4;
}
cout << "Selective Repeat Protocol Ends. All packets are successfully
transmitted." << endl;
return 0;
}
int receiver(int tmp1) {
int i;
for (i = 0; i < 5; i++)
rand();
i = rand() % tmp1;
return i;
}
int negack(int tmp1) {
int i;
for (i = 0; i < 5; i++)
rand();
i = rand() % tmp1;
return i;
}
int simulate(int windowsize) {
int tmp1, i;
for (i = 0; i < 5; i++)
tmp1 = rand();
if (tmp1 == 0)
tmp1 = simulate(windowsize);
i = tmp1 % windowsize;
if (i == 0)
return windowsize;
else
return tmp1 % windowsize;
}
OUTPUT-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char findClass(char str[]);
void separate(char str[], char ipClass);
int main() {
char str[30];
printf("Enter the IP address: ");
(void)scanf("%29s", str); // Cast to void to suppress the warning
char ipClass = findClass(str);
printf("Given IP address belongs to Class %c\n", ipClass);
separate(str, ipClass);
return 0;
}
char findClass(char str[]) {
char arr[4];
int i = 0;
while (str[i] != '.') {
arr[i] = str[i];
i++;
}
arr[i] = '\0';
int ip = atoi(arr);
if (ip >= 1 && ip <= 126)
return 'A';
else if (ip >= 128 && ip <= 191)
return 'B';
else if (ip >= 192 && ip <= 223)
return 'C';
else if (ip >= 224 && ip <= 239)
return 'D';
else
return 'E';
}
void separate(char str[], char ipClass) {
char network[30], host[30];
memset(network, 0, sizeof(network));
memset(host, 0, sizeof(host));
int i = 0, j = 0, dotCount = 0;
while (str[j] != '.' && str[j] != '\0') {
network[i++] = str[j++];
}
while (str[j] != '\0') {
if (str[j] == '.') {
dotCount++;
if (dotCount ==
(ipClass - 'A' + 1)) { // ipClass - 'A' + 1 to get the number of dots
j++;
break;
}
}
host[i++] = str[j++];
}
printf("Network ID is %s\n", network);
printf("Host ID is %s\n", host);
}
OUTPUT-
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char findClass(char str[]);
void separate(char str[], char ipClass);
int main() {
char str[30];
printf("Enter the IP address: ");
(void)scanf("%29s", str); // Limit input length to prevent buffer overflow
char ipClass = findClass(str);
printf("Given IP address belongs to Class %c\n", ipClass);
separate(str, ipClass);
return 0;
}
char findClass(char str[]) {
char arr[4];
int i = 0;
while (str[i] != '.') {
arr[i] = str[i];
i++;
}
arr[i] = '\0';
int ip = atoi(arr);
// Class A
if (ip >= 1 && ip <= 126)
return 'A';
// Class B
else if (ip >= 128 && ip <= 191)
return 'B';
// Class C
else if (ip >= 192 && ip <= 223)
return 'C';
// Class D
else if (ip >= 224 && ip <= 239)
return 'D';
// Class E
else
return 'E';
}
void separate(char str[], char ipClass) {
char network[12], host[12];
memset(network, 0, sizeof(network));
memset(host, 0, sizeof(host));
int i = 0, j = 0, dotCount = 0;
while (str[j] != '.' && str[j] != '\0') {
network[i++] = str[j++];
}
while (str[j] != '\0') {
if (str[j] == '.') {
dotCount++;
if (dotCount ==
(ipClass - 'A' + 1)) { // ipClass - 'A' + 1 to get the number of dots
j++;
break;
}
}
host[i++] = str[j++];
}
printf("Network ID is %s\n", network);
printf("Host ID is %s\n", host);
}
OUTPUT-
#include <stdio.h>
struct node {
unsigned dist[20];
unsigned from[20];
} rt[10];
int main() {
int costmat[20][20];
int nodes, i, j, k;
printf("\nEnter the number of nodes : ");
(void) scanf("%d", &nodes);
printf("\nEnter the cost matrix :\n");
for (i = 0; i < nodes; i++) {
for (j = 0; j < nodes; j++) {
(void)scanf("%d", &costmat[i][j]);
rt[i].dist[j] = costmat[i][j];
rt[i].from[j] = j;
}
}
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;
}
}
}
}
printf("\n\n");
for (i = 0; i < nodes; i++) {
printf("\n\n For router %d\n", i + 1);
for (j = 0; j < nodes; j++) {
printf("\t\nnode %d via %d Distance %d ", j + 1, rt[i].from[j] + 1,
rt[i].dist[j]);
}
}
printf("\n\n");
return 0;
}
OUTPUT-
OUTPUT-