Lab 1b: Data Representation in C
Overview
Learning Objectives:
- Extract useful information from data using bitmasks and bitwise operators.
- Recognize differences in C data types and use C casting.
- Explain the relationship between pointers and arrays in C and use it to access and manipulate data.
You will interact with a custom data representation for keeping track of things in a store, implementing functions using bitwise operators, pointers, and arrays.
Code for this lab
wget https://courses.cs.washington.edu/courses/cse351/24sp/labs/lab1b.tar.gz
tar xzvf lab1b.tar.gz
from the terminal will
extract the lab files to a directory called lab1b
.
Lab 1b Instructions
Lab Format
This lab uses a custom data representation of aisles in a store.
aisle_manager.c
and store_client.c
have
skeleton functions for you to implement, each with a comment
describing its desired behavior.
You will start by implementing functions that use bitwise operators, bitmasks, and C control structures to manage an individual aisle. After implementing these functions, you will move onto functions that use pointers, arrays, and your aisle functions to manage multiple aisles in a store. We have provided two test programs to help you check the correctness of your work (see for more details).
Style
This isn't a programming course and we generally won't be checking your style. However, for this lab we will do a quick check for the following items, with the intent of helping you avoid introducing unnecessary bugs. This will be worth just 1 point and is intended to be relatively stress-free.
- Magic numbers: You should not hard-code constants in your code (exceptions: 0, 1) when you can define and (re)use macros to give them meaningful names and consistent values.
- Function usage:
You should use defined functions when appropriate, especially the
get_*
andset_*
functions.
Aisle Manager
Start by reading the description of an aisle's data representation
at the top of aisle_manager.c
.
Make sure you fully understand the layout of the
representation before continuing.
If you have any questions, please post on the message board or come
to office hours.
Once you understand the data representation, you can begin to
implement the skeleton functions in aisle_manager.c
:
get_section
get_spaces
get_id
set_section
set_spaces
set_id
toggle_space
num_items
add_items
remove_items
rotate_items_left
rotate_items_right
Do not be discouraged by the number of functions! They build gradually in level of difficulty, and many of them are similar to each other. Here's one recommended way of working through the file:
- Start by filling in the macros at the top of the file with various bitmasks to be used later.
- Continue by implementing the
get_*
andset_*
functions. - Finish by implementing the rest of the functions related to manipulating the items in an aisle.
At this point you should be able to pass all tests in the
aisle_test
executable!
Now you can move onto store_client.c
.
Store Client
store_client.c
has some functions for interacting with
a group of aisles and a stockroom in the store.
Pay particular attention to its use of global arrays:
(1) of type unsigned long
to represent multiple aisles
and (2) of type int
to represent a stockroom for the
store (items stowed away that are not in the aisles).
Skeleton functions:
refill_from_stockroom
fulfill_order
empty_section_with_id
section_with_most_items
At this point you should be able to pass all tests in the
store_test
executable!
This means you are done with the programming part of the assignment
(don't forget about the synthesis questions!).
Checking Your Work
We have included the following tools to help you check the correctness of your work:
-
The functions
print_binary_long
andprint_binary_short
take anunsigned long
andunsigned short
, respectively, and output its binary representation. These are optional to use but can be useful in debugging your code. All calls to these functions should be removed from your final submission. Here is an example usage ofprint_binary_long
(print_binary_short
works similarly except it takes anunsigned short
as its argument):void print_binary_example(unsigned long* argument) { // Print out binary view of argument for debugging. // The type of argument is unsigned long*, so we // must pass its dereference to print_binary_long. print_binary_long(*argument); }
-
You can also use
printf
to debug your functions, but you'll have to add#include <stdio.h>
at the top of your file. All calls toprintf
should be removed from your final submission. -
aisle_test
andstore_test
are programs that check the functional correctness of the code inaisle_manager.c
andstore_client.c
, respectively.You will need to rebuild these executables before running them each time you modify your code using themake
command.To test all of the functions, use the following commands:
$ ./aisle_test $ ./store_test
The test one function at a time, use the
-f
flag on the appropriate executable, e.g.:$ ./aisle_test -f set_id $ ./store_test -f section_with_most_items
-
aisle_test
andstore_test
first test your solution on specific cases, and then on a wide range of inputs. As a manner of limiting the test output, for the range of inputs the testing program will only print out the first input that your solutions are incorrect for (or no input if you pass all cases). - There is some partial credit awarded if your function passes the specific cases but not the range of cases, and each function is tested separately to allow for the opportunity for partial credit should you only complete some of the functions.
-
Lab 1b Synthesis Questions
lab1Bsynthesis.txt
!
- Data representation: Your friend suggests an alternative
data representation for sections in an aisle.
In this representation, the lowest 8 bits would use unsigned
integer encoding to represent the number of items in a section, and
the upper 8 bits would be used to store a unique item id. [4 pt]
- Compared to our current representation, give two benefits and one drawback of this new representation.
- If you were the store manager, which representation would you prefer to use? Briefly justify your choice.
- Number representation: Consider the
following two statements (noting that the type of
y
is purposefully omitted):y = -1;
y = 0xFFFFFFFF;
- Floating point: Explain why comparing two floating point numbers with == or != is problematic. The suggested equality alternative from lecture involved comparing the difference against a threshold value: what should be considered in choosing such a threshold? [3 pt]
Submission
You will submit:
aisle_manager.c
,
store_client.c
,
and
lab1Bsynthesis.txt
.
Be sure to run make clean
and then make
on your code before submitting!
Submissions that don't compile will automatically receive a score
of ZERO.
After submitting, please wait until the autograder is done running and double-check that you passed the "File Check" and "Compilation and Execution Issues" tests. If either test returns a score of -1, be sure to read the output and fix any problems before resubmitting. Failure to do so will result in a programming score of ZERO for the lab.
Submit your files to the "Lab 1b" assignment on . Don't forget to add your partner, if you have one.