Question 1: Read in series of lines each containing 4 floating point numbers -- x1, y1, x2, y2 -- representing a line segment from (x1,y1) to (x2,y2). These will be terminated by a line with just the word "end". Output "yes" if any two of the line segments intersect, and "no" otherwise.
Question 2: The function ex = 1 + x + x2/2! + x3/3! + ... Use this function to read in a floating point number x and output ex to within 0.0001. Note that your code may not include any estimate for e itself.
Question 3: Read in series of lines each containing 4 integers -- x1, y1, x2, y2 -- representing a rectangle from (x1,y1) to (x2,y2). These will be terminated by a line specifying a rectangle with an area smaller than the area of the unit circle. Output the radius of the smallest possible circle centered at (3,5) which circumscribes all the rectangles.
Question 4: Here you will make a simple calculator: read in a mathematical expression and output what it evaluates to. The expression may include positive or negative floating point numbers, infix operators + (plus) - (minus) * (times) / (divide) with normal precedence, and the functions pow(x,y), sin(x), cos(x).
input: pow(sin(5),2) + cos(5)*cos(5)Question 5: Read in an unsorted list of composite (ie, non-prime) numbers (terminated by a prime number which is not in the list). Note that every element in the list occurs an even number of times except one element which occurs an odd number of times. Thus, you might have ten 21's, twelve 22's, and nine 24's (hence 24 is the special odd-occurring element). Your task is to print out the odd-occurring element. Hint: this is easiest if you use a special property of XOR.
output: 1
Question 6: As you know, ASCII is a fixed-length 8-bit encoding of alphanumeric symbols where 'A' is 65, 'B' is 66, etc. By contrast, a Huffman Code is a variable-length encoding, so that some characters might have only 1 or 2 bits to encode them, whereas others might require more. The goal of a Huffman Code is to reduce the overall number of bits required to represent strings by using fewer bits to represent the most common characters. To construct a Huffman Code, you begin with a count of the number of occurrences of each letter in a sample corpus. You construct a tree by first imagining that each letter is the root of its own tree with no leaves, and then by continually merging the two trees with the smallest counts and placing their sum at the new root until there is only one tree remaining (place the smaller child to the left, and resolve ties randomly). Finally, label left children with a 0 and right children with a 1.
The following tree results from the input: a:45, b:13, c:12, d:16, e:9, f:5:
(100)This produces the following Huffman Code:
0/ \1
(a:45) (55)
0/ \1
(25) (30)
0/ \1 0/ \1
(c:12)(b:13)(14) (d:16)
0/ \1
(f:5) (e:9)
| Letter | Huffman Code |
| a | 0 |
| b | 101 |
| c | 100 |
| d | 111 |
| e | 1101 |
| f | 1100 |
Your task: Read in a frequency table all on one line in the form
letter:occurrences, as in:
a:45 b:13 c:12 d:16 e:9 f:5
Next, repeatedly read in a line. If that line is just 1's and 0's, it represents an encoded message, and you should print out the sequence of letters which maps to that encoding. Conversely, if the line contains more than 1's and 0's, it represents a message to encode, and you should output its Huffman Encoding. If the line contains the word "quit", you should quit.
Question 7: This problem finds the minimum distance between two cities. First, read in the distances between cities. This is done by reading in lines of the form: city1 city2 distance (where all the values are non-negative integers). Keep reading until you read in a line of the form 0 0 0. Note that not all cities are connected by roads, so you can only travel between cities which have a distance entered in this table. Also, assume that distances are the same in both directions -- the distance from A to B is the same as the distance from B to A. Next, read in two indices -- a start city and an end city -- and print out the minimum distance required to travel from the start city to the end city when travelling only along the city-to-city links defined in the data set.