Week 1 Homework
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++? A: The machine code is the language which the computer hardware understands and executes. It is preferable to write programs in a high level language such as C ++ because it is much easier to understand and learn this machine language. 2. What does a compiler do? What kinds of errors are reported by a compiler? A: A computer translates high level language into machine code. While a compiler reports errors such as syntax errors.
3. What does the linker do?
A: 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?
A: An algorithm is a list of instructions set up 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.
A: Inputs are the diameter and cost of each of the three different size pizzas
Outputs are the size of pizza with the lowest cost per inch
b. Identify the processing needed to convert the inputs to the outputs.
A: 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 person
2. Find area of first pizza
3. Find cost per square inch of first pizza
4. Get diameter of second size pizza
5. Find area of second pizza
6. Find cost per square inch of second pizza
7. Get diameter of third pizza
8. Get cost of third pizza
9. Find area of third pizza
10. Find cost per square inch of third pizza
11. If the cost per square inch of first pizza is lowest output size of first pizza
Part 2: Complete the following problems.
1. Given the following expressions, what value would they have in a C++ program?
a. 13 / 4
b. 2 + 12 / 4
c. 21 % 5
d. 3 - 5 % 7
e. 17.0 / 4
f. 8 - 5 * 2.0
g. 14 + 5 % 2 – 3
h. 15.0 + 3.0 / 2.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;
b. num2 = num1 - 18;
c. num1 = 5; num2 = 2 + 6; num1 = num2 / 3;
d. num1 + num2 = newNum;
A: Invalid the variable has to be on the other side
e. x = 12 * num1 - 15.3;
f. num1 * 2 = newNum;
A: Invalid the expression has to be on the left side
g. x / y = x * y;
A: Invalid, there could be one expression
h. num2 = num1 % 2.0;
i. newNum = static_cast<int> (x) % 5;
j. x = x + 5;
k. newNum = num1 + static_cast<int> (4.6 / 2);
A: Invalid, wrong use of
3. For each of the following lines of variable declarations, identify it as valid or describe what makes the line invalid.
n = 12;
A: Invalid, Data type of n is missing
Line 2: char letter = ;
A: Invalid, no value assigned to letter
Line 3: int one = 5, two;
Line 4: double x, y, z;
4. Write C++ statements that accomplish each of the following:
a. Declare and initialize int variables x to 25 and y to 18.
- int x = 25, y = 18;
b. Declare and initialize an int variable temp to 10 and a char variable ch to 'A'.
- int temp = 10;
- char ch = ‘A’;
Please join StudyMode to read the full document