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.9
30
5 months ago
Preview (3 of 9 Pages)
100%
Purchase to unlock

Page 1

COMP122: Introduction to C++ Programming - Week 1 Homework - Page 1 preview image

Loading page image...

1COMP122: Introduction to C++ Programming-Week 1 HomeworkCOMP122Week 1 HomeworkPart 1:Completethe following problems.1. What is machine code? Whyis it preferable to write programs in a high level languagesuch as C++?Machine codeis the low-level, binary instructions that a computer’s processor candirectly 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 likeC++because they areeasier to read, write, and maintain. High-level languages are more abstract, closer tohuman language, and allow for faster development compared to working directly withmachine code.2. What does a compiler do? What kinds of errors are reported by a compiler?Acompilertranslates source code written in a high-level programming language (likeC++) into machine code or an intermediate form (like bytecode).The compiler reports errors such assyntax errors(e.g., missing semicolons, incorrectvariable names) andsemantic errors(e.g., type mismatches or incorrect function calls).3. What does the linker do?Thelinkercombines object files (produced by the compiler) and resolves referencesbetween them, including library calls. It produces the final executable program.4. What is an algorithm?Analgorithmis a step-by-step procedure or formula for solving a problem or performinga 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 thesize. 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.

Page 2

COMP122: Introduction to C++ Programming - Week 1 Homework - Page 2 preview image

Loading page image...

Page 3

COMP122: Introduction to C++ Programming - Week 1 Homework - Page 3 preview image

Loading page image...

2c. Design an algorithm in pseudocode to solve this problem. Make sure to includesteps to get each input and generate each output.Part 2: Completethe following problems.1. Given the following expressions, what value would they have in a C++ program?a. 13 / 4b. 2 + 12 / 4c. 21 % 5d. 3-5 % 7e. 17.0 / 4f. 8-5 * 2.0g. 14 + 5 % 2-3h. 15.0 + 3.0 / 2.0a.13 / 4Result:3Explanation: Integer division truncates the result.b.2 + 12 / 4Result:5Explanation: Division is done first, so12/4=3, then2+3=5.c.21 % 5Result:1Explanation: The remainder when 21 is divided by 5 is 1.d.3-5 % 7Result:3-5=-2Explanation:5%7is 5 (because 5 is less than 7), so3-5=-2.
Preview Mode

This document has 9 pages. Sign in to access the full document!

Study Now!

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

Document Details

Related Documents

View all