Click to Copy Code Snippets

1D Parity

#include 
#include 
int main(int argc, char** argv){
    int array [50], i, size; int count, parity;
    printf ("Enter the size of bit stream: \n");
    scanf ("%d", &size) ;
    printf ("Enter the bit stream:\n") ;
    for (i = 0; i < size; i++) {
        scanf ("%d", &array[i]) ;
    }
    printf ("Input bit stream:");
    for (i = 0; i < size; i++) {
        printf ("%d", array[i]);
    }
    for (i = 0; i < size; i++) {
        if (array[i] == 1){
            count++;
        }
        if (count % 2 == 0) {
            parity = 0;
        } else {
            parity = 1;
        }
    }
    printf ("\nThus the Parity bit is %d\n", parity);
    printf ("The final bit stream is\n") ;
    for (i = 0; i < size; i++) {
        printf ("%d", array[i]);
    }
    printf ("%d", parity) ;

}
    

2D Parity

#include 
#include 

int main() {
    int i, j, row, column, count;
    int array[11][11];

    printf("Enter number of rows: ");
    scanf("%d", &row);
    printf("Enter number of columns: ");
    scanf("%d", &column);


    printf("Enter the bit stream:\n");
    for (j = 0; j < row; j++) {
        for (i = 0; i < column; i++) {
            scanf("%d", &array[j][i]);
        }
    }

    printf("Initial bit stream is:\n");
    for (j = 0; j < row; j++) {
        for (i = 0; i < column; i++) {
            printf("%d ", array[j][i]);
        }
        printf("\n");
    }
    for (j = 0; j < row; j++) {
        count = 0;
        for (i = 0; i < column; i++) {
            if (array[j][i] == 1) {
                count++;
            }
        }
        array[j][column] = (count % 2 == 0) ? 0 : 1;
    }
    for (i = 0; i < column; i++) {
        count = 0;
        for (j = 0; j < row; j++) {
            if (array[j][i] == 1) {
                count++;
            }
        }
        array[row][i] = (count % 2 == 0) ? 0 : 1;
    }
    count = 0;
    for (j = 0; j < row; j++) {
        if (array[j][column] == 1) {
            count++;
        }
    }
    for (i = 0; i < column; i++) {
        if (array[row][i] == 1) {
            count++;
        }
    }
    array[row][column] = (count % 2 == 0) ? 0 : 1;
    printf("Output bit stream is:\n");
    for (j = 0; j <= row; j++) {
        for (i = 0; i <= column; i++) {
            printf("%d ", array[j][i]);
        }
        printf("\n");
    }

    return (EXIT_SUCCESS);
}
    

CRC

#include 
#include 
#include 

#define MAX_DATA_SIZE 64
#define MAX_REM_SIZE 32

char data[MAX_DATA_SIZE], rem[MAX_REM_SIZE];
char gen_poly[MAX_REM_SIZE];
char poly8[] = "100000111", poly10[] = "11000110101";
char poly16[] = "10001000000100001";
char poly32[] = "100000100110000010001110110110111";
int data_len, poly_len, i, j, e, c, ch;

void chooseGeneratingPolynomial() {
    printf("\nPress 1 for CRC-8"
           "\nPress 2 for CRC-10"
           "\nPress 3 for CRC-16"
           "\nPress 4 for CRC-32\nChoice: ");
    scanf("%d", &ch);

    switch (ch) {
        case 1:
            strcpy(gen_poly, poly8);
            break;
        case 2:
            strcpy(gen_poly, poly10);
            break;
        case 3:
            strcpy(gen_poly, poly16);
            break;
        case 4:
            strcpy(gen_poly, poly32);
            break;
        default:
            printf("Invalid choice. Defaulting to CRC-8.\n");
            strcpy(gen_poly, poly8);
    }
    poly_len = strlen(gen_poly);
    printf("\nGenerating polynomial is: %s\n", gen_poly);
}

void xorOperation() {
    for (c = 1; c < poly_len; c++)
        rem[c] = (rem[c] == gen_poly[c]) ? '0' : '1';
}

void remCal() {
    for (e = 0; e < poly_len; e++)
        rem[e] = data[e];

    do {
        if (rem[0] == '1')
            xorOperation();
        for (c = 0; c < poly_len - 1; c++)
            rem[c] = rem[c + 1];
        rem[c] = data[e++];
    } while (e <= data_len + poly_len - 1);
}

void sender() {
    data_len = strlen(data);
    for (i = data_len; i < data_len + poly_len - 1; i++)
        data[i] = '0';
    data[data_len + poly_len - 1] = '\0';

    printf("\nModified data is: %s", data);
    remCal();
    printf("\n\nRemainder is: %s", rem);

    for (i = data_len; i < data_len + poly_len - 1; i++)
        data[i] = rem[i - data_len];
    data[data_len + poly_len - 1] = '\0';

    printf("\n\nFinal codeword being transmitted is: %s\n", data);
}

