CS202 - Assignment Two

Due: 2018-09-26 9:00a

Solution part 1-2

Solution part 3

Objectives

  • Get comfortable with number systems and conversions
  • Start thinking about memory
  • Get some practice working with pointers and arrays
  • Continue to build your C programming muscles

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 4 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. 3C
b. DEAD
6. 465

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

a. 1101
b. 11010101
c. 11010101010

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

a. 2B
b. FF
c. 145

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


[5 points] Follow the submission directions

  • If the first section is turned on paper, it must be legible and have your name and section on the top
  • If the first section is turned in online, it must be a PDF or RTF file (no .doc or .docx files)
  • Each file must be named according to the instructions above.
  • Make sure that the assignment runs on Linux before you submit it. Do not assume that it will -- check it.
  • Each function should be commented to describe its function
  • At the very top of each file, you will place a block comment that follows this format exactly:
/*
Assignment 2
<Mem or base2base as appropriate>

Name:
Email: 
Date:
Collaborators:

To compile:
To run:
*/

The name, date, and email fields should be self explanatory. In the collaborators field, you should list everyone who helped you with this assignment including classmates, tutors, and myself. In the to compile and to run fields, provide the commands required to compile and execute your code (i.e., someone should be able to copy these commands directly onto a command line and run your code). The to run should include usage instructions (what arguments need to be provided and what they do).

Turning in your work

Submit the completed assignment on Canvas.

Last Updated: 10/3/2018, 11:29:06 PM