COS 1511 Solutions to exercises in the Guide
Solutions to exercises in the Guide Solutions to exercises in the Guide Part I : Starting to Program Solutions to Lesson 1 Exercise 1.1 Compare the description of the program structure that we have just given with the program we gave when describing the purpose of this lesson. #include using namespace std; int main( ) { cout "Hello world"; return 0; } What part of the program corresponds to DescriptiveComment? What parts correspond to StandardHeaderFile and StatementSequence? Solution to exercise 1.1 Descriptive Comment: There is no descriptive comment. StandardHeaderFile: iostream StatementSequence: cout "Hello world"; return 0; Exercise 1.2 Write a program to display the following poem1 on the screen. Twinkle, twinkle, little bat! How I wonder what you're at? Up above the world you fly, Like a tea-tray in the sky. Solution to Exercise 1.2 //A poem from Alice's Adventures in Wonderland by Lewis Carroll #include using namespace std; int main( ) { cout "Twinkle, twinkle, little bat!" endl; cout "How I wonder what you're at?" endl; cout "Up above the world you fly," endl; cout "Like a tea-tray in the sky." endl; return 0; } Solutions to Lesson 2 Exercise 2.1 Add round brackets to the following C++ expressions to show the order in which the operators will be evaluated: (i) 80 / 5 + 70 / 6 (ii) -5 + -4 - -3 (iii) 6 * 7 / 8 * 9 (iv) 1 - 2 + 3 / 4 * 5 (v) -1 + 23 / -4 + 56 Also give the value of each of the expressions. Solution to exercise 2.1 (i) ((80 / 5) + (70 / 6)) | | 16 + 11 | 27 (ii) ((-5 + -4) - -3) | -9 - -3 | -6 Modules: Print Module Page 1 of 58 (iii) (((6 * 7) / 8) * 9) | 42 / 8 * 9 | 5 *9 | 45 (iv) ((1 - 2) + ((3 / 4) * 5)) | -1 + 0 * 5 | -1 +* 0 | -1 (v) ((-1 + (23 / -4)) + 56) | -1 + -5 + 56 | -6 + 56 | 50 Exercise 2.2 Write a program that produces the following output: There are 60 seconds in a minute. There are XXX seconds in an hour. There are YYY seconds in a day. There are ZZZ seconds in a year. In place of XXX, YYY and ZZZ, the program should calculate and display the appropriate number of seconds. Solution to exercise 2.2 // Lesson 2 Exercise 2.2 // Display number of seconds in a minute, hour, day and year #include using namespace std; int main() { cout "The are 60 seconds in a minute." endl; cout "The are " 60 * 60 " seconds in an hour." endl; cout "The are " 60 * 60 * 24 " seconds in a day." endl; cout "The are " 60 * 60 * 24 * 365 " seconds in a year." endl; return 0; } Excercise 2.3 Write a program to calculate the remainder of 234 divided by 13. Remember that the / operator throws away the remainder. Hint: divide 234 by 13 and then multiply it by 13 again. The difference between 234 and the result of this will be the remainder. Solution to exercise 2.3 // Lesson 2 Exercise 2.3 #include using namespace std; int main( ) { cout "The remainder of 234 divided by 13 is "; cout 234 - (234 / 13) * 13 endl; return 0; } Solutions to Lesson 3 Exercise 3.1 Write a program that inputs three values and displays them on a single line in reverse order. Solution to exercise 3.1 // Lesson 3 Exercise 3.1 // Inputs three numbers and displays them in reverse order #include using namespace std; int main( ) { int i, j, k; Modules: Print Module Page 2 of 58 cout "Enter three numbers: "; cin i j k; cout "In reverse: " k " " j " " i endl; return 0; } Exercise 3.2 Look at the following program and then answer the questions that follow without typing in the program and testing it: #include using namespace std; int main( ) { int x, y, z; cout "Enter values for variables x, y and z:" endl; cin x y z; cout "x + y / z is " x + y / z endl; cout "x % z is " x % z endl; cout "y * z / x + 2 is " y * z / x + 2 endl; return 0; } (i) What will the output be if the user enters 2, 6 and 4? (ii) What will the output be if the user enters 5, 1, 3? (iii) If the last output statement is changed to cout "y * (z / x + 2) is " y * (z / x + 2) endl; what will the output of this statement be if the user enters the same values as specified in (i) and (ii)? Solution to exercise 3.2 (i) Enter values for variables x, y and z: 2 6 4 x + y / z is 3 x % z is 2 y * z / x + 2 is 14 (ii) Enter values for variables x, y and z: 5 1 3 x + y / z is 5 x % z is 2 y * z / x + 2 is 2 (iii) If 2 6 4 are entered: y * (z / x + 2) is 24 If 5 1 3 are entered: y * (z / x + 2) is 2
Geschreven voor
- Instelling
- University of South Africa
- Vak
- COS1511 - Introduction To Programming 1
Documentinformatie
- Geüpload op
- 6 november 2021
- Aantal pagina's
- 58
- Geschreven in
- 2021/2022
- Type
- Tentamen (uitwerkingen)
- Bevat
- Vragen en antwoorden
Onderwerpen
-
cos 1511
-
cos 1511 solutions to exercises in the guide