Coding Practice

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

No comments:

Post a Comment

Change Theme
X