Question: Your program will take a random number seed as a command line argument. You will convert this argument to a decimal number and use it
Your program will take a random number seed as a command line argument. You will convert this argument to a decimal number and use it as the parameter for the srandom() function. You will then generate 16 random numbers in the range of 0..63 and store them in an array of unsigned char (origArray). The 16 bytes in this array will be packed into an array of 12 bytes, saving 4 bytes of space (packedArray). This packed array will be printed, and then the process will be reversed, unpacking the bytes into a new array of unsigned char (newArray). Your program will use the following functions to perform its tasks:
void PrintByte(unsigned char x);
This function will print the decimal value of x, as well as the binary representation of that value (see the Macros section below).
void PackArray(unsigned char *packed, size_t packedSize, unsigned char *unpacked, size_t unpackedSize);
This function will take each byte from the array named unpacked, and pack them into the array named packed.
void UnpackArray(unsigned char *unpacked, size_t unpackedSize, unsigned char *packed, size_t packedSize);
This function will unpack the packed bits from the array named packed into individual bytes, and store each byte in the array named unpacked.
Once the original array has been packed and unpacked into the new array, use a loop to run a byte-to-byte comparison of the values in each array. If any values differ, print an error message to the screen. The packing of the bits for each unpacked byte will follow the specifications for Project 2 (to be posted soon). In other words, given the following four bytes in a bit representation:
A -> A7A6A5A4A3A2A1A0
B -> B7B6B5B4B3B2B1B0
C -> C7C6C5C4C3C2C1C0
D -> D7D6D5D4D3D2D1D0
These bytes will be packed into three bytes in the following manner:
packedArray[0] -> B1B0A5A4A3A2A1A0
packedArray[1] -> C3C2C1C0B5B4B3B2
packedArray[2] -> D5D4D3D2D1D0C5C4
Macros:
You may use the following macros to print the binary representation of unsigned character variables: #define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d" #define BYTETOBINARY(byte) \ (byte & 0x80 ? 1 : 0), \ (byte & 0x40 ? 1 : 0), \ (byte & 0x20 ? 1 : 0), \ (byte & 0x10 ? 1 : 0), \ (byte & 0x08 ? 1 : 0), \ (byte & 0x04 ? 1 : 0), \ (byte & 0x02 ? 1 : 0), \ (byte & 0x01 ? 1 : 0) #define PRINTBIN(x) printf(BYTETOBINARYPATTERN, BYTETOBINARY(x)); You can use the macros in a manner similar to the code below: unsigned char num = 48; PRINTBIN(num); You may find it useful to declare other macros such as the size of the packed and unpacked arrays.
Sample Run:
Your output should be similar to the following (and the values will match exactly) if you use a random number seed of 34567: bash$ ./Lab7 34567 The Original Array 26 -> 00011010 56 -> 00111000 61 -> 00111101 30 -> 00011110 39 -> 00100111 57 -> 00111001 20 -> 00010100 60 -> 00111100 52 -> 00110100 18 -> 00010010 0 -> 00000000 18 -> 00010010 20 -> 00010100 10 -> 00001010 0 -> 00000000 38 -> 00100110 The Packed Array 26 -> 00011010 222 -> 11011110 123 -> 01111011 103 -> 01100111 78 -> 01001110 241 -> 11110001 180 -> 10110100 4 -> 00000100 72 -> 01001000 148 -> 10010100 2 -> 00000010 152 -> 10011000 The New (unpacked) Array 26 -> 00011010 56 -> 00111000 61 -> 00111101 30 -> 00011110 39 -> 00100111 57 -> 00111001 20 -> 00010100 60 -> 00111100 52 -> 00110100 18 -> 00010010 0 -> 00000000 18 -> 00010010 20 -> 00010100 10 -> 00001010 0 -> 00000000 38 -> 00100110
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
