Question

1. Given a binary code, determine the number of errors that it can detect and the...

1. Given a binary code, determine the number of errors that it can detect and the number of errors that it can correct. WRITE A PROGRAM PYTHON or JAVA. both would be nice

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package Networking;

import java.util.*;

public class ErrorDectCorrect

{

static int setParityBit(int data[])

{

// Initializing count to zero which will count the number of 1.

int counter = 0;  

// Loops till data length

for(int c = 0; c < data.length; ++c)

// Increase count if value in array "data" is 1.

if(data[c] == 1)

++counter;

// Returns 0 if counter value is even number (even number of 1s)

if((counter % 2) == 0)

return 0;

// Otherwise returns 1 for odd number of 1

else

return 1;

}// End of method

// main method definition

public static void main(String args[])

{

// Scanner class object created

Scanner keyboard = new Scanner(System.in);

System.out.print("\n Error detection and correction using EVEN parity");

// Sets number of data bits

int numberOfDataBits = 4;

// Accepts 4 bits of data

System.out.print("\n\n Enter 4 data bits: ");

// Declares an array to accept data bits

int dataBits[] = new int[4];

for(int pos = numberOfDataBits - 1; pos >= 0; --pos)

{

System.out.print("Enter the value of D" + (pos + 1) + " -> ");

dataBits[pos]=keyboard.nextInt();

}// End of for loop

// Formula (2 ^ numberOfParityBits >= numberOfDataBits + numberOfParityBits + 1)

// Initializing it to zero.

int numberOfParityBits = 0;

// Calculating the value of k(number of parity bits).

while(Math.pow(2, numberOfParityBits) < (numberOfDataBits + numberOfParityBits + 1))

++numberOfParityBits;

System.out.println("\n" + numberOfParityBits +

" parity bits are required for the " +

"transmission of data bits.");

// Declares an array to store parity bits

int parityBits[] = new int[numberOfParityBits];

// Array to store the hamming code.

// (n + numberOfParityBits + 1) as we start from pos 1.

int hammingCode[] = new int[numberOfDataBits + numberOfParityBits + 1];

// Initialize each hammingCode array element to -1

for(int pos = 0; pos < 7; ++pos)

hammingCode[pos] = -1;

int count = 0;

int c = 2;

while(count < 4)

{

++c;

if(c == 4)

continue;

hammingCode[c] = dataBits[count];

++count;

}// End of while loop

int p1[] = {hammingCode[1], hammingCode[3], hammingCode[5], hammingCode[7]};

int p2[] = {hammingCode[2], hammingCode[3], hammingCode[6], hammingCode[7]};

int p3[] = {hammingCode[4], hammingCode[5], hammingCode[6], hammingCode[7]};

int parity[] = new int[3];

// Calls the method to set the value of parity bit

parity[0]= setParityBit(p1);

parity[1]= setParityBit(p2);

parity[2]= setParityBit(p3);

// Inserting the parity bits in the hamming code

hammingCode[1] = parity[0];

hammingCode[2] = parity[1];

hammingCode[4] = parity[2];

System.out.println("\n\n SENDER SIDE:");

System.out.print("\n The data bits entered are: ");

for(int pos = 3; pos >= 0; --pos)

System.out.print(dataBits[pos] +" ");

System.out.println("\n\n The Parity bits are: ");

for(int pos = 2; pos >= 0; --pos)

System.out.println("Value of P " + (pos + 1) + " is " + parity[pos] + " ");

System.out.print("\n\n The Hamming code is as follows :-" +

"\nD4 D3 D2 P3 D1 P2 P1 \n");

for(int pos = (numberOfDataBits + numberOfParityBits); pos > 0; --pos)

System.out.print(hammingCode[pos] + " ");

System.out.println();

System.out.println("\n Enter the hamming code with error at " +

"any position of your choice." +

"\nNOTE: ENTER A SPACE AFTER EVERY BIT POSITION." +

"\nError should be present only at one bit position");

for(int pos = 7; pos > 0; --pos)

hammingCode[pos] = keyboard.nextInt();

int p4[]={hammingCode[1], hammingCode[3], hammingCode[5], hammingCode[7]};

int p5[]={hammingCode[2], hammingCode[3], hammingCode[6], hammingCode[7]};

int p6[]={hammingCode[4], hammingCode[5], hammingCode[6], hammingCode[7]};

parity[0]= setParityBit(p4);

parity[1]= setParityBit(p5);

parity[2]= setParityBit(p6);

int position = (int)(parity[2] * Math.pow(2,2) +

parity[1] * Math.pow(2,1)+

parity[0] * Math.pow(2,0));

System.out.print("\n\n RECEIVER:");

System.out.print("\n Error is detected at position " + position +

" at the receiving end.");

System.out.print("\n Correcting the error.... ");

if(hammingCode[position] == 1)

hammingCode[position]=0;

else

hammingCode[position] = 1;

System.out.print("\n The correct code is ");

for(int pos = 7; pos > 0; --pos)

System.out.print(hammingCode[pos] + " ");

}// End of main method

}// End of class

