KTC
std::freak
Pseudo-Admin
Dr. of C++ology
    
Posts: 636
|
 |
« on: July 13, 2007, 03:21:12 AM » |
|
Exercises 0-1What does the following statement do? 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
|
 |
« Reply #1 on: July 13, 2007, 03:21:46 AM » |
|
Exercises 0-2Write a program that, when run, writes 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. 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
|
 |
« Reply #2 on: July 13, 2007, 03:22:20 AM » |
|
Exercises 0-4Write 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. 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. 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
|
 |
« Reply #3 on: July 13, 2007, 03:22:41 AM » |
|
Exercises 0-5Is this a valid program? Why or why not? #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
|
 |
« Reply #4 on: July 13, 2007, 03:22:52 AM » |
|
Exercises 0-6Is this a valid program? Why or why not? #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
|
 |
« Reply #5 on: July 13, 2007, 03:23:04 AM » |
|
Exercises 0-7What about this one? #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
|
 |
« Reply #6 on: July 13, 2007, 03:23:15 AM » |
|
Exercises 0-8...and this one? #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
|
 |
« Reply #7 on: July 13, 2007, 03:23:27 AM » |
|
Exercises 0-9What is the shortest valid program? 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
|
 |
« Reply #8 on: July 13, 2007, 03:23:37 AM » |
|
Exercises 0-10Rewrite the Hello, world! program so that a newline occurs everywhere that whitespace is allowed in the program. 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."
|
|
|
|