C++ Learning Community Forum
August 01, 2010, 02:58:22 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Chapter 2  (Read 2383 times)
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« on: July 13, 2007, 04:10:45 PM »

Exercise 2-1
Quote
Change the framing program so that it writes its greeting with no separation from the frame.

The whole point of the exercise is to show that the program as rewritten in this chapter made it much more flexible than before such that only one line or two need to be change for it to obtain our desired result.

In this case, we only need to change the initialization value of pad to 0.

Code
GeSHi (cpp):
#include <iostream>
#include <string>
 
// say what standard-library names we use
using std::cin;         using std::endl;
using std::cout;        using std::string;
 
int main()
{
   // ask for the person's name
   cout << "Please enter your first name: ";
 
   // read the name
   string name;
   cin >> name;
 
   // build the message that we intend to write
   const string greeting = "Hello, " + name + "!";
 
   // the number of blanks surrounding the greeting
   const int pad = 0;
 
   // the number of rows and columns to write
   const int rows = pad * 2 + 3;
   const string::size_type cols = greeting.size() + pad * 2 + 2;
 
   // write a blank line to separate the output from the input
   cout << endl;
 
   // write rows rows of output
   // invariant: we have written r rows so far
   for (int r = 0; r != rows; ++r) {
 
       string::size_type c = 0;
 
       // invariant: we have written c characters so far in the current row
       while (c != cols) {
 
           // is it time to write the greeting?
           if (r == pad + 1 && c == pad + 1) {
               cout << greeting;
               c += greeting.size();
           } else {
 
               // are we on the border?
               if (r == 0 || r == rows - 1 ||
                   c == 0 || c == cols - 1)
                   cout << "*";
               else
                   cout << " ";
               ++c;
           }
       }
 
       cout << endl;
   }
 
   return 0;
}
 
Created by GeSHI 1.0.7.18
« Last Edit: July 13, 2007, 04:17:12 PM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #1 on: July 13, 2007, 04:10:54 PM »

Exercise 2-2
Quote
Change the framing program so that it uses a different amount of space to separate the sides from the greeting than it uses to separate the top and bottom borders from the greeting.

In this case, we need to update our desired amount of padding as before, and also the statement where we check whether it's time to write the greeting yet, and the calculation for the number of rows/columns to write.

For example, let there be twice as many padding top & bottom as there are left & right.

Code
GeSHi (cpp):
#include <iostream>
#include <string>
 
// say what standard-library names we use
using std::cin;         using std::endl;
using std::cout;        using std::string;
 
int main()
{
   // ask for the person's name
   cout << "Please enter your first name: ";
 
   // read the name
   string name;
   cin >> name;
 
   // build the message that we intend to write
   const string greeting = "Hello, " + name + "!";
 
   // the number of blanks surrounding the greeting
   const int pad = 2;
 
   // the number of rows and columns to write
   const int rows = pad * 4 + 3;
   const string::size_type cols = greeting.size() + pad * 2 + 2;
 
   // write a blank line to separate the output from the input
   cout << endl;
 
   // write rows rows of output
   // invariant: we have written r rows so far
   for (int r = 0; r != rows; ++r) {
 
       string::size_type c = 0;
 
       // invariant: we have written c characters so far in the current row
       while (c != cols) {
 
           // is it time to write the greeting?
           if (r == pad *2 + 1 && c == pad + 1) {
               cout << greeting;
               c += greeting.size();
           } else {
 
               // are we on the border?
               if (r == 0 || r == rows - 1 ||
                   c == 0 || c == cols - 1)
                   cout << "*";
               else
                   cout << " ";
               ++c;
           }
       }
 
       cout << endl;
   }
 
   return 0;
}
Created by GeSHI 1.0.7.18
« Last Edit: July 13, 2007, 04:29:05 PM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #2 on: July 13, 2007, 04:11:03 PM »

Exercise 2-3
Quote
Rewrite the framing program to ask the user to supply the amount of spacing to leave between the frame and the greeting.

Code
GeSHi (cpp):
#include <iostream>
#include <string>
 
// say what standard-library names we use
using std::cin;         using std::endl;
using std::cout;        using std::string;
 