int checkError() {
    remCal();
    for (i = 0; (i < poly_len - 1) && (rem[i] != '1'); i++);
    if (i < poly_len - 1) {
        printf("\nError detected, data processing aborted!\n\n");
        return 1;
    } else {
        printf("\nNo error detected, proceed to data processing.\n\n");
        return 0;
    }
}

void receiver() {
    printf("\nCode received!\nPress 0 to detect error"
           "\nPress 1 to introduce errors: ");
    scanf("%d", &i);

    if (i == 0) {
        checkError();
    } else if (i == 1) {
        do {
            do {
                printf("\nEnter the position where error is to be inserted (1 to %d): ", data_len + poly_len - 1);
                scanf("%d", &i);
            } while (i <= 0 || i > data_len + poly_len - 1);
            data[i - 1] = (data[i - 1] == '0') ? '1' : '0';
            printf("\nPress 1 to introduce more errors\nPress 0 to continue\nOption: ");
            scanf("%d", &ch);
        } while (ch == 1);

        printf("\nData at receiver: %s\n", data);
        checkError();
    } else {
        printf("Invalid option.\n");
    }
}

int main() {
    int data_size;

    printf("Enter data size (max 32 bits): ");
    scanf("%d", &data_size);

    if (data_size > 32 || data_size <= 0) {
        printf("Invalid data size. Exiting.\n");
        return 1;
    }

    printf("\nEnter the %d-bit data: ", data_size);
    scanf("%s", data);

    if (strlen(data) != data_size) {
        printf("Data size mismatch. Exiting.\n");
        return 1;
    }

    chooseGeneratingPolynomial();
    sender();
    receiver();

    return 0;
}
    

Neighbourhood Table

#include 
#include 
#include 
#include 
int i,j,n;
int x[15],y[15];
float distance [15][15];
void createNetwork(){
    int x_range,y_range;
    printf("Enter the number of nodes(max 15):");
    scanf("%d",&n);
    printf("Enter the X-Coordinates ranges:");
    scanf("%d",&x_range);
    printf("Enter the Y-Coordinates ranges:");
    scanf("%d",&y_range);
    srand(time(NULL));
    for(i=0;i=distance[i][j])
            printf("%d:( %.2f )\t",j+1,distance[i][j]);
        }
    }
}
int main(int argc, char** argv) {
    createNetwork();
    computeDistance();
    findNeighbor();
return (EXIT_SUCCESS);
}
    

Vector

#include 
#include 

int findShortestPath(int G[6][6], int Vertices, int Edges, int edge[20][2], int Source) {
    int distance[20], parent[20], flag = 1;


    for (int i = 0; i < Vertices; i++) {
        distance[i] = 999;
        parent[i] = -1;
    }
    distance[Source] = 0;


    for (int i = 0; i < Vertices - 1; i++) {
        for (int k = 0; k < Edges; k++) {
            int u = edge[k][0], v = edge[k][1];
            if (distance[u] + G[u][v] < distance[v]) {
                distance[v] = distance[u] + G[u][v];
                parent[v] = u;
            }
        }
    }


    for (int k = 0; k < Edges; k++) {
        int u = edge[k][0], v = edge[k][1];
        if (distance[u] + G[u][v] < distance[v]) {
            flag = 0;
        }
    }

    printf("Destination\tCost\tPath\n");
    if (flag) {
        for (int i = 0; i < Vertices; i++) {
            if (i == Source) continue;

            printf("%d\t\t", i);
            if (distance[i] == 999) {
                printf("INF\tNo Path\n");
            } else {
                printf("%d\t", distance[i]);


                int nodes[10], b = 9, next = i;
                nodes[b--] = i;
                while (next != -1) {
                    next = parent[next];
                    if (next != -1) {
                        nodes[b--] = next;
                    }
                }


                for (int g = b + 1; g < 10; g++) {
                    if (g == 9) {
                        printf("%d", nodes[g]);
                    } else {
                        printf("%d-->", nodes[g]);
                    }
                }
                printf("\n");
            }
        }
    } else {
        printf("Graph contains a negative weight cycle.\n");
    }

    return flag;
}

