Description
Description
You are working with a friend on a communication protocol. The protocol is very simple: you have a 16-bit bus that sends 16-bits every clock tick. You have found problems occur if too many of the bits are set or unset.
To find out if this will create a problem you have trace files with 16-bit binary samples of the bus. You will check the number of set bits for each consecutive pair of samples in a file and output the lowest and highest percentage that you see. The percentages you output will be rounded to the nearest whole number using round() from math.h. Each sample will be encoded as a binary number that may have leading zeros.
here is an example file, sample.dat:
0000000011111111
1010101010101010
101010101010101
0000000000000000
1111111111111111
For the first pair of numbers: 0000000011111111
and 1010101010101010
has 50% set bits. The next pair 1010101010101010
and 101010101010101
has 50% set bits. The third pair 101010101010101
and 0000000000000000
has 25% set bits. The final pair 0000000000000000
and 1111111111111111
has 50% set bits. So, the program will output:
25%
50%
since 25% was the lowest number of set bits and 50% was the highest.
If you encounter any errors, you will stop, print and error with the invalid line, and exit with a code of 2.
TO COUNT THE NUMBER OF SET BITS, YOU MUST USE STRTOL TO CONVERT THE STRING TO A NUMBER AND COUNT THE BITS. DO NOT COUNT THE NUMBER OF 1 CHARACTERS.
If sample1.dat is
1
hello
101010101
110011111
running ./biased_bits with that file should look like:
$ ./biased_bits sample1.dat
invalid: hello
$ echo $?
2
Too many bits, even if it is a zero bit, is also invalid, for example, sample2.dat:
10101010
000000000000111111
1
would produce:
$ ./biased_bits sample2.dat
invalid: 000000000000111111
$ echo $?
2
If you get passed an file that you cannot open for reading, use perror
to indicate the problem with the file, passing the filename as the parameter to perror
, and exit with a code of 2.
$ ./biased_bits bad_file.dat
bad_file.dat: No such file or directory
$ echo $?
2
If you get a file that doesn’t have enough samples to collect a pair, print the following error and exit with a code of 2.
For example, the file short.dat
:
101
would produce
$ ./biased_bits short.dat
not enough samples
$ echo $?
2
Required Functions
When implementing your solution, you must use strtol
from #include <stdlib.h>
to convert strings to numbers, printf
from #include <stdio.h>
to generate output, exit
to generate an exit code, round
from #include <math.h>
to implement rounding, fopen
to open the sample file, fclose
to close it, getline
to read the lines, and free
to free allocated memory.
Examples
Command to RUN: ./runvalgrind.sh ./biased_bits
Expected Exit Code: 1
USAGE: ./biased_bits sample_file
Command to RUN: ./runvalgrind.sh ./biased_bits short.dat
Expected Exit Code: 2
not enough samples
Command to RUN: ./runvalgrind.sh ./biased_bits sample.dat
Expected Exit Code: 0
25%
50%
Command to RUN: ./runvalgrind.sh ./biased_bits short.dat
Expected Exit Code: 2
not enough samples