# Week 1 Homework

Topics: Programming language, Assembly language, Inch Pages: 5 (843 words) Published: November 5, 2014
COMP122
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 6. Find cost per square inch of second pizza = cost of second pizza / area of second pizza 7. Get diameter of third size pizza and cost of third pizza

8. Find area of third pizza = pi * (diameter of third size pizza / 2) 2 9. Find cost per square inch of third pizza = cost of third pizza / area of third pizza 10. if cost per square inch of first pizza is lowest, output size of first pizza 11. if cost per square inch of second pizza is lowest, output size of second pizza 12. if cost per square inch of third pizza is lowest, output size of third pizza

Part 2: Complete the following problems.

1. Given the following expressions, what value would they have in a C++ program?
a. 13 / 4= 3
b. 2 + 12 / 4= 5
c. 21 % 5= 1
d. 3 - 5 % 7= -2
e. 17.0 / 4= 4.25
f. 8 - 5 * 2.0= -2.0
g. 14 + 5 % 2 - 3= 12
h. 15.0 + 3.0 / 2.0= 16.5
i. 6.0 + 12 % 5 = 8.0

2. Given the following variable declarations:

int num1 = 10, num2 = 20, newNum = 30;
double x = 5.0, y = 8.0;

Determine which of the following assignment statements are valid. For each invalid statement, explain why it is invalid. Assume that each statement immediately follows the above variable declarations.

a. num1 = 15;valid
b. num2 = num1 - 18;valid
c. num1 = 5; num2 = 2 + 6; num1 = num2 / 3;valid
d. num1 + num2 = newNum;expression on left side of =
e. x = 12 * num1 - 15.3;valid
f. num1 * 2 = newNum;expression on left side of =
g. x / y = x * y;expression on left side of =
h. num2 = num1 % 2.0;can't use modulo with floats
i. newNum = static_cast (x) % 5;valid
j. x = x + 5;valid
k. newNum = num1 + static_cast (4.6 / 2);valid

3. For each of the following lines of variable declarations, identify it as valid or describe what makes the line invalid.

Line 1:n = 12;Data type of n is missing
Line 2: char letter = ;No value assigned to letter
Line 3: int one = 5,...