• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
Solutions to Exercises
1.1
#include <iostream.h>int main (void){double fahrenheit;double celsius;cout << "Temperature in Fahrenhait: ";cin >> fahrenheit;celsius = 5 * (fahrenheit - 32) / 9;cout << fahrenheit << " degrees Fahrenheit = "<< celsius << " degrees Celsius\n";return 0;}
1.2
int n = -100;// validunsigned int i = -100;// validsigned int = 2.9;//
invalid
: no variable namelong m = 2, p = 4;// validint2k;//
invalid
: 2k not an identifierdouble x = 2 * m;// validfloat y = y * 2;// valid (but dangerous!)unsigned double z = 0.0;//
invalid
: can't be unsigneddouble d = 0.67F;// validfloat f = 0.52L;// validsigned char = -1786;//
invalid
: no variable namechar c = '$' + 2;// validsign char h = '\111';//
invalid
: 'sign' not recognizedchar *name = "Peter Pan";// validunsigned char *num = "276811";// valid
1.3
identifier// validseven_11// valid _unique_// validgross-income//
invalid
: - not allowed in idgross$income//
invalid
: $ not allowed in id2by2//
invalid
: can't start with digitdefault//
invalid
: default is a keywordaverage_weight_of_a_large_pizza// validvariable// validobject.oriented//
invalid
: . not allowed in id
230C++ ProgrammingCopyright © 1998 Pragmatix Software
 
1.4
int age;// age of a persondoubleemployeeIncome;// employee incomelongwordsInDictn;// number of words in dictionarycharletter;// letter of alphabetchar*greeting;// greeting message
2.1
// test if
n
is even:n%2 == 0// test if
c
is a digit:c >= '0' && c <= '9'// test if
c
is a letter:c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z'// test if
n
is odd and positive or
n
is even and negative:n%2 != 0 && n >= 0 || n%2 == 0 && n < 0// set the
n
-th bit of a long integer
to 1:f |= (1L << n)// reset the
n
-th bit of a long integer
to 0:f &= ~(1L << n)// give the absolute value of a number
n
:(n >= 0 ? n : -n)// give the number of characters in a null-terminated string literal
s
:sizeof(s) - 1
2.2
(((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
double d = 2 * int(3.14);// initializes d to 6longk = 3.14 - 3;// initializes k to 0charc = 'a' + 2;// initializes c to 'c'charc = 'p' + 'A' - 'a';// initializes c to 'P'
2.4
#include <iostream.h>int main (void){long n;cout << "What is the value of n? ";cin >> n;cout << "2 to the power of " << n << " = " << (1L << n) << '\n';return 0;}
2.5
#include <iostream.h>int main (void){double n1, n2, n3;cout << "Input three numbers: ";cin >> n1 >> n2 >> n3;
231C++ ProgrammingCopyright © 1998 Pragmatix Software
 
cout << (n1 <= n2 && n2 <= n3 ? "Sorted" : "Not sorted") << '\n';return 0;}
3.1
#include <iostream.h>int main (void){double height, weight;cout << "Person's height (in centimeters): ";cin >> height;cout << "Person's weight (in kilograms: ";cin >> weight;if (weight < height/2.5)cout << "Underweight\n";else if (height/2.5 <= weight && weight <= height/2.3)cout << "Normal\n";elsecout << "Overweight\n";return 0;}
3.2It will output the message
n is negative
. This is because the else clause is associated with the if clause immediately preceding it. The indentation in the codefragment
if (n >= 0)if (n < 10)cout << "n is small\n";elsecout << "n is negative\n";
is therefore misleading, because it is understood by thecompiler as:
if (n >= 0)if (n < 10)cout << "n is small\n";elsecout << "n is negative\n";
 The problem is fixed by placing the second if within acompound statement:
if (n >= 0) {if (n < 10)cout << "n is small\n";} elsecout << "n is negative\n";
www.pragsoft.comSolutions to Exercises232
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...