Coding Practice
Showing posts with label Data Communication. Show all posts
Showing posts with label Data Communication. Show all posts

Write a program to find Hamming Distance

Hamming Distance

Hamming distance is a metric for comparing two binary data strings. While comparing two binary strings of equal length, Hamming distance is the number of bit positions in which the two bits are different.

Example: Find the distance between the vectors 01101010 and 11011011.
(01101010
11011011)
They differ in four places, So the Hamming distance d(01101010, 11011011) = 4.

Sample Output
Enter Comp1 data: 111101010
Enter Comp2 data: 010101010

Hamming Distance: 2
C-Source Code
#include<stdio.h>

void main()
{
    int i, comp1Len, comp2Len, hammingDistance = 0;
    char comp1[500], comp2[500];
input:
    printf("Enter Comp1 data: ");
    scanf("%s", comp1);
    printf("Enter Comp2 data: ");
    scanf("%s", comp2);

    for(i = 0; comp1[i] != '\0'; i++);
    comp1Len = i;

    for(i = 0; comp2[i] != '\0'; i++);
    comp2Len = i;

    if(comp1Len != comp2Len)
    {
        printf("\nComp1 and Comp2 length are not same!\n\n");
        goto input;
    }

    for(i = 0; i < comp1Len; i++)
    {
        if(comp1[i] != comp2[i])
        {
            hammingDistance++;
        }
    }
    printf("\nHamming Distance: %d\n\n", hammingDistance);
}
Sample Output
Enter Comp1 data: 01101010
Enter Comp2 data: 11011011

Hamming Distance: 4

Write a program to calculate the MINIMUM HAMMING DISTANCE for a given set of binary data taken as input

Minimum Hamming Distance
Sample Input:
    Number of inputs: 3
    10110
    01110
    11001
    Sample Output:
    Minimum hamming distance, (C1, C2) = 2
Sample Output
Number of inputs: 3
10110
01110
11001

Minimum hamming distance, (10110, 01110) = 2
C-Source Code
/**
I will share the code after 15 April 2021,
If you need urgent, please contact with me.
*/
Sample Output
Number of inputs: 4
1110
0011
0000
10

Data must be same length!
Enter 4 data, whose length 4 bits...
1110
0011
0000
1011

Minimum hamming distance, (0011, 1011) = 1

Write a program that choose Even parity / Odd parity according to user choose. And accordingly performs Even parity / Odd parity process

Parity Bit Checker

Example 1:
Enter 1 for Even parity and 2 for Odd parity.
Input: 1

Even parity:
Input:
Data: 110110
Output:
modified data: 1101100
Parity bit: 0

Example 2:
Enter 1 for Even parity and 2 for Odd parity.
Input: 2

Odd parity:
Input:
Data: 1101101
Output:
Modified data: 11011010
Parity bit: 0

Sample Output
Enter 1 for Even parity and 2 for Odd parity.

Enter your choice: 1

Even parity:
Data: 110110

Modified Data: 1101100
Parity Bit: 0
C-Source Code
#include<stdio.h>
#include<string.h>

void main()
{
    int i, choice, even_parity_len, count = 0, p_bit;
    char parity_bit, even_parity[100];
main:
    printf("Enter 1 for Even parity and 2 for Odd parity.\n\nEnter your choice: ");
    scanf("%d", &choice);

    switch(choice)
    {
    case 1:
        printf("\nEven parity:\nData: ");
        scanf("%s", even_parity);

        for(i = 0; even_parity[i] != '\0'; i++);
        even_parity_len = i;

        for(i = 0; i < even_parity[i]; i++)
        {
            if(even_parity[i] == '1')
            {
                count++;
            }
        }

        if(count%2 == 0)
        {
            even_parity[even_parity_len] = '0';
            p_bit = 0;
        }
        else
        {
            even_parity[even_parity_len] = '1';
            p_bit = 1;
        }
        break;

    case 2:
        printf("\nOdd parity:\nData: ");
        scanf("%s", even_parity);

        for(i = 0; even_parity[i] != '\0'; i++);
        even_parity_len = i;

        for(i = 0; i < even_parity[i]; i++)
        {
            if(even_parity[i] == '1')
            {
                count++;
            }
        }

        if(count%2 == 0)
        {
            even_parity[even_parity_len] = '1';
            p_bit = 1;
        }
        else
        {
            even_parity[even_parity_len] = '0';
            p_bit = 0;
        }
        break;

    default:
        printf("\nInvalid Input! Please enter 1 or 2.\n");
        goto main;
        break;
    }
    even_parity[even_parity_len+1] = '\0';

    printf("\nModified Data: %s\nParity Bit: %d\n\n", even_parity, p_bit);
}
Sample Output
Enter 1 for Even parity and 2 for Odd parity.

