Solutions to Exercises in Study Guide
LESSON 1
Exercise 1.1
We repeat the program here:
#include <iostream>
using namespace std;
int main( )
{ cout << "Hello world"; return 0; }
Descriptive Comment:
There is no descriptive comment.
StandardHeaderFile:
iostream
StatementSequence:
cout << "Hello world"; return 0;
Exercise 1.2
//A poem
#include <iostream>
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;
}
LESSON 2
Exercise 2.1
(i) (() + ())
| |
16 + 11
|
27
(ii) ((-5 + -4) - -3)
|
-9 - -3
|
-6
,(iii) (((6 * 7) / 8) * 9)
|
42 / 8 * 9
|
5 * 9
|
45
(iv) ((1 - 2) + (() * 5))
| |
-1 + 0 * 5
|
-1 + 0
|
-1
(v) ((-1 + (23 / -4)) + 56)
|
-1 + -5 + 56
|
-6 + 56
|
50
Exercise 2.2
//Lesson 2 Exercise 2.2
//display number of seconds in a minute, hour, day and year
#include <iostream>
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;
}
Exercise 2.3
//Lesson 2 Exercise 2.3
#include <iostream>
using namespace std;
int main( )
{
2
, cout << "The remainder of 234 divided by 13 is ";
cout << 234 - () * 13 << endl;
return 0;
}
The round brackets are not necessary but their use makes the program more readable.
LESSON 3
Exercise 3.1
//Inputs three numbers and displays them in reverse order
#include <iostream>
using namespace std;
int main( )
{
int i, j, k;
cout << "Enter three numbers: ";
cin >> i >> j >> k;
cout << "In reverse: " << k << " " << j << " " << i << endl;
return 0;
}
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
Exercise 3.3
//Fahrenheit to Celsius conversion
#include <iostream>
using namespace std;
int main( )
{
int fahrenheit;
cout << "Enter the temperature in Fahrenheit: ";
cin >> fahrenheit;
cout << "Celsius = " << 5 *(fahrenheit - 32) / 9 << endl;
return 0;
}
, LESSON 4
Exercise 4.1
//Fahrenheit to Celsius conversion (version for lesson 4)
#include <iostream>
using namespace std;
int main( )
{
int fahrenheit, celsius;
cout << "Enter the temperature in Fahrenheit: ";
cin >> fahrenheit;
celsius = 5 * (fahrenheit - 32) / 9;
cout << "Celsius = " << celsius << endl;
return 0;
}
Exercise 4.2
//How many boxes?
#include <iostream>
using namespace std;
int main( )
{
int items, itemsPerBox, boxes, remainder;
cout << "How many items to be packed? ";
cin >> items;
cout << "How many items fit in a box? ";
cin >> itemsPerBox;
boxes = items / itemsPerBox;
remainder = items % itemsPerBox;
cout << "You will need " << boxes << " boxes." << endl;
cout << "There will be " << remainder << " items left over." << endl;
return 0;
}
Exercise 4.3
int n = 10; // 10
n += 3; // 13
n /= 2; // 6
n++; // 7
n %= 4; // 3
n -= 5; // -2
The final value of n is -2.
LESSON 5
Exercise 5.1
j k m n
Line 7: ? ? ? ?
j k m n
Line 8: 3 ? ? ?
14
LESSON 1
Exercise 1.1
We repeat the program here:
#include <iostream>
using namespace std;
int main( )
{ cout << "Hello world"; return 0; }
Descriptive Comment:
There is no descriptive comment.
StandardHeaderFile:
iostream
StatementSequence:
cout << "Hello world"; return 0;
Exercise 1.2
//A poem
#include <iostream>
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;
}
LESSON 2
Exercise 2.1
(i) (() + ())
| |
16 + 11
|
27
(ii) ((-5 + -4) - -3)
|
-9 - -3
|
-6
,(iii) (((6 * 7) / 8) * 9)
|
42 / 8 * 9
|
5 * 9
|
45
(iv) ((1 - 2) + (() * 5))
| |
-1 + 0 * 5
|
-1 + 0
|
-1
(v) ((-1 + (23 / -4)) + 56)
|
-1 + -5 + 56
|
-6 + 56
|
50
Exercise 2.2
//Lesson 2 Exercise 2.2
//display number of seconds in a minute, hour, day and year
#include <iostream>
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;
}
Exercise 2.3
//Lesson 2 Exercise 2.3
#include <iostream>
using namespace std;
int main( )
{
2
, cout << "The remainder of 234 divided by 13 is ";
cout << 234 - () * 13 << endl;
return 0;
}
The round brackets are not necessary but their use makes the program more readable.
LESSON 3
Exercise 3.1
//Inputs three numbers and displays them in reverse order
#include <iostream>
using namespace std;
int main( )
{
int i, j, k;
cout << "Enter three numbers: ";
cin >> i >> j >> k;
cout << "In reverse: " << k << " " << j << " " << i << endl;
return 0;
}
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
Exercise 3.3
//Fahrenheit to Celsius conversion
#include <iostream>
using namespace std;
int main( )
{
int fahrenheit;
cout << "Enter the temperature in Fahrenheit: ";
cin >> fahrenheit;
cout << "Celsius = " << 5 *(fahrenheit - 32) / 9 << endl;
return 0;
}
, LESSON 4
Exercise 4.1
//Fahrenheit to Celsius conversion (version for lesson 4)
#include <iostream>
using namespace std;
int main( )
{
int fahrenheit, celsius;
cout << "Enter the temperature in Fahrenheit: ";
cin >> fahrenheit;
celsius = 5 * (fahrenheit - 32) / 9;
cout << "Celsius = " << celsius << endl;
return 0;
}
Exercise 4.2
//How many boxes?
#include <iostream>
using namespace std;
int main( )
{
int items, itemsPerBox, boxes, remainder;
cout << "How many items to be packed? ";
cin >> items;
cout << "How many items fit in a box? ";
cin >> itemsPerBox;
boxes = items / itemsPerBox;
remainder = items % itemsPerBox;
cout << "You will need " << boxes << " boxes." << endl;
cout << "There will be " << remainder << " items left over." << endl;
return 0;
}
Exercise 4.3
int n = 10; // 10
n += 3; // 13
n /= 2; // 6
n++; // 7
n %= 4; // 3
n -= 5; // -2
The final value of n is -2.
LESSON 5
Exercise 5.1
j k m n
Line 7: ? ? ? ?
j k m n
Line 8: 3 ? ? ?
14