Your program will take a random number seed as a command line argument. You will
ID: 3801745 • Letter: Y
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 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
Explanation / Answer
Given below is the program for the question. Output is also attached. Please rate the answer if it helped. Thank you.
You will need to pass a command line argument i..e a seed value for random number generation.
#include <stdio.h>
#include <stdlib.h>
#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));
void PrintByte(unsigned char x)
{
printf("%d->", x);
PRINTBIN(x);
printf(" ");
}
void PackArray(unsigned char *packed, size_t packedSize, unsigned char *unpacked, size_t unpackedSize)
{
int i = 0, j = 0, BYTELEN = 8, MAXBIT = 5, EXTRACTLEN = 8;
int totalBits, start = 0, end;
unsigned char currentBits, packedBits;
int leftShift , rightShift;
for(i = 0; j < unpackedSize && i < packedSize; i++)
{
totalBits = 0;
packedBits = 0;
while(totalBits < EXTRACTLEN)
{
end = start + EXTRACTLEN - totalBits - 1;
if(end > MAXBIT)
end = MAXBIT;
leftShift = BYTELEN - end - 1;
rightShift = leftShift + start;
currentBits = (unpacked[j] << leftShift);
currentBits = currentBits >> rightShift;
packedBits |= currentBits << totalBits;
totalBits += end - start + 1;
if(end == MAXBIT)
{
j++;
if(j >= unpackedSize)
break;
start = 0;
}
else
start = end + 1;
}
packed[i] = packedBits;
}
}
void UnpackArray(unsigned char *unpacked, size_t unpackedSize, unsigned char *packed, size_t packedSize)
{
int i = 0, j = 0, BYTELEN = 8, MAXBIT = 7, EXTRACTLEN = 6;
int totalBits, start = 0, end;
unsigned char currentBits, unpackedBits;
int leftShift , rightShift;
for(i = 0; i < unpackedSize && j < packedSize; i++)
{
totalBits = 0;
unpackedBits = 0;
while(totalBits < EXTRACTLEN)
{
end = start + EXTRACTLEN - totalBits - 1;
if(end > MAXBIT)
end = MAXBIT;
leftShift = BYTELEN - end - 1;
rightShift = leftShift + start;
currentBits = (packed[j] << leftShift);
currentBits = currentBits >> rightShift;
unpackedBits |= currentBits << totalBits;
totalBits += end - start + 1;
if(end == MAXBIT)
{
j++;
if(j >= packedSize)
break;
start = 0;
}
else
start = end + 1;
}
unpacked[i] = unpackedBits;
}
};
int main(int argc, char *argv[])
{
int seed;
int i ;
unsigned char origArray[16], packedArray[12], newArray[16];
if(argc != 2)
{
printf(" usage: %s <seed_number>", argv[0]);
return 1;
}
seed = atoi(argv[1]);
srand(seed);
printf(" The original array: ");
//generate 16 random values
for(i = 0; i < 16; i++)
{
origArray[i] = rand() % 64;
PrintByte(origArray[i]);
}
PackArray(packedArray, 12, origArray, 16);
printf(" The packed array: ");
for(i = 0; i < 12; i++)
{
PrintByte(packedArray[i]);
}
UnpackArray(newArray, 16, packedArray, 12);
printf(" The New (unpacked) array: ");
for(i = 0; i < 16; i++)
{
PrintByte(newArray[i]);
}
}
output
The original array:
17->00010001
25->00011001
54->00110110
20->00010100
62->00111110
8->00001000
2->00000010
28->00011100
14->00001110
16->00010000
48->00110000
53->00110101
7->00000111
25->00011001
29->00011101
55->00110111
The packed array:
81->01010001
102->01100110
83->01010011
62->00111110
34->00100010
112->01110000
14->00001110
4->00000100
215->11010111
71->01000111
214->11010110
221->11011101
The New (unpacked) array:
17->00010001
25->00011001
54->00110110
20->00010100
62->00111110
8->00001000
2->00000010
28->00011100
14->00001110
16->00010000
48->00110000
53->00110101
7->00000111
25->00011001
29->00011101
55->00110111
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.