Skip to content

Instantly share code, notes, and snippets.

@PradyumnaKrishna
Last active May 1, 2022 17:31
Show Gist options
  • Save PradyumnaKrishna/fe80c76ed66ab3ca78b92ba015e4f3a5 to your computer and use it in GitHub Desktop.
Save PradyumnaKrishna/fe80c76ed66ab3ca78b92ba015e4f3a5 to your computer and use it in GitHub Desktop.
/*
* CRC
* Created on: 01-Oct-2014
* Author: Gourav Siddhad
*/
#include <cstring>
#include <iostream>
#define MAX 20
using namespace std;
char myxor(char x, char y) {
if (x == y)
return '0';
else
return '1';
}
int main() {
char msg[MAX], gen[MAX], temp[MAX], trans[MAX];
unsigned int m, g, k;
cout << " Enter The Message : ";
cin >> msg; // Input The Message To Be Transmitted
strcpy(temp, msg);
m = strlen(msg);
cout << " Enter The Generator (Must Start With 1) : ";
cin >> gen; // Input The Generator Polynomial
g = strlen(gen);
if (strlen(msg) == 0 || strlen(gen) == 0) {
cout << " Can't Generate Message To Be Transmitted .. ";
return 0;
}
for (k = 0; k < g - 1; ++k) // Appending Zero's At The End Of Message
temp[m++] = '0';
temp[m] = '\0';
strcpy(trans, temp);
unsigned int i = 0;
while (i <= (m - g)) // Starting Divisio0n
{
if (temp[i] == '1') {
for (k = 0; k < g; k++)
temp[i + k] = myxor(temp[i + k], gen[k]);
}
if (temp[i] == '1')
continue;
else
++i;
}
for (k = 0; k < g; k++) // Conctatinating Last Bits To Transmitting Message
trans[m - k] = temp[m - k];
cout << "\n The Message To Be Transmitted Is : ";
cout << trans;
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;
}
/*
* 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;
}
// 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment