COMP122: Introduction to C++ Programming - Week 1 Homework

A homework assignment introducing C++ programming concepts in the first week of the course.

Evelyn Morris
Contributor
4.5
55
about 1 month ago
Preview (3 of 9)
Sign in to access the full document!
1
COMP122: Introduction to C++ Programming - Week 1 Homework
COMP122
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++?
Machine code is the low-level, binary instructions that a computer’s processor can
directly execute. It consists of 0s and 1s and is specific to the computer's architecture.
It is preferable to write programs in high-level languages like C++ because they are
easier to read, write, and maintain. High-level languages are more abstract, closer to
human language, and allow for faster development compared to working directly with
machine code.
2. What does a compiler do? What kinds of errors are reported by a compiler?
A compiler translates source code written in a high-level programming language (like
C++) into machine code or an intermediate form (like bytecode).
The compiler reports errors such as syntax errors (e.g., missing semicolons, incorrect
variable names) and semantic errors (e.g., type mismatches or incorrect function calls).
3. What does the linker do?
The linker combines object files (produced by the compiler) and resolves references
between them, including library calls. It produces the final executable program.
4. What is an algorithm?
An algorithm is a step-by-step procedure or formula for solving a problem or performing
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 of the pizza, cost of the pizza.
Output: Pizza size with the lowest cost per square inch.
b. Identify the processing needed to convert the inputs to the outputs.
2
c. Design an algorithm in pseudocode to solve this problem. Make sure to include
steps to get each input and generate each output.
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
a. 13 / 4
Result: 3
Explanation: Integer division truncates the result.
b. 2 + 12 / 4
Result: 5
Explanation: Division is done first, so 12 / 4 = 3, then 2 + 3 = 5.
c. 21 % 5
Result: 1
Explanation: The remainder when 21 is divided by 5 is 1.
d. 3 - 5 % 7
Result: 3 - 5 = -2
Explanation: 5 % 7 is 5 (because 5 is less than 7), so 3 - 5 = -2.
Preview Mode

Sign in to access the full document!

100%

Study Now!

XY-Copilot AI
Unlimited Access
Secure Payment
Instant Access
24/7 Support
Document Chat

Document Details

University
DeVry University
Subject
Information Technology

Related Documents

View all