# Lab 2: Types

Pre-lab homework. None.

Background

As you recall, numbers can be represented in various bases. A base describes how many unique digits are used is representing the number. For example, we mostly represent numbers in base 10; there are 10 different digits used to represent the numbers: 0,1,2,3,4,5,6,7,8,9. Numbers can be represented in many (infinite!) other bases, such as base 8. In base 8, the numbers are represented with only 8 digits: 0,1,2,3,4,5,6,7.

The actual value of a number depends on both what digits are used, and the location of the digits. Obviously, the number 123 is smaller than the number 321. In particular the farther left a digit is the more contribution it makes to the size of the number. If we number the columns from right to left, starting with 0, then when the base is b and the digit d is in column c, then the contribution of that digit to the value of the number is d*bc. For example the number 123 base 10 (written 12310) is 3*100 + 2*101 + 1*102, which totals 123. For a less obvious example, 1238 is 3*80 + 2*81 + 1*82, which totals 83.

The reason you're reading all this is because computers don't store their numbers in base 10, but rather in base 2 (the only digits are 0 and 1). As computer programmers, we need to be able to convert numbers from the binary (base 2) format to the decimal (base 10) format. That is what you will do for the lab.

What to do

You will write a program that converts a number from binary to decimal format and prints out the decimal value of that number. In particular your program will:

1.     Display your name and alpha code.

2.     Prompt the user to enter a 4-bit binary number (Recall that binary digits are also called bits).  You MAY assume that:

1. the number the user enters really is a 4-bit binary number (ie, composed of exactly four 1s and 0s), and
2. the number will have a space between each digit, so 1110 would be entered as 1 0 1 1

3.     Convert the four bit number to a single decimal number.  See background discussion above.

4.     Print out the decimal value of the number.

Call your instructor over and demo your solution when you have finished.  Reminder: Any lab not completed and shown to your instructor during the lab period must be submitted (as a screen capture printout of your working program and paper copy of your source code) by the close of business the following day or you will receive a grade of 0.

Converting Base 10 (Decimal) Numbers to Binary

Background

Now that you understand how to convert binary numbers into their decimal form you must become familiar with performing the opposite function, converting a given decimal number into it's binary representation.

In the class lecture notes, we covered two different methods for converting a decimal value to its binary representation.  For the second half of the lab you will choose one of the two methods and implement it in C++.  Assume the user will provide an input value that requires no more than 6 bits to express in binary.

For example if the user input the value 23, your program should output 010111Question:  What is the maximum decimal value that can be expressed with six bits?  Call this value MAX6.  You will need that knowledge for your program.

What to do

You will write a program that converts a number from binary to decimal format and prints out the decimal value of that number. In particular your program will:

1.     Display your name and alpha code.

2.     Prompt the user to enter a decimal number that is not lager than MAX6.  You may assume that the number the user enters will not be larger than the value you specify.

3.     Convert the decimal number to its binary equivalent.  See background discussion above and class notes.

4.     Print out the binary value of the number.

Call your instructor over and demo your solution when you have finished.  Reminder: Any lab not completed and shown to your instructor during the lab period must be submitted (as a screen capture printout of your working program and paper copy of your source code) by the close of business the following day or you will receive a grade of 0.

NOTE: This is the end of the “must turn in” section of the lab.  For some labs, you will reach this point before the end of the lab period.  When that happens, you should demo the working lab pieces to your instructor, then spend the remainder of the lab period working on the “Going Further” sections.  You are expected to devote the entire lab period time to as much IC210 practice as possible.

Going Further

If you finish the steps above you should tackle this problem:

The Shift Cipher, also known as the Caesar Cipher, is a rather basic method of encryption that was used extensively by the Roman military as they conquered the the western world.

Caesar is considered to be one of the first persons to have ever employed encryption for the sake of securing messages. Caesar decided that shifting each letter in the message would be his standard algorithm, and so he informed all of his generals of his decision, and was then able to send them secured messages. Using the Caesar Shift (3 to the right), the message,

would be encrypted as,

"UHWXUA WR URPH"

In this example, 'R' is shifted to 'U', 'E' is shifted to 'H', and so on. Now, even if the enemy did intercept the message, it would be useless, since only Caesar's generals could read it.

Thus, the Caesar cipher is a shift cipher since the ciphertext alphabet is derived from the plaintext alphabet by shifting each letter a certain number of spaces. For example, if we use a shift of 16, then we get the following pair of ciphertext and plaintext alphabets:

`Plaintext:  A B C D E F G H I J K L M N O P Q R S T U V W X Y Z`
```

```
`Ciphertext: Q R S T U V W X Y Z A B C D E F G H I J K L M N O P`

The amount of the shift is also know as the "key".  If the key is known it is a simple matter of shifting a letter than many positions to find its ciphertext value.  The challenge will be to figure out how to loop from the front of the alphabet back to the beginning.  Remember, we have discussed an operation that may be new to you but will be invaluable in this exercise.

Using the skills you have learned to this point you can write a program that reads in a 4-letter message and a key value and outputs the correct ciphertext.  In particular you program will:

1.     Display your name and alpha code.

2.     Prompt the user for a 4-letter message.

3.     Prompt the user for a key.

4.     Convert the plaintext to ciphertext.
NOTE: First implement a solution that ignores the “loop around” problem (e.g., getting the letter Z plus a key of 3 to come out to the letter C).  Once you have this working, then tackle the more complete solution.

5.     Display the ciphertext to the screen.

Converting Radians to Degrees, Minutes and Seconds

`As students at an engineering school you have had to deal with angular measurement in the form of radians.  To`
`understand the definition of a radian, consider the unit circle (a circle of radius 1) whose center is the vertex of the`
`angle in question. Then the angle cuts off an arc of the circle, and the length of that arc is the radian measure of the`
`angle. It is easy to convert between degree measurement and radian measurement. The circumference of the entire`
`circle is 2, so it follows that 360° equals 2 radians. Hence, 1° equals /180 radians, and 1 radian equals 180/`
`degrees.`
` `
`Write a program that reads in an angle measure in radians, and prints out the same angle measure in degrees, minutes and seconds.`
`Remember, if d,m,s is the degree/minutes/seconds measure, all must be integers, m and s must be in [0,59], and the angle in degrees must equal`
` `
`        d + m/60 + s/3600`
` `
`to within less than 1 second of a degree of error.`
` `
`To convert from a radians angle measure to degrees,`
` `
`        degrees = radians * 180/Pi`
` `
`where Pi is approximately 3.14159265358979323846.`
` `

Write a program that will ask the user for the length of an angle in radians and convert it to the form d° m” s’.

In particular you program will:

1.     Display your name and alpha code.

2.     Prompt the user for an input.

3.     Convert the radians to degrees, minutes and seconds.

4.     Display the result to the screen in the correct format (d° m” s’).
NOTE: It’s tricky to get the degree symbol (°) to show up in your program’s output, consistently (it’s not a standard ASCII character).  To simplify things, you can just use a capital D to indicates degrees.