Sample Output:


Error detection and correction using EVEN parity

Enter 4 data bits: Enter the value of D4 -> 1
Enter the value of D3 -> 0
Enter the value of D2 -> 1
Enter the value of D1 -> 0

3 parity bits are required for the transmission of data bits.


SENDER SIDE:

The data bits entered are: 1 0 1 0

The Parity bits are:
Value of P 3 is 0
Value of P 2 is 1
Value of P 1 is 0


The Hamming code is as follows :-
D4 D3 D2 P3 D1 P2 P1
1 0 1 0 0 1 0

Enter the hamming code with error at any position of your choice.
NOTE: ENTER A SPACE AFTER EVERY BIT POSITION.
Error should be present only at one bit position
1 0 1 1 0 0 1


RECEIVER:
Error is detected at position 7 at the receiving end.
Correcting the error....
The correct code is 0 0 1 1 0 0 1

Add a comment
Know the answer?
Add Answer to:
1. Given a binary code, determine the number of errors that it can detect and the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Given two binary tree, can you determine if they are identical or not? Write a program...

    Given two binary tree, can you determine if they are identical or not? Write a program for that. Algorithm: a) If both trees are NULL then return true b) If both trees are not NULL then, return TRUE if they are identical. In java.

  • 1. (30 points) Consider the systematic binary linear (6,3) code with generator matrix 1 0 01...

    1. (30 points) Consider the systematic binary linear (6,3) code with generator matrix 1 0 01 1 0 G- 0 1 0 0 1 1 a) Determine the parity check matrix H of the code. b) What is the minimum distance of the code? How many errors can this code correct and detect? c) Show the results in b) using decoding table d) Find the most likely codeword, given that the noisy received codeword is 010101. e) Now suppose 001101...

  • In C:Write a program that can determine whether a user input is a binary number or...

    In C:Write a program that can determine whether a user input is a binary number or not. (1, 2, 3, 8, 13) Write a program that accomplishes all of the following: Greets the user and informs them that the program will determine whether an input from the user is a valid binary number or not (e.g., consisting of only 0’s and/or 1’s). Accept an integer input from the user. Assume that the user provides a valid numeric input that will...

  • Assignment Modify the code Binary File IO Example Code. The code reads and prints information abo...

    In java Assignment Modify the code Binary File IO Example Code. The code reads and prints information about a BMP file. Modify the code so that it also prints the resolution of the BMP file (both width and height) and the number of bits per pixel. Do not use any Java library or third-party library code that reads BMP files. You must read the file using simple Java code and extract the information, similar to the code in the example....

  • JAVA: Write a program that inputs a string that represents a binary number. The string can...

    JAVA: Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console.

  • CODE IN JAVA** V. Given a pointer to the root of a binary tree and a...

    CODE IN JAVA** V. Given a pointer to the root of a binary tree and a pointer ‘p’ to a given node in the tree and a second pointer ‘q’ to another node in the tree write a routine which will return the total number of nodes in the tree that are on level ‘p’ and ‘q’. If they are on the same level you should multiply the answer by 3.

  • How would I write code in Java, where the user inputs a binary number and the...

    How would I write code in Java, where the user inputs a binary number and the system reads the input and outputs if there was an even or odd number of 1s typed. For example, if I typed 10101 it should output as odd. and if I typed 11011 it should output as even.

  • Write a python code to determine the factors of a positive number N entered by the...

    Write a python code to determine the factors of a positive number N entered by the user and their sum. For instance, if N=10 its factors are: 1, 2, 5, 10. Hint: You can use the mod or % operator to compute the remainder of an integer division. Test the program with N=100.

  • A magnetic tape storing information in binary form has been corrupted, so it can only be...

    A magnetic tape storing information in binary form has been corrupted, so it can only be read with bit errors. The probability that you correctly detect a 0 is 0.9, while the probability that you correctly detect a 1 is 0.85. Each digit is a 1 or a 0 with equal probability. Given that you read a 1, what is the probability that this is a correct reading?

  • 3. A magnetic tape storing information in binary form has been corrupted, so it can only...

    3. A magnetic tape storing information in binary form has been corrupted, so it can only be read with bit errors. The probability that you correctly detect a 0 is 0.9, while the probability that you correctly detect a 1 is 0.85. Each digit is a 1 or a 0 with equal probability. Given that you read a 1, what is the probability that this is a correct reading?

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT