Sei sulla pagina 1di 3

Exercises

1.1

Write a program which inputs a temperature reading expressed in Fahrenheit


and outputs its equivalent in Celsius, using the formula:
5
C = ( F 32 )
9
Compile and run the program. Its behavior should resemble this:
Temperature in Fahrenheit: 41
41 degrees Fahrenheit = 5 degrees Celsius

1.2

Which of the following represent valid variable definitions?


int n = -100;
unsigned int i = -100;
signed int = 2.9;
long m = 2, p = 4;
int 2k;
double x = 2 * m;
float y = y * 2;
unsigned double z = 0.0;
double d = 0.67F;
float f = 0.52L;
signed char = -1786;
char c = '$' + 2;
sign char h = '\111';
char *name = "Peter Pan";
unsigned char *num = "276811";

1.3

Which of the following represent valid identifiers?


identifier
seven_11
_unique_
gross-income
gross$income
2by2
default
average_weight_of_a_large_pizza
variable
object.oriented

1.4

Define variables to represent the following entities:

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

Copyright 2004 World eBook Library

Exercises
2.1

Write expressions for the following:


To test if a number n is even.
To test if a character c is a digit.
To test if a character c is a letter.
To do the test: n is odd and positive or n is even and negative.
To set the n-th bit of a long integer f to 1.
To reset the n-th bit of a long integer f to 0.
To give the absolute value of a number n.
To give the number of characters in a null-terminated string literal s.

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

Write a program which produces a simple multiplication table of the


following format for integers in the range 1 to 9:
1 x 1 = 1
1 x 2 = 2
...
9 x 9 = 81

44

C++ Programming

Copyright 2004 World eBook Library

Potrebbero piacerti anche