C++ Learning Community Forum
September 09, 2010, 08:23:12 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 0  (Read 1918 times)
KTC
std::freak
Pseudo-Admin
Dr. of C++ology
*****
Posts: 636



View Profile
« on: July 13, 2007, 03:21:12 AM »

Exercises 0-1
Quote
What does the following statement do?
Code:
3+4;

As the book discussed just before the start of the exercises, this is an expression statement. The program execute the expression for its side effects and discard its result. In this case, the program add 3 to 4, which of course equals 7. As this particular expression have no side effect, the result (7) is then discarded.
« Last Edit: July 13, 2007, 03:31:11 AM 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: 636



View Profile
« Reply #1 on: July 13, 2007, 03:21:46 AM »

Exercises 0-2
Quote
Write a program that, when run, writes
Code:
This (") is a quote, and this (\) is a backslash.

This exercise is design to remind the reader that certain characters have special meaning in a string literal in C++, and that to include such characters, they need to be escaped with a backslash (\) immediately beforehand.

Code
GeSHi (cpp):
#include <iostream>
 
int main()
{
   std::cout << "This (\") is a quote, and this (\\) is a backslash." << std::endl;
 
   return 0;
}
 
Created by GeSHI 1.0.7.18

Reference : A full list of C++ Escape Characters.
« Last Edit: July 13, 2007, 03:37:21 AM 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: 636



View Profile
« Reply #2 on: July 13, 2007, 03:22:20 AM »

Exercises 0-4
Quote
Write a program that, when run, writes the Hello, world! program as its output.

This is more of the same as 0-2, with the addition of tab character which you were asked to play around with in 0-3.

Code
GeSHi (cpp):
#include <iostream>
 
int main()
{
   std::cout << "// a small C++ program" << std::endl;
   std::cout << "#include <iostream>" << std::endl;
   std::cout << '\n';
   std::cout << "int main()" << std::endl;
   std::cout << '{' << std::endl;
   std::cout << "\tstd::cout << \"Hello, World!\" << std::endl;" << std::endl;
   std::cout << "\treturn 0;" << std::endl;
   std::cout << '}' << std::endl;
 
   return 0;
}
 
Created by GeSHI 1.0.7.18

It should be noted that another way to produce the desired output would be to read in the source file of the Hello, World! program, and in turn output it. Though file I/O is not yet covered by the book, here is what it would look like.

Code
GeSHi (cpp):
#include <iostream>
#include <fstream>
#include <string>
 
int main()
{
   // The string literal given is the (relative/absolute) path to the source file to be displayed
   std::ifstream ifs("../0-1/main.cpp");
 
   std::string s;
   while( std::getline(ifs, s) )
   {
       std::cout << s << std::endl;
   }
 
   return 0;
}
 
Created by GeSHI 1.0.7.18
« Last Edit: July 13, 2007, 03:43:24 AM 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: 636



View Profile
« Reply #3 on: July 13, 2007, 03:22:41 AM »

Exercises 0-5
Quote
Is this a valid program? Why or why not?
Code:
#include <iostream>
int main()   std::cout << "Hello, world!" << std::endl;

No, this is not a valid program.

As the book discussed just before the start of the exercises, the body of a function must be enclosed in braces, even if it is only a single statement. Since the main function is a function, the body must be enclosed in braces, it is not here.
« Last Edit: July 13, 2007, 03:48:48 AM 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: 636



View Profile
« Reply #4 on: July 13, 2007, 03:22:52 AM »

Exercises 0-6
Quote
Is this a valid program? Why or why not?
Code:
#include <iostream>
int main()   {{{{{{ std::cout << "Hello, world!" << std::endl; }}}}}}

Yes, this is a valid program.

Aside from the restriction that the body of a function must be enclosed in braces, there are no restriction as to how many level of braces there can be*, and multiple levels is allowed so long as the number of opening braces ({) matches with the number of closing braces (}).

Nitpicker's corner:
* : There would be implementation restriction as to a maximum number, but we don't need to worry about that here.
« Last Edit: July 13, 2007, 03:56:04 AM 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: 636



View Profile
« Reply #5 on: July 13, 2007, 03:23:04 AM »

Exercises 0-7
Quote
What about this one?
Code:
#include <iostream>

int main()
{
    /* This is a comment that extends over several lines
        because it uses /* and */ as its starting and ending delimiters */
    std::cout << "Does this work?" << std::endl;
    return 0;
}

No, this is not a valid program.

Beware of what's call C style comment (/* ... */). While it is free-form and can span multiple lines, it doesn't nest. What this mean is the first time the compiler come across */, the comment is ended.
« Last Edit: July 13, 2007, 03:59:47 AM 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: 636



View Profile
« Reply #6 on: July 13, 2007, 03:23:15 AM »

Exercises 0-8
Quote
...and this one?
Code:
#include <iostream>
int main()
{
    // This is a comment that extends over several lines
    // by using // at the beginning of each line instead of using /*
    // or */ to delimit comments.
    std::cout << "Does this work?" << std::endl;
    return 0;
}

Yes, this is a valid program.

A C++ style comment (// ...) ends at and only at the end of the current line. Everything after the // is considered comment text by the compiler and is thus ignored. That include any characters that would otherwise start or end a C style comment.
« Last Edit: July 13, 2007, 04:03:25 AM 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: 636



View Profile
« Reply #7 on: July 13, 2007, 03:23:27 AM »

Exercises 0-9
Quote
What is the shortest valid program?

Code
GeSHi (cpp):
int main(){}
Created by GeSHI 1.0.7.18

#include directives are not compulsory. Empty parameter list as before. Compulsory braces for the function body can go on the same line due to C++ free-form nature. Return statement omitted allow due to the main function special status where a zero return value is assumed where none have been specified.
« Last Edit: July 13, 2007, 04:09:54 AM 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: 636



View Profile
« Reply #8 on: July 13, 2007, 03:23:37 AM »

Exercises 0-10
Quote
Rewrite the Hello, world! program so that a newline occurs everywhere that whitespace is allowed in the program.

Code
GeSHi (cpp):
// a small C++ program
#include <iostream>
 
int
main
(
)
{
   std::cout
       <<
       "Hello, World!"
       <<
       std::endl
       ;
   return
       0
       ;
}
 
Created by GeSHI 1.0.7.18
« Last Edit: July 13, 2007, 04:11:22 AM 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!