Enter your choice: 2

Odd parity:
Data: 1101101

Modified Data: 11011010
Parity Bit: 0

Write a program that construct an Even parity Hamming code

Even parity Hamming code

Example-1:
Input: 10111010100101
Output: 
Mod data: 1111011110101000101
Number of parity bits: 5

Example-2:
Input: 101100
Output: 
Mod data: 0110011000
Number of parity bits: 4

Table Even Parity Hamming Code

Before add parity bit, data will generate like this: 0X1X011X1010100X101

P1 = [3, 5, 7, 9, 11, 13, 15, 17, 19] = 101111011
= X1 = 1 {Odd parity}
P2 = [3, 6, 7, 10, 11, 14, 15, 18, 19] = 111010001
= X2 = 1 {Odd parity}
P4 = [5, 6, 7, 12, 13, 14, 15] = 0110100
= X4 = 1 {Odd parity}
P8 = [9, 10, 11, 12, 13, 14, 15] = 1010100
= X8 = 1 {Odd parity}
P16 = [17, 18, 19] = 101
= X16 = 0 {Even parity}

So, Modified data = 1111011110101000101

Sample Output
Enter data: 101100

Mod data: 0110011000
Number of parity bits: 4
Source Code
#include<stdio.h>
#include<math.h>

void main()
{
    int i, j, k, l, m, n, x, p_bit[50], p_bit_count = -1;
    int count, dataLen, value = 0, temp_even_parity_len;
    char data[500], temp_data[500], modData[500], temp_even_parity[100];

    printf("Enter data: ");
    scanf("%s", data);

    for(i = 0; data[i] != '\0'; i++);
    dataLen = i;

    for(i = 0; value <= (dataLen + i + 1); i++)
    {
        value = pow(2, i);
        p_bit[i] = value;
        p_bit_count++;
    }

    i = 0;
    j = 0;
    k = 0;
    while(i < (dataLen + p_bit_count))
    {
        if(p_bit[k] == i+1)
        {
            modData[i] = 'X';
            k++;
        }
        else
        {
            modData[i] = data[j];
            j++;
        }
        i++;
    }
    modData[i] = '\0';

    for(k = 0; k < p_bit_count; k++)
    {
        l = 0;
        for(m = p_bit[k]; m <= dataLen + p_bit_count;)
        {
            for(n = 0; n < p_bit[k] && m <= dataLen + p_bit_count; n++)
            {
                if(m == p_bit[k])
                {
                    m++;
                }
                else
                {
                    temp_even_parity[l++] = modData[m-1];
                    m++;
                }
            }
            for(n = 0; n < p_bit[k] && m <= dataLen + p_bit_count; n++)
            {
                m++;
            }
        }
        temp_even_parity[l] ='\0';

        for(i = 0; temp_even_parity[i] !='\0'; i++);
        temp_even_parity_len = i;

        for(i = 0, count = 0; i < temp_even_parity_len; i++)
        {
            if(temp_even_parity[i] == '1')
            {
                count++;
            }
        }

        if(count % 2 == 0)
        {
            x = (p_bit[k]-1);
            modData[x] = '0';
        }
        else
        {
            x = (p_bit[k]-1);
            modData[x] = '1';
        }
    }
    printf("\nMod data: %s\n", modData);
    printf("Number of parity bits: %d\n\n", p_bit_count);
}
Sample Output
Enter data: 10111010100101

Mod data: 1111011110101000101
Number of parity bits: 5

Data Communication Problem Solution

1. Write a C program that takes a classless IP address as input and displays it in binary, the number of network and host address depending on number represented after the backslash

Sample Input: 10.15.15.1/24
Sample Output:
IP in binary: 00001010.00001111.00001111.00000001
Network Address: 16777214
Hosts Address: 254

2. Write a C program that takes a classful IP address as input and displays it in binary, identifies its class from the first binary octet and the number of network and host address depending on the class

Sample Input:10.15.15.1
Sample Output:
IP in binary: 00001010.00001111.00001111.00000001
Class: A
Network Address: 254
Hosts Address: 16777214

