Appendix A – Answers to Exercises

Chapter 2

1.   (a) 3   (b) 5  (c) 15   (d) 21   (e) 255   (f) 254
2.   (a) 101   (b) 10   (c) 111   (d) 1000   (e) 1111   (f) 11000
3.   (a) 1   (b) 1001   (c) 1011   (d) 11011100   (e) 11110001   (f) 11111111
4.   (a) 72 105
     (b) 97 112 112 108 108 101
     (c) 107 105 119 105
     (d) 69 120 99 105 114 101 33
 
5.   I am hungry.
6.   2,000,000,000 / 700,000 = 2857

Chapter 5

1.   HelloWorld                             7.   5 
2.   1 is less than 3                       8.   3
3.   X * Y = 8                              9.   120
     The End
                                            10.  3    2     1     GO!
4.   Z
                                            11.  "apples"
5.   AB                                          are good
     CD
                                            12.  /---\
6.   21                                          | " |
                                                 \ - /

Chapter 6

1.   short int     Note: You could also use unsigned short int
2.   float
3.   double
4.   char
5.   int          
Note: You could also use unsigned int
6.   bool
7.   double
8.   short int
9.   0
10.  0.25
11.  2                          
12.  0                          
13.  ABBA
14.  -4

Chapter 8 

1. 

#include <iostream.h>

int main()
{
   cout << "Enter a number to convert from inches to centimeters: ";
   float Inches, Centimeters;
   cin >> Inches;
   Centimeters = Inches * 2.54;
   cout << "This is equal to << Centimeters << " centimeters.\n";
   return 0;
}

2.

#include <iostream.h>

int main()
{
   int Age, Weight;
   cout << "Enter your age: ";
   cin >> Age;
   cout << "Enter your weight: ";
   cin >> Weight;
   cout << "You still have " << 100 – Age << " years before you are 100.\n";
   cout << "You can still gain " << 500 – Weight;
   cout << " pounds before you weigh 500.\n";
   return 0;
}

 

Chapter 9

1. false                6. false
2. false                7. false
3. true                 8. true
4. true                 9. false
5. false                10. true

Be careful on question 10 – here we accidentally used single equal signs instead of double.  When using a single equal sign, you are actually doing an assignment statement – this will change the values of Z to 15 and Y to 10.

11. Yes
12. Pear
13. Yes
14. blue
15.

#include <iostream>
using namespace std;

int main()
{
   cout << "Please enter an integer: ";
   int N;
   cin >> N;
   if ((N%2) == 0)
      cout << "Your number is divisible by 2\n";
   if ((N%3) == 0)
      cout << "Your number is divisible by 3\n";
   if ((N%3) == 0)
      cout << "Your number is divisible by 4\n";
   return 0;
}

 

Chapter 11

1.

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
   cout << "This program raises one integer to the power of the other.\n";
   int A,B;
   cout << "Enter the first number: ";
   cin >> A;
   cout << "Enter the second number: ";
   cin >> B;
   cout << A << " to the power of " << B << " is " << pow(A,B) << endl;
   return 0;
}

2.

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
   cout << "a = sin(x)\n";
   cout << "b = cos(x)\n";
   cout << "c = tan(x)\n";
   cout << "Please select an option: ";
   char Choice;
   cin >> Choice;
   cout << "\nEnter a value: ";
   float Value;
   cin >> Value;
   if (Choice == 'a')
      cout << "The sine of " << Value << " is " << sin(Value) << endl;
   if (Choice == 'b')
      cout << "The cosine of " << Value << " is " << cos(Value) << endl;
   if (Choice == 'c')
      cout << "The tangent of " << Value << " is " << tan(Value) << endl;

   return 0;
}

3.  The sqrt(9) function returns an integer.  You cannot assign an integer to a char variable type.

 

Chapter 12

1.  1234 
2.  543210 
3.  7 7 7 7 7 7 7 
4.  2 4 8 16 32 64 128
5.  ******
6.  yes no yes no yes no yes no yes no yes

 

Chapter 13 

1.  1 2 3 4 5 
2.  10 9 8 7 6 5 4 3 2 1
3.  2468101214161820 
4.  100 48 22 9 2.5
5.  -10
6.  *
    **
    ***
    ****
    *****

7.  for (int X=3; X<=21; X = X + 3)
    {
        cout << X << " ";
    } 

8.  for (int Y=4; Y>=-4; Y = Y – 2)
    {
        cout << Y << " ";
    } 

9.  for (int X=1; X<=127; X = X * 2 + 1)
    {
        cout << X << " ";
    }

10. for (int I=1; I<=5; I++)
    {
       for (int J=1; J<=(6-I); J++)
       {
           cout << "*";
       }
       cout << endl;
    }

 

Chapter 14

1.  int Corvettes[5] = {1956, 1963, 1968, 1983, 1997}; 

2.  string Days[7] = {"Monday","Tuesday","Wednesday","Thursday",
                      "Friday","Saturday","Sunday"};

 3.  for (int X=0; X<=9; X++)
     {
        Scores[X] = Scores[X] + 1;
     }

4.  int Total = 0;
    for (int X=0; X<=9; X++)
    {
        Total = Total + Scores[X];
    }

 

Chapter 15 

1. abc
2. zyx
3. hi
4. texas
5. (c) alp
6. (a) goodsoup
7. (b) more
8. (a) good evening
9. (b) 5
10. (a) alpha
11. (c) very good evening
12. 17
13. 14
14. rain
15. sun
16. 0
17. 3
18. 12
19. 11
20. 4294967295

 

Chapter 16 

     Return Type   Parameter(s)

1.   float         float Radius
2.   float         float Temperature
3.   float         float Miles
4.   int           int Number
5.   int           int Number1, int Number2, int Number3
6.   int           float Flour, float BPowder, float Butter, float Milk
7.   string        int Month
8.   string        string Word
9.   bool          string Word
10.  bool          int Number 

11.  float Area_Circle(float Radius)
     {
         return 3.1415 * Radius * Radius;
     }

12.  string Day_Week(int Day)
     {
         if (Day == 1) return "Sunday";
         if (Day == 2) return "Monday";
         if (Day == 3) return "Tuesday";
         if (Day == 4) return "Wednesday";
         if (Day == 5) return "Thursday";
         if (Day == 6) return "Friday";
         if (Day == 7) return "Saturday";
         return "invalid number";
     }

13.  string Reverse_Word(string Word)
     {
         string Reverse = Word;
         for (int X=0; X<Word.size(); X++)
         {
             Reverse[X] = Word[Word.size()-1-X];
         }
         return Reverse;
     } 

14.  bool Vowel_First(string Word)
     {
        char F = Word[0];
        if (F == 'a' || F == 'e' || F == 'i' || F == 'o' || F == 'u')
           return true;
        else
           return false;
     }