Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1.1
1.2
1.3
1.4
16
Age of a person.
Income of an employee.
Number of words in a dictionary.
A letter of the alphabet.
A greeting message.
C++ Programming
Exercises
2.1
2.2
Add extra brackets to the following expressions to explicitly show the order
in which the operators are evaluated:
(n <= p + q && n >= p - q || n == 0)
(++n * q-- / ++p - q)
(n | p & q ^ p << 2 + q)
(p < q ? n < p ? q * n - 2 : q / n + 1 : q - n)
2.3
What will be the value of each of the following variables after its
initialization:
double
longk =
charc =
charc =
d = 2 * int(3.14);
3.14 - 3;
'a' + 2;
'p' + 'A' - 'a';
2.4
Write a program which inputs a positive integer n and outputs 2 raised to the
power of n.
2.5
Write a program which inputs three numbers and outputs the message Sorted
if the numbers are in ascending order, and outputs Not sorted otherwise.
wwwWorldLibrary.net
Chapter 2: Expressions
29
Exercises
3.1
Write a program which inputs a persons height (in centimeters) and weight
(in kilograms) and outputs one of the messages: underweight, normal, or
overweight, using the criteria:
Underweight: weight < height/2.5
Normal:
height/2.5 <= weight <= height/2.3
Overweight: height/2.3 < weight
3.2
Assuming that n is 20, what will the following code fragment output when
executed?
if (n >= 0)
if (n < 10)
cout << "n is small\n";
else
cout << "n is negative\n";
3.3
Write a program which inputs a date in the format dd/mm/yy and outputs it in
the format month dd, year. For example, 25/12/61 becomes:
December 25, 1961
3.4
Write a program which inputs an integer value, checks that it is positive, and
outputs its factorial, using the formulas:
factorial(0) = 1
factorial(n) = n factorial(n-1)
3.5
Write a program which inputs an octal number and outputs its decimal
equivalent. The following example illustrates the expected behavior of the
program:
Input an octal number: 214
Octal(214) = Decimal(532)
3.6
44
C++ Programming