int main()
{
   // ask for the person's name
   cout << "Please enter your first name: ";
 
   // read the name
   string name;
   cin >> name;
 
   // build the message that we intend to write
   const string greeting = "Hello, " + name + "!";
 
   // the number of blanks surrounding the greeting
   int pad;
   cout << "Please enter the amount of spacing: ";
   cin >> pad;
 
   // the number of rows and columns to write
   const int rows = pad * 2 + 3;
   const string::size_type cols = greeting.size() + pad * 2 + 2;
 
   // write a blank line to separate the output from the input
   cout << endl;
 
   // write rows rows of output
   // invariant: we have written r rows so far
   for (int r = 0; r != rows; ++r) {
 
       string::size_type c = 0;
 
       // invariant: we have written c characters so far in the current row
       while (c != cols) {
 
           // is it time to write the greeting?
           if (r == pad + 1 && c == pad + 1) {
               cout << greeting;
               c += greeting.size();
           } else {
 
               // are we on the border?
               if (r == 0 || r == rows - 1 ||
                   c == 0 || c == cols - 1)
                   cout << "*";
               else
                   cout << " ";
               ++c;
           }
       }
 
       cout << endl;
   }
 
   return 0;
}
Created by GeSHI 1.0.7.18
« Last Edit: July 13, 2007, 04:30:11 PM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #3 on: July 13, 2007, 04:11:13 PM »

Exercise 2-4
Quote
The framing program writes the mostly blank lines that separate the borders from the greeting one character at a time. Change the program so that it writes all the spaces needed in a single output expression.

Code
GeSHi (cpp):
#include <iostream>
#include <string>
 
// say what standard-library names we use
using std::cin;         using std::endl;
using std::cout;        using std::string;
 
int main()
{
   // ask for the person's name
   cout << "Please enter your first name: ";
 
   // read the name
   string name;
   cin >> name;
 
   // build the message that we intend to write
   const string greeting = "Hello, " + name + "!";
 
   // the number of blanks surrounding the greeting
   const int pad = 1;
 
   // the number of rows and columns to write
   const int rows = pad * 2 + 3;
   const string::size_type cols = greeting.size() + pad * 2 + 2;
   const string spaces(cols - 2, ' ');
 
   // write a blank line to separate the output from the input
   cout << endl;
 
   // write rows rows of output
   // invariant: we have written r rows so far
   for (int r = 0; r != rows; ++r) {
 
       string::size_type c = 0;
 
       // invariant: we have written c characters so far in the current row
       while (c != cols) {
 
           // is it time to write the greeting?
           if (r == pad + 1 && c == pad + 1) {
               cout << greeting;
               c += greeting.size();
           } else if (r != 0 && r != rows - 1 && r != pad + 1 &&
                      c != 0 && c != cols - 1) {
               cout << spaces;
               c += spaces.size();
           } else {
               // are we on the border?
               if (r == 0 || r == rows - 1 ||
                   c == 0 || c == cols - 1)
                   cout << "*";
               else
                   cout << " ";
               ++c;
           }
       }
 
       cout << endl;
   }
 
   return 0;
}
Created by GeSHI 1.0.7.18
« Last Edit: July 13, 2007, 04:47:19 PM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #4 on: July 13, 2007, 04:11:21 PM »

Exercise 2-5
Quote
Write a set of "*" characters so that they form a square, a rectangle, and a triangle.

Code
GeSHi (cpp):
#include <iostream>
 
// say what standard-library names we use
using std::cin;         using std::endl;
using std::cout;        using std::string;
 
int main()
{
   // the number of rowsto write
   const int rows = 5;
 
   // ** Square **
 
   // write rows rows of output
   // invariant: we have written r rows so far
   for (int r = 0; r != rows; ++r) {
 
       // Square: rows == columns
       const int cols = rows;
 
       // invariant: we have written c columns so far
       for(int c = 0; c != cols; ++c) {
           cout << "*";
       }
 
       cout << endl;
   }
 
   // Separate the shapes
   cout << endl;
 
   // ** Rectangle **
 
   // write rows rows of output
   // invariant: we have written r rows so far
   for (int r = 0; r != rows; ++r) {
 
       // Let's have a rectangle twice as wide as it is high
       const int cols = rows * 2;
 
       // invariant: we have written c columns so far
       for(int c = 0; c != cols; ++c) {
           cout << "*";
       }
 
       cout << endl;
   }
 
   // Separate the shapes
   cout << endl;
 
   // ** Triangle **
 
   // write rows rows of output
   // invariant: we have written r rows so far
   for (int r = 0; r != rows; ++r) {
       // write rows columns of output to form triangle
       // we are writing the r + 1 rows
       // invariant: we have written c columns so far
       for(int c = 0; c != r + 1; ++c) {
           cout << "*";
       }
 
       cout << endl;
   }
 
   return 0;
}
Created by GeSHI 1.0.7.18
« Last Edit: July 13, 2007, 05:00:35 PM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #5 on: July 13, 2007, 04:11:31 PM »

