Week 1 Homework Solutions
Part 1: Complete the following problems.
1. What is machine code? Why is it preferable to write programs in a high level language such as C++?
Machine code is the language which the computer hardware understands and executes. Instructions in a high-level language are closer to a natural language, such as English and therefore are easier to understand and learn than machine language.
2. What does a compiler do? What kinds of errors are reported by a compiler?
A compiler translates high level language into machine code. Compilers deal with syntax errors.
3. What does the linker do?
A linker takes one or more machine code modules generated by a compiler and combines them into a single executable program.
4. What is an algorithm?
An algorithm is a list of well-defined steps for completing a task.
5. Bob enters a pizza shop and notices there are three different sizes of pizzas available. Sizes are given as the diameter of the pizza in inches. The cost of a pizza is based on the size. Bob would like to know which size of pizza has the lowest cost per square inch.
a. Identify the inputs and outputs for this problem.
Inputs: diameter and cost of each of three different size pizzas
Output: size of pizza with the lowest cost per square inch
b. Identify the processing needed to convert the inputs to the outputs.
Area of each pizza in square inches = pi * (diameter / 2) 2
Cost per square inch = cost / area
c. Design an algorithm in pseudocode to solve this problem. Make sure to include steps to get each input and generate each output.
1. Get diameter of first size pizza and cost of first pizza
2. Find area of first pizza = pi * (diameter of first size pizza / 2) 2
3. Find cost per square inch of first pizza = cost of first pizza / area of first pizza
4. Get diameter of second size pizza and cost of second pizza
5. Find area of second pizza = pi * (diameter of second size pizza/ 2) 2