3. Write a C/JAVA program to implement CRC. The program will take a sequence of binary data bits, and a divider as input. And gives the resultant data with CRC as output. (Where, divider < binary data bits)

Sample Input:
Data: 10110
Divider: 1101

Sample Output:
Modified data: 10110000
Data Send: 10110101
CRC bit: 101

4. Write a program that choose Even parity / Odd parity according to user choose. And accordingly performs Even parity / Odd parity process

Example 1:
Enter 1 for Even parity and 2 for Odd parity.
Input: 1

Even parity:
Input:
Data: 110110
Output:
modified data: 1101100
Parity bit: 0

Example 2:
Enter 1 for Even parity and 2 for Odd parity.
Input: 2

Odd parity:
Input:
Data: 1101101
Output:
Modified data: 11011010
Parity bit: 0

5. Write a program that construct an Even parity Hamming code

Example-1:
Input: 10111010100101
Output: 
Mod data: 1111011110101000101
Number of parity bits: 5

Example-2:
Input: 101100
Output: 
Mod data: 0110011000
Number of parity bits: 4

6. Write a program to calculate the MINIMUM HAMMING DISTANCE for a given set of binary data taken as input. The first line of input is an integer, i (0<i<10) which mentions the number of inputs to be taken

Sample Input:
    Number of inputs: 3
    10110
    01110
    11001
    Sample Output:
    Minimum hamming distance, (C1, C2) = 2

7. Write a program to find Hamming Distance

Write a C program to convert IP address to 32-bit binary format

Convert IP Address to 32-bit Binary Format

1. Write a C program that takes a classless IP address as input and displays it in binary, the number of network and host address depending on number represented after the backslash

Classless is called Classless Inter-Domain Routing. It was introduced in 1993 to replace classfull addressing and it allows the user to use Variable Length Subnet Makss(VLSM). Class-less subnet masks are denoted by /X. X is the subnet mask. For example 192.168.0.1/24.
[Note: Input must be like this XXX.XXX.XXX.XXX/XX
Example: 192.168.0.1/24]
Network Address: (224-2)
Host Address: (28-2)

Sample Output
Enter IPv4: 192.168.0.1/24
IP in binary: 11000000.10101000.00000000.00000001
Network address: (2^24)-2 = 16777214
Host Address: (2^8)-2 = 254


Process returned 29 (0x1D)   execution time : 19.943 s
Press any key to continue.
Source Code
#include<stdio.h>
#include<math.h>

void decToBin(int number)
{
    int i, bin[8];

    for(i = 0; number > 0; i++)
    {
        bin[i] = number % 2;
        number = number/2;
    }
    for(i = i-1; i >= 0; i--)
    {
        number = (10 * number) + bin[i];
    }
    printf("%0.8d", number);
}

void main()
{
    int i, count = 0, ipv4Len, number = 0, digit, netMask = 0, inputLen;
    long netAddress, hostAddress;
    char ip[30];

    printf("Enter IPv4: ");
    scanf("%s", ip);

    for(i = 0; ip[i] != 47; i++);
    ipv4Len = i;
    for(i = ipv4Len+1; ip[i] != '\0'; i++);
    inputLen = i;

    printf("IP in binary: ");
    for(i = 0; i < ipv4Len; i++)
    {
        if(ip[i] == '.')
        {
            decToBin(number);
            number = 0;
            printf(".");
        }
        else
        {
            digit = ip[i] - '0';
            number = (10 * number) + digit;
        }
    }
    decToBin(number);

    for(i = ipv4Len+1; i < inputLen; i++)
    {
        //ch = '8' = 56, '0' = 48, digit = ch - '0'
        digit = ip[i] - '0';
        netMask = (10 * netMask) + digit;
    }
    netAddress = pow(2, netMask);
    hostAddress = pow(2, (32-netMask));

    printf("\nNetwork address: (2^%d)-2 = %ld\n", netMask, (netAddress-2));
    printf("Host Address: (2^%d)-2 = %ld\n\n", (32-netMask), (hostAddress-2));
}
Sample Output
Enter IPv4: 103.237.76.98/16
IP in binary: 01100111.11101101.01001100.01100010
Network address: (2^16)-2 = 65534
Host Address: (2^16)-2 = 65534


Process returned 32 (0x20)   execution time : 43.430 s
Press any key to continue.
Change Theme
X