Exercise 2-6
Quote
What does the following code do?
Code:
int i = 0;
while (i < 10) {
    i += 1;
    std::cout << i << std::endl;
}

Noting the increment to i is before the output, we can conclude the program code output the number 1 to 10, each on its own line.
« Last Edit: July 13, 2007, 05:02:47 PM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #6 on: July 13, 2007, 04:11:40 PM »

Exercise 2-7
Quote
Write a program to count down from 10 to -5 .

Code
GeSHi (cpp):
#include <iostream>
 
using std::cout;    using std::endl;
 
int main()
{
   // Starting from 10, we count down until we reach the first number we don't want
   // namely, -6
   for(int i = 10; i != -6; --i) {
       std::cout << i << std::endl;
   }
 
   return 0;
}
Created by GeSHI 1.0.7.18
« Last Edit: July 13, 2007, 05:06:50 PM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #7 on: July 13, 2007, 04:11:49 PM »

Exercise 2-8
Quote
Write a program to generate the product of the numbers in the range [1, 10).

This is a program I'm sure the book will return to. The product of the numbers in the range [1, N) is the mathematical operation N-1 factorial, or (N-1)!. Factorial is a classical textbook introduction to the concept of recursion when writing our own functions have been introduced.

Code
GeSHi (cpp):
#include <iostream>
 
using std::cout;    using std::endl;
 
int main()
{
   const int N = 10;
 
   // note we start the product off at 1 as we recall anything multiple by 0 is 0
   int product = 1;
 
   // invariant: we have multipled by the number in the range [1, i)
   for(int i=1; i != N; ++i) {
       product *= i;
   }
 
   cout << product << endl;
 
   return 0;
}
Created by GeSHI 1.0.7.18
« Last Edit: July 13, 2007, 05:15:46 PM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #8 on: July 13, 2007, 04:11:59 PM »

Exercise 2-9
Quote
Write a program that asks the user to enter two numbers and tells the user which number is larger than the other.

Code
GeSHi (cpp):
#include <iostream>
 
using std::cin;     using std::cout;
using std::endl;
 
int main()
{
   cout << "The first number: ";
   int num1;
   cin >> num1;
 
   cout << "The second number: ";
   int num2;
   cin >> num2;
 
   cout << "The larger number is " << ((num1 > num2) ? num1 : num2) << endl;
 
   return 0;
}
Created by GeSHI 1.0.7.18
« Last Edit: July 13, 2007, 05:22:38 PM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 635



View Profile
« Reply #9 on: July 13, 2007, 04:12:09 PM »

Exercise 2-10
Quote
Explain each of the uses of std:: in the following program:
Code:
int main() {
int k =  0;
    while (k != n) {             // invariant: we have written k asterisks so far
        using std::cout;
        cout << "*";
        ++k;
    }
    std::cout << std::endl;      // std:: is required here
    return 0;
}

Code:
using std::cout;
This is a using declaration. We declare cout to be from the std namespace from this point on, until the end of the current scope, in this case, the end of the while loop.

Code:
std::cout << std::endl;      // std:: is required here
In both case, the :: scope resolution operator is used and std is specified to say we want cout and endl from the std namespace.
« Last Edit: July 13, 2007, 05:29:40 PM by KTC » Logged

A young man came to interview a bank president,

"Tell me sir, how did you become successful?"
"Two words."
"And what are they, Sir?"
"Right decisions."
"How do you make right decisions?"
"One word... experience."
"How do you get experience?"
"Two words."
"And what are they?"
"Wrong decisions."
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!