Section 1.2 Homework 2
The goal of this assignment is to give you some practice writing simple C++ programs and to check your knowledge of some of the main concepts fromΒ chapters 4 & 5:Β if statements and loops.
Donβt forget to only submit once you have all questions right.
Exercises Exercises
2.
3.
4.
Write a simple calculator program in C++. Your program should:
-
read in the operator symbol (+, -, *, /) as a char
-
read in two decimal numbers (type double)
-
output the result of performing the requested operation on the given numbers
-
format the output as in the given example
For example:
| Input | Result |
|---|---|
+ 5.5 2.01 |
5.5 + 2.01 = 7.51 |
5.
Write a C++ program that reads in two integers, and then outputs whether the first number is greater than the second, the first number is less than the second, or the two numbers are equal. See the examples for the correct format of the output.
For example:
| Input | Result |
|---|---|
| 3 5 |
3 is less than 5 |
| 6 1 |
6 is greater than 1 |
| -42 -42 |
-42 is equal to -42 |
6.
In C++, which operator is used to compare two values for equality (for example, checking whether a variable has the value 2)?Β Hint: There is one right answer.
- ==
- equal
- =
- .equals
- $
7.
Which of the following expressions evaluate to true? Note: choose all the correct answers.
0true1(10 <= 3)!(10 <= 3)false
8.
Which of the following expressions evaluate to TRUE?
true && false! ( false || (false && true) )true || (false && ! true)(true && false) || ! (true || false)false || ! falsefalse && ! false
9.
10.
11.
12.
13.
Which part is missing from the for loop below?
for (int n = 0; ; n += 2)
- Variable initialization
- Loop condition
- Variable update
14.
Which part is missing from the for loop below?
for (int n = 100; n >= 0 ; )
- Variable initialization
- Loop condition
- Variable update
15.
16.
Which of the following is NOT a C++ loop structure?
fordo-whilewhilerepeat
17.
Write a C++ program that gets a positive integer from standard input, then outputs the square of all integers from the entered number down to one.Β See the example for the correct format of the output.
Note: Use a βforβ loop to solve this problem.
For example:
| Input | Result |
|---|---|
| 5 | 5 squared is 25 4 squared is 16 3 squared is 9 2 squared is 4 1 squared is 1 |
18.
Write a C++ program that repeatedly reads a string from standard input and compares it with the string "my_password".
- If the strings match, the program outputs the message, "CORRECT!", and stops.
- If the strings do not match, and the user has not already made ten attempts, the program reads another string and compares with "my_password" as before.
- If the userβs tenth attempt fails to match "my_password", the program outputs the message, "WRONG!", and stops.
Note: Use a βforβ loop to solve this problem.
For example:
| Input | Result |
|---|---|
| my_password | CORRECT! |
| your_password monkeymonkeymonkey 1234 my_password |
CORRECT! |
| x1 x2 x3 x4 x5 x6 x7 x8 x9 x99 |
WRONG! |
19.
Write a C++ program that reads integers from standard input and keeps a running sum of all the positive integers that are entered. As soon as a negative integer is entered, the program stops and outputs the sum of the positive integers that were read. If the first number is negative, then the result should be 0. Hint: Solve this problem with a
whileloop.
Format your output as in the given examples.
For example:
| Input | Result |
|---|---|
| 4 6 -5 |
The sum of the positive numbers is 10. |
| -65 | The sum of the positive numbers is 0. |
20.
Write a C++ program that reads integers from standard input until a number greater than 1000 is entered.Β At that point, the program stops and prints a report regarding how many numbers were entered that were not greater than 1000.Β Match the form of your output to the given examples.
Hint: Solve this problem with a
whileloop.
For example:
| Input | Result |
|---|---|
| 45 823 1000 1001 |
You entered 3 numbers <= 1000. |
| 2018 | You entered 0 numbers <= 1000. |
21.
In Linux, what is the command
pwd used for?
- print name of current/working directory
- change password
- path wrapper disable
- pluggable window module
22.
In Linux, what is the command
cd used for?
- change directory
- change distribution
- check daemon
- check disk capacity
23.
In Linux, running the
cd command (without any arguments) will ...
- take you to your home directory
- cache your current directory path
- check current document for spelling errors
- list all the files in your current directory
24.
25.
In Linux (and in other systems), a path gives theΒ unique location of a file or a folder in a file system..
Alternatively referred to as the full path, the ___________ path for a file specifies the root directory and all other subdirectories in which the file is contained.
- absolute
- relative
- proper
- normal
26.
In Linux (and in other systems), a path gives theΒ unique location of a file or a folder in a file system.
The relative path for a file specifies the fileβs location relative to ...
- the current working directory
- the root of the file system
- the parentof the current working directory
- the userβs home directory
You have attempted of activities on this page.