int main() {
    int Vertices = 6, edge[20][2], k = 0;
    int graph[6][6] = {
        {0, 1, 5, 0, 0, 0},
        {1, 0, 3, 4, 0, 0},
        {5, 3, 0, 5, 9, 0},
        {0, 4, 5, 0, 2, 6},
        {0, 0, 9, 2, 0, 3},
        {0, 0, 0, 6, 3, 0}
    };

    printf("The Adjacency Matrix representation of the graph:\n");
    for (int i = 0; i < Vertices; i++) {
        for (int j = 0; j < Vertices; j++) {
            printf("%d\t", graph[i][j]);
            if (graph[i][j] != 0) {
                edge[k][0] = i;
                edge[k++][1] = j;
            }
        }
        printf("\n");
    }

    for (int i = 0; i < Vertices; i++) {
        printf("\n----------------------------\n"
               "Source vertex: %d\n", i);
        findShortestPath(graph, Vertices, k, edge, i);
    }

    return EXIT_SUCCESS;
}
    

ARQ

#include 
#include 


void StopAndWait(){
    int n, i=0,frame;
    printf("Enter the total number of frames:");
    scanf("%d",&n);
    while(i!=n){
        printf("\nEnter received frame ");
        scanf("%d",&frame);
        if(frame==i+1){
            printf("Transmitting ..... ACK to frame %d\n",frame);
            i++;
        }else{
            printf("Negative ACK ...to frame %d\n",i+1);
        }
    }
}
void GoBackN(){
    int n,i=0,frame,size,t;
    printf("Enter the total number of frames:");
    scanf("%d",&n);
    printf("Enter window size of frames:");
    scanf("%d",&size);
    printf("Sending frames");
    for (int j=0;j a[j]) {
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }


    printf("\nFinal received frame sequence:\n");
    for (i = 0; i < m; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
}

int main(int argc, char** argv) {
    int val;
    while(1){
        printf("\nEnter\n1.Stop And Wait protocol\n"
                "2.Go back N \n3.Selective repeat \n");
        scanf("%d",&val);
        switch(val){
            case 1: StopAndWait();
            break;
            case 2: GoBackN();
            break;
            case 3: Selective();
            break;
            default:return(0);
        }
    }

    return (EXIT_SUCCESS);
}
    

Chart Box

server.py

import time, socket, sys
print('Setup Server...')
time.sleep(1)
#Get the hostname, IP Address from socket and set Port
soc = socket.socket()
host_name = socket.gethostname()
ip = socket.gethostbyname(host_name)
port = 1234
soc.bind((host_name, port))
print(host_name, '({})'.format(ip))
name = input('Enter name: ')
soc.listen(1) #Try to locate using socket
print('Waiting for incoming connections...')
connection, addr= soc.accept()
print("Received connection from", addr[0], "(" , addr[1], ")\n")
print('Connection Established. Connected From: {}, ({})'.format(addr[0], addr[0]))
#get a connection from client side
client_name = connection.recv(1024)
client_name = client_name.decode()
print(client_name + 'has connected.')
print('Press [bye] to leave the chat room')
connection.send(name.encode())
while True:
	message = input('Me >')
	if message == '[bye]':
		message = 'Bye, leaving the chat room!'
		connection.send(message.encode())
		print("\n")
		break
	connection.send(message.encode())
	message = connection.recv(1024)
	message = message.decode()
	print(client_name, '>', message)
    

client.py

import time, socket, sys
print('client Server...')
time.sleep(1)
#Get the hostname, IP Address from socket and set Port
soc = socket.socket()
shost = socket.gethostname()
ip = socket.gethostbyname (shost)
#get information to connect with the server
print(shost, '({})'.format(ip))
server_host = input('Enter server\'s IP address:')
name = input('Enter Client\'s name: ')
port = 1234
print('Trying to connect to the server: {}, ({})'.format(server_host, port))
time.sleep(1)
soc.connect((server_host, port))
print("Connected...\n")
soc.send(name.encode())
server_name = soc.recv(1024)
server_name = server_name.decode()
print('{}) has joined...'.format(server_name))
print('Enter [bye] to exit.')
while True:
	message = soc.recv(1024)
	message = message.decode()
	print(server_name, ">", message)
	message = input("Me > ")
	if message == "[bye]":
		message = "Leaving the Chat room"
		soc.send(message.encode())
		print("\n")
		break
	soc.send(message.encode())
    

Socket

server.py

import socket

HOST='127.0.0.1'#Standard loopback interface address(localhost)
PORT=65432 #Port to listen on (non-privileged ports are >1023)
with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s:
	s.bind((HOST,PORT))
	s.listen()
	conn,addr=s.accept()
	with conn:
		print("Connected by",addr)
		while True:
			data = conn . recv(1024)
			if not data:
			    break
			conn.sendall(data)
    

client.py

import socket
HOST='127.0.0.1' # The server's hostname or IP address
PORT=65432 # The port used by the server
message=b"Hello World"
with socket.socket(socket.AF_INET,socket.SOCK_STREAM)as s:
	s.connect((HOST,PORT))
	s.send(message)
	data=s.recv(1024)

print("Received ", repr(data))