Due by 11:59 pm, Thursday, February 8, 2024
1. Lab Goals
-
Explore the binary data representation of different types:
int
,unsigned
,float
, andchar
. -
Convert between decimal, binary, and hexadecimal representations.
-
Apply arithmetic operations to binary numbers.
-
Manipulate binary data with C bit-wise operators.
-
Practice with C programming and the
gdb
debugger.
2. Lab Description
This lab assignment includes a programming exercise and will be submitted electronically by Thursday 12:59pm using git
.
3. Getting Lab 2 Starting Point Code
Like the previous lab, we’ll use git
to distribute the starting code for the
lab. As a reminder, for each lab assignment you will do the following:
-
Get the ssh-URL to your Lab git repository from the GitHub server for our class: CS31 GitHub organization.
-
Using your URL, and from your cs31/labs/ subdirectory, run git clone to clone a local copy of your repo:
$ cd ~/cs31/labs/ $ git clone
-
cd
into your new lab repo directory and work on the lab assignment.
More details are available in the setup for CS31 lab assignment instructions.
4. C Programming
You will write a single C program that, when run, prints out answers to each of the questions below. For each question, print out a string that is your answer to the question, and then print out some expressions and their results that support your answer to the question. For example, the beginning of a run of your program might look like this:
$ ./lab2
Question 1: my answer to question 1 is ...
This can be verified by examining the result of the expression ...
when x is the int value ... and y is ... the expression is ...
when x is ... and y is ... the expression is ...
Question 2: my answer to question 2 is ...
This can be verified by ...
Each answer should include printing out the result(s) of computation(s) that demonstrates your answer’s correctness. DO NOT just print something like this:
Instead, have C code that computes the answer to show or to prove that your answer is correct:
|
For some questions, the code proving your answer correct may be as simple as the example above. For others, however, you will have to think about how to construct some arithmetic expressions that demonstrate the correctness of your answer.
Answer these questions by writing a C program that prints out the answer and prints out example expression(s) that support your answer:
-
Given the following variable declaration and initialization, show how you can use 3 different format strings in
printf
to display the value ofval
in different ways.Hint: If you aren’t sure how to proceed, try taking a look at the list of format specifiers in the textbook.
int val; val = 97;
-
What is the maximum value that can be stored in a C unsigned int variable (unsigned)? Demonstrate this through code.
-
What is the maximum positive value that can be stored in a C int variable (signed)? Demonstrate this through code.
-
What arithmetic operation is equivalent to left shifting an unsigned int value by 1? Demonstrate this through code.
-
What arithmetic operation is equivalent to left shifting an unsigned int value by 2? Demonstrate this through code.
-
What arithmetic operation is equivalent to right shifting an unsigned int value by 1? Demonstrate this through code.
5. Requirements
-
Your answers should be implemented in the
lab2.c
program, and when compiled and run, should output answers. -
TThe answer to each question should be implemented as a separate function called by
main
. For example, your code andmain
might look like:void question1(void); void question2(void); void question3(void); .... int main() { question1(); // call the question1 function question2(); ... } .. void question1(void){ .... //example print statement (not the actual statement you would right) printf("The sum of %d and %d is %d", x, y, x+y); }
You are welcome to add additional helper functions!
-
Each answer should contain necessary and sufficient examples to support it. Do not, for example, enumerate every possible
int
value.-
For most questions, it should be enough to have 3 to 5 examples to support your answer. Do not have more than 10 for any one question.
-
For questions 2 and 3, you don’t really need more than one example if your example clearly demonstrates that your claim is true.
-
-
Examples in support of your answer must be computed by the C program. For example, do not just print out the string "3 + 6 = 9" instead write C code that computes the expression and prints out its result, like this:
int x, y; x = 3; y = 6; printf ("%d + %d = %d\n", x, y, (x+y));
-
Your C program, when run, should print out the answer to each question in order, with supporting examples, in an easy to read format. Use formatted printf statements, and do not print out lines that are longer than 80 characters (break long output up into multiple lines).
-
Your code should be commented, modular, robust, and use meaningful variable and function names. This includes having a top-level comment describing your program and listing your name. In addition, every function should include brief description of its behavior.
6. Tips
-
Remember that type is important in C!
-
If you give different formatting codes to
printf
, you can print out the same value as different types (for example, printing a value as%c
looks different than printing it as%d
). -
If you are not seeing the values that you expect to see, check your
printf
format string and usegdb
to examine your running program.
-
7. Submitting your Lab
Please remove any debugging output prior to submitting.
To submit your code, commit your changes locally using git add
and
git commit
. Then run git push
while in your lab directory.
Also, it is good practice to run make clean
before doing a git add and
commit; you do not want to add to the repo any files that are built by gcc
(e.g. executable files). Included in your lab git repo is a .gitignore
file
telling git to ignore these files, so you likely won’t add these types of files
by accident. However, if you have other gcc
compiled binary files in your
repo, please be careful about this.
Here are the commands to submit your solution (from your ~/cs31/labs/Lab2-userID1
directory):
$ make clean
$ git add lab2.c
$ git commit -m "correct and well-commented Lab2 solution"
$ git push
Verify that the results appear (e.g., by viewing the the repository on CS31-S24). You will receive deductions for submitting code that does not run or repos with merge conflicts. Also note that the time stamp of your final submission is used to verify you submitted by the due date or by the number of late days that you used on this lab, so please do not update your repo after you submit your final version for grading.
If you have difficulty pushing your changes, see the "Troubleshooting" section and "can’t push" sections at the end of the Using Git for CS31 Labs page. And for more information and help with using git, see the git help page.
After you submit your solution for grading, you should fill out and submit the required Lab 2 Questionnaire.
8. Handy Resources
-
Class EdSTEM page for questions and answers about lab assignment