CS202 - Assignment Two

Due: 2016-03-02 11:55p

Objectives

1. [22 points] A little bit of math

A. [2 points] When we count on our fingers, we typically assign a single number to each finger. Imagine if instead we used our fingers to count in binary, with extended fingers for 1 and retracted fingers for 0. What is the highest number you could count to using all of your fingers? What if you include your toes (ignoring for the moment that perhaps not all of us can move each toe individually)?

B. [2 points] You just bought a new 3 TB hard drive. You plug it in and your computer reports the size in TiB/GiB/MiB/KiB. If you didn’t know the difference, how many bytes would appear to have gone “missing”?

C. [6 points] Convert the following hexadecimal numbers to binary. (For all of these conversions, you will only get full credit if I can see your work and follow it. So, do not just break out your calculator…)

a. 4B
b. DEAD
6. 521

D. [6 points] Convert the following binary numbers to hexadecimal.

a. 1101
b. 10110101
c. 11010011010

E. [6 points] Convert the following hexadecimal numbers to decimal.

a. FF
b. 3C
c. 222

2. [8 points] Where are the variables?

Consider the following variables declarations:

char a, b, c, *s, *t, *u;

How are these laid out in memory? Write a program which includes this declaration and prints out the locations of all of these variables in memory. Describe how they are arranged in memory. How big are they? Are they in order? Are they tightly packed? Write your observations into the comment before this function.

Now, let’s make use of that knowledge. Add the following lines of code to you program:

  a = 'A'; 
  b = 'B'; 
  c = 'C'; 
  s = &a; 
  t = &b; 
  u = &c; 

As you should be able to tell, this loads a, b, and c with ‘A’, ‘B’, and ‘C’, respectively. It also sets s, t, and u to point at a, b, and c. Add another printf to your program that prints out the contents of each one of those variables (which means you should give me addresses for the pointers, not what they are pointing at). Normally, you would write this:

 printf("a: %c, b: %c, c: %c, s*: %p, t*: %p, u*: %p\n", a,b,c,s,t,u); 

However, I want you to demonstrate what you learned above. So, the only variable you are allowed to use in the parameter list of the printf is s. Hint: you can use both the contents _and_ the address of s in your solution, and, of course, you can include s or its address in mathematical expressions with constants.

Put this program in a file called hw2_mem.c

3. [15 points] Base2base

Write a program that takes three inputs: a number, the current base, and a target base. Your program should print out the value in the target base. If you were paying attention during the last assignment, you would have noticed that you could do half of this assignment with some judicious use of strtol, however, I would like you to not do that. I would like you to write two functions. One function should take a string and a base and return an integer value (basically, you are writing your own version of strol). The second function should take an integer value and a base and print out a string representation of the number in the given base. We have not talked about how to create strings dynamically, so you cannot return a new string from a function yet.

An important aspect of this is that the char type that we use to store characters in C is actually a numeric type. In other words, ‘a’ and the value 97 (the ASCII value of ‘a’) are interchangeable. So, given some char variable c, you could compare it to ‘a’ (c > 'a'), or do math with it (c + 5 or even c - '0').

Example (assuming you named the executable base2base):


> ./base2base 1010 2 10
10
> ./base2base 1010 2 16
A
> ./base2base 1010 2 8 
12
> ./base2base FA 16 2 
11111010
> ./base2base 1K 29 10
49


Put this program in a file called hw2_base2base.c


Turning in your work

My preference for the first part of this assignment would be that you turn it in electronically in a PDF or RTF file (not in a .doc or .docx file). however, I realize that since I’m asking you to do math the long way, I will accept handwritten submissions (provided I can actually read your handwriting).

The two programs should be handed in as separate files as indicated by their descriptions. Each function should be commented to describe its function (including extra information were requested). The top of each file should also include a comment including your name, date, assignment, compile instructions, and run instructions.

Submit the completed assignment on Moodle.