Skip to content

Instantly share code, notes, and snippets.

@Chetan-Goyal
Last active May 1, 2022 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chetan-Goyal/ad7e050b00cc8e7d6d1394a528651578 to your computer and use it in GitHub Desktop.
Save Chetan-Goyal/ad7e050b00cc8e7d6d1394a528651578 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string.h>
#include <regex>
#include <time.h>
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;
}
string sender(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 (CRC): "
<< remainder << "\n";
cout << "Sent Data (Data + CRC) :"
<< codeword << "\n";
return (codeword);
}
void receiver(string data, string key)
{
cout << "Received Data: "
<< data << endl;
string crc = mod2div(data, key);
if (regex_replace(crc, regex("0"), "").length() == 0)
cout << "No Error";
else
cout << "Error";
}
int main()
{
srand(time(0));
string bin, key;
int choice;
cout << "Enter binary number\n";
bin = "100100";
// cin >> bin;
cout << "Enter binary key\n";
key = "1101";
// cin >> key;
string encodedData = sender(bin, key);
if (rand() % 10 > 7)
{
int errorIndex = rand() % encodedData.length();
encodedData[errorIndex] = encodedData[errorIndex] == '0' ? '1' : '0';
}
receiver(encodedData, key);
return 0;
}
#include<iostream>
#include<climits>
using namespace std;
int miniDist(int distance[], bool Tset[])
{
int minimum=INT_MAX,ind;
for(int k=0;k<6;k++)
{
if(Tset[k]==false && distance[k]<=minimum)
{
minimum=distance[k];
ind=k;
}
}
return ind;
}
void DijkstraAlgo(int graph[6][6],int src)
{
int distance[6];
bool Tset[6];
for(int k = 0; k<6; k++)
{
distance[k] = INT_MAX;
Tset[k] = false;
}
distance[src] = 0;
for(int k = 0; k<6; k++)
{
int m=miniDist(distance,Tset);
Tset[m]=true;
for(int k = 0; k<6; k++)
{
if(!Tset[k] && graph[m][k] && distance[m]!=INT_MAX && distance[m]+graph[m][k]<distance[k])
distance[k]=distance[m]+graph[m][k];
}
}
cout<<"Vertex\t\tDistance from source vertex"<<endl;
for(int k = 0; k<6; k++)
{
char str=65+k;
cout<<str<<"\t\t\t"<<distance[k]<<endl;
}
}
int main()
{
int graph[6][6]={
{0, 1, 2, 0, 0, 0},
{1, 0, 0, 5, 1, 0},
{2, 0, 0, 2, 3, 0},
{0, 5, 2, 0, 2, 2},
{0, 1, 3, 2, 0, 1},
{0, 0, 0, 2, 1, 0}};
DijkstraAlgo(graph,0);
return 0;
}
// Go Back N
// Stop and Wait if window size is 1
#include <math.h>
#include <time.h>
#include <cstdlib>
#include <iostream>
using namespace std;
static int r = 0;
void sender(int);
bool receiver(int, int);
bool success(int success = 80) {
return rand() % 100 < success;
}
void sender(int n, int w = 1) {
int rec = 0, i = 1, init = 0;
int bit, seq = (pow(2, w - 1) + 1);
while (i <= n) {
init = i;
cout << "\n Frames Sent : " << w << " | ";
for (int j = 0; j < w && j <= n - init; j++)
cout << i + j << " ";
for (int j = 0; j < w && j <= n - init; j++) {
bit = (i - 1) % seq;
cout << "\n Frame " << i << " | ";
rec = receiver(bit, seq);
if (rec) {
cout << "Received";
i += 1;
} else {
cout << "Timeout ";
break;
}
}
cout << "\n Frames Received | ";
while (init < i) {
cout << init << " ";
init += 1;
}
cout << endl;
}
}
bool receiver(int fbit, int seq) {
if (success()) {
if (r == fbit) {
r = (r + 1) % seq;
} else
cout << "Duplicate ";
return success(90);
} else {
cout << "Damaged ";
return false;
}
}
int main() {
srand(time(NULL));
int n = 0, w = 1;
cout << " Enter The Number Of Frames : ";
cin >> n;
cout << " Enter The window size : ";
cin >> w;
sender(n, w);
return 0;
}
/*
* Selective Repeat Slide
* Created on: 01-Nov-2014
* Author: Gourav Siddhad
*/
#include <time.h>
#include <cstdlib>
#include <iostream>
using namespace std;
static int done = 0;
void sender(int, int);
int receiver(int[], bool[], int);
bool success(int success = 80) {
return rand() % 100 < success;
}
void sender(const int f, const int w) {
int temp = 0;
int frames[w + 1], win = 0;
bool ack[w + 1], ackno[f + 1];
for (int j = 0; j <= f; j++) ackno[j] = 0;
for (int i = 0; i < f;) {
win = 0, temp = i;
for (int j = 0; j < f; j++) {
if (win == w) break;
if (ackno[j] == 0) {
ack[win] = 0;
frames[win] = j;
win++;
}
}
cout << "\n Frames Sent : " << win << " | ";
for (int j = 0; j < win; j++)
if (ack[j] == 0)
cout << frames[j] + 1 << " ";
else {
if ((frames[j] + 1) % 100 > 10)
cout << " ";
cout << " ";
}
i = receiver(frames, ack, win);
for (int j = 0; j < win; j++) {
ackno[frames[j]] = ack[j];
}
if (i == temp)
cout << "\n Time Out... Resending... ";
}
}
int receiver(int frames[], bool ack[], int win) {
for (int i = 0; i < win; i++) {
ack[i] = success();
}
cout << "\n Frames Received ";
if (win % 100 > 9)
cout << " ";
cout << "| ";
for (int i = 0; i < win; i++)
if (ack[i] == 1) {
cout << frames[i] + 1 << " ";
done++;
} else {
if ((frames[i] + 1) % 100 > 9)
cout << " ";
cout << " ";
}
cout << endl;
return done;
}
int main() {
srand(time(NULL));
int f, w;
cout << " Enter The Number Of Frames : ";
cin >> f;
cout << " Enter Window Size : ";
cin >> w;
sender(f, w);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment