📄 apd.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><!-- This document was created from RTF source by rtftohtml version 3.0.1 --> <META NAME="GENERATOR" Content="Symantec Visual Page 1.0"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"> <TITLE>Teach Yourself C++ in 21 Days</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1></H1><H2 ALIGN="CENTER"><A HREF="apc.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/apc.htm"><IMG SRC="BLANPREV.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANPREV.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="tppmsgs/msgs0.htm#1" tppabs="http://www.mcp.com/sams"><IMGSRC="BLANHOME.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"BORDER="0"></A><A HREF="index.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/index.htm"><IMG SRC="BLANTOC.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANTOC.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><BR><BR><BR><A NAME="Heading1"></A><FONT COLOR="#000077">Appendix D<BR><BR>Answers</FONT></H2><H2 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H2><H3><A NAME="Heading3"></A><FONT COLOR="#000077">Day 1</FONT></H3><H4 ALIGN="CENTER"><A NAME="Heading4"></A><FONT COLOR="#000077">Quiz</FONT></H4><DL> <DD><B>1. What is the difference between interpreters and compilers?<BR> </B><BR> Interpreters read through source code and translate a program, turning the programmer's code, or program instructions, directly into actions. Compilers translate source code into an executable program that can be run at a later time.<BR> <B><BR> 2. How do you compile the source code with your compiler?</B><BR> <BR> Every compiler is different. Be sure to check the documentation that came with your compiler.<BR> <B><BR> 3. What does the linker do?</B><BR> <BR> The linker's job is to tie together your compiled code with the libraries supplied by your compiler vendor and other sources. The linker lets you build your program in pieces and then link together the pieces into one big program.<BR> <B><BR> 4. What are the steps in the development cycle?</B><BR> <BR> Edit source code, compile, link, test, repeat.</DL><H4 ALIGN="CENTER"><A NAME="Heading5"></A><FONT COLOR="#000077">Exercises</FONT></H4><DL> <DD><B>1.</B> Initializes two integer variables and then prints out their sum and their product.<BR> <B><BR> 2.</B> See your compiler manual.<BR> <B><BR> 3.</B> You must put a <TT>#</TT> symbol before the word <TT>include</TT> on the first line.<BR> <B><BR> 4. </B>This program prints the words <TT>Hello World</TT> to the screen, followed by a new line (carriage return).</DL><H3><A NAME="Heading6"></A><FONT COLOR="#000077">Day 2</FONT></H3><H4 ALIGN="CENTER"><A NAME="Heading7"></A><FONT COLOR="#000077"><B>Quiz</B></FONT></H4><DL> <DD><B>1. What is the difference between the compiler and the preprocessor?</B><BR> <BR> Each time you run your compiler, the preprocessor runs first. It reads through your source code and includes the files you've asked for, and performs other housekeeping chores. The preprocessor is discussed in detail on Day 18, "Object-Oriented Analysis and Design."<BR> <BR> <B>2. Why is the function <TT>main()</TT> special?</B><BR> <TT><BR> main()</TT> is called automatically, each time your program is executed.<BR> <BR> <B>3. What are the two types of comments, and how do they differ?</B><BR> <BR> C++-style comments are two slashes (<TT>//</TT>), and they comment out any text until the end of the line. C-style comments come in pairs (<TT>/* */</TT>), and everything between the matching pairs is commented out. You must be careful to ensure you have matched pairs.<BR> <BR> <B>4. Can comments be nested?</B><BR> <BR> Yes, C++-style comments can be nested within C-style comments. You can, in fact, nest C-style comments within C++-style comments, as long as you remember that the C++-style comments end at the end of the line.<BR> <B>5. Can comments be longer than one line?<BR> </B><BR> C-style comments can. If you want to extend C++-style comments to a second line, you must put another set of double slashes (<TT>//</TT>).</DL><H4 ALIGN="CENTER"><A NAME="Heading8"></A><FONT COLOR="#000077">Exercises</FONT></H4><DL> <DD><B>1.</B> Write a program that writes <TT>I love C++</TT> to the screen.</DL><PRE><FONT COLOR="#0066FF">1: #include <iostream.h>2:3: int main()4: {5: cout << "I love C++\n";6: return 0;7: }</FONT></PRE><DL> <DD><B>2.</B> Write the smallest program that can be compiled, linked, and run.</DL><PRE><FONT COLOR="#0066FF">int main(){}</FONT></PRE><DL> <DD><B>3</B>. BUG BUSTERS: Enter this program and compile it. Why does it fail? How can you fix it?</DL><PRE><FONT COLOR="#0066FF">1: #include <iostream.h>2: main()3: {4: cout << Is there a bug here?";5: }</FONT></PRE><DL> <DD>Line 4 is missing an opening quote for the string.<BR> <BR> <B><BR> 4.</B> Fix the bug in Exercise 3 and recompile, link, and run it.</DL><PRE><FONT COLOR="#0066FF">1: #include <iostream.h>2: main()3: {4: cout << "Is there a bug here?";5: }</FONT></PRE><H3><A NAME="Heading9"></A><FONT COLOR="#000077">Day 3</FONT></H3><H4 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">Quiz</FONT></H4><DL> <DD><B>1. What is the difference between an integral variable and a floating-point variable?<BR> </B><BR> Integer variables are whole numbers; floating-point variables are "reals" and have a "floating" decimal point. Floating-point numbers can be represented using a mantissa and an exponent.<BR> <BR> <B>2. What are the differences between an <TT>unsigned</TT> <TT>short</TT> <TT>int</TT> and a <TT>long</TT> <TT>int</TT>?</B><BR> <BR> The keyword <TT>unsigned</TT> means that the integer will hold only positive numbers. On most computers, short integers are 2 bytes and long integers are 4.<BR> <BR> <B>3. What are the advantages of using a symbolic constant rather than a literal?</B><BR> <BR> A symbolic constant explains itself; the name of the constant tells what it is for. Also, symbolic constants can be redefined at one location in the source code, rather than the programmer having to edit the code everywhere the literal is used.<BR> <BR> <B>4. What are the advantages of using the <TT>const</TT> keyword rather than <TT>#define</TT>?</B><BR> <TT><BR> const</TT> variables are "typed;" thus the compiler can check for errors in how they are used. Also, they survive the preprocessor; thus the name is available in the debugger.<BR> <BR> <B>5. What makes for a good or bad variable name?</B><BR> <BR> A good variable name tells you what the variable is for; a bad variable name has no information. <TT>myAge</TT> and <TT>PeopleOnTheBus</TT> are good variable names, but <TT>xjk</TT> and <TT>prndl</TT> are probably less useful.<BR> <BR> <B>6. Given this <TT>enum</TT>, what is the value of <TT>Blue</TT>?</B></DL><PRE><FONT COLOR="#0066FF">enum COLOR { WHITE, BLACK = 100, RED, BLUE, GREEN = 300 };BLUE = 102</FONT></PRE><DL> <DD><B>7. Which of the following variable names are good, which are bad, and which are invalid?</B> <DL> <DD><B><BR> a. <TT>Age</TT></B><BR> Good<BR> <BR> <B>b. <TT>!ex</TT></B><BR> Not legal<BR> <BR> <B>c. <TT>R79J</TT></B><BR> Legal, but a bad choice<BR> <BR> <B>d. <TT>TotalIncome</TT></B><BR> Good<BR> <BR> <B>e. <TT>__Invalid</TT></B><TT><BR> </TT>Legal, but a bad choice </DL></DL><H4 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">Exercises</FONT></H4><DL> <DD><B>1. </B>What would be the correct variable type in which to store the following information? <DL> <DD><B><BR> a. </B>Your age.<BR> <TT>Unsigned short</TT> integer<BR> <BR> <B>b. </B>The area of your backyard.<BR> <TT>Unsigned long</TT> integer or <TT>unsigned float</TT><BR> <BR> <B>c. </B>The number of stars in the galaxy.<BR> <TT>Unsigned double</TT><BR> <BR> <B>d. </B>The average rainfall for the month of January.<BR> <TT>Unsigned short</TT> integer </DL> <DD><BR> <B>2.</B> Create good variable names for this information. <DL> <DD><B><BR> a</B>. <TT>myAge</TT><BR> <BR> <B>b</B>. <TT>backYardArea</TT><BR> <BR> <B>c</B>. <TT>StarsInGalaxy</TT><BR> <BR> <B>d</B>. <TT>averageRainFall</TT> </DL> <DD><BR> <B>3.</B> Declare a constant for pi as 3.14159.</DL><BLOCKQUOTE> <PRE><FONT COLOR="#0066FF">const float PI = 3.14159;</FONT></PRE></BLOCKQUOTE><PRE><FONT COLOR="#0066FF"></FONT></PRE><DL> <DD><B>4.</B> Declare a <TT>float</TT> variable and initialize it using your pi constant.</DL><BLOCKQUOTE> <PRE><FONT COLOR="#0066FF">float myPi = PI;</FONT></PRE></BLOCKQUOTE><PRE><FONT COLOR="#0066FF"></FONT></PRE><H3><A NAME="Heading12"></A><FONT COLOR="#000077">Day 4</FONT></H3><H4 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">Quiz</FONT></H4><DL> <DD><B>1. What is an expression?</B><BR> <BR> Any statement that returns a value.<BR> <BR> <B>2. Is <TT>x = 5 + 7</TT> an expression? What is its value?</B><BR> <BR> Yes. <TT>12</TT><BR> <BR> <B>3. What is the value of <TT>201 / 4</TT>?</B><BR> <TT><BR> 50</TT><BR> <BR> <B>4. What is the value of <TT>201 % 4</TT>?</B><BR> <TT><BR> 1</TT><BR> <BR> <B>5. If <TT>myAge</TT>, <TT>a</TT>, and <TT>b</TT> are all <TT>int</TT> variables, what are their values after:</B></DL><PRE><FONT COLOR="#0066FF">myAge = 39;a = myAge++;b = ++myAge;myAge: 41, a: 39, b: 41</FONT></PRE><DL> <DD><B>6. What is the value of <TT>8+2*3</TT>?</B></DL><PRE><FONT COLOR="#0066FF">14</FONT></PRE><DL> <DD><FONT COLOR="#0066FF"></FONT> <P><B>7. What is the difference between <TT>if(x = 3)</TT> and <TT>if(x == 3)</TT>?</B><BR> <BR> The first one assigns <TT>3</TT> to <TT>x</TT> and returns true. The second one tests whether <TT>x</TT> is equal to <TT>3</TT>; it returns true if the value of <TT>x</TT> is equal to 3 and false if it is not.<BR> <BR> <B>8. Do the following values evaluate to <TT>TRUE</TT> or <TT>FALSE</TT>?</B> <DL> <DD><B>a. 0</B><TT><BR> FALSE</TT><BR> <B><BR> b. <TT>1</TT></B><BR> <TT>TRUE</TT> </DL> <DD> <DL> <DD><B>c. <TT>-1</TT></B><BR> <TT>TRUE</TT> </DL> <DD> <DL> <DD><B>d. <TT>x = 0</TT></B><BR> <TT>FALSE</TT> </DL> <DD> <DL> <DD><B>e. <TT>x == 0 // assume that x has the value of 0</TT></B><TT><BR> TRUE</TT> </DL></DL><H4 ALIGN="CENTER"><A NAME="Heading14"></A><FONT COLOR="#000077">Exercises</FONT></H4><DL> <DD><B>1.</B> Write a single <TT>if</TT> statement that examines two integer variables and changes the larger to the smaller, using only one <TT>else</TT> clause.</DL><PRE><FONT COLOR="#0066FF">if (x > y) x = y;else // y > x || y == x y = x;</FONT></PRE><DL> <DD><B>2. </B>Examine the following program. Imagine entering three numbers, and write what output you expect.</DL><PRE><FONT COLOR="#0066FF">1: #include <iostream.h>2: int main()3: {4: int a, b, c;5: cout << "Please enter three numbers\n";6: cout << "a: ";7: cin >> a;8: cout << "\nb: ";9: cin >> b;10: cout << "\nc: ";11: cin >> c;12:13: if (c = (a-b))14 {15: cout << "a: " << a << " minus b: ";16: cout << b << " _equals c: " << c;17: }15: else16: cout << "a-b does not equal c: ";17: return 0;18: }</FONT></PRE><DL> <DD><B>3.</B> Enter the program from Exercise 2; compile, link, and run it. Enter the numbers <TT>20</TT>, <TT>10</TT>, and <TT>50</TT>. Did you get the output you expected? Why not?<BR> <BR> Enter <TT>20</TT>, <TT>10</TT>, <TT>50</TT>.<BR> <BR> Get back <TT>a</TT>: <TT>20</TT> <TT>b:</TT> <TT>30</TT> <TT>c:</TT> <TT>10</TT>.<BR> <BR> Line 13 is assigning, not testing for equality.<BR> <B><BR> 4.</B> Examine this program and anticipate the output:</DL><PRE><FONT COLOR="#0066FF">1: #include <iostream.h>2: int main()3: {4: int a = 2, b = 2, c;5: if (c = (a-b))6: cout << "The value of c is: " << c;7: return 0;</FONT></PRE><PRE><FONT COLOR="#0066FF">8: }</FONT></PRE><DL> <DD><B>5.</B> Enter, compile, link, and run the program from Exercise 4. What was the output? Why?<BR> <BR> Because line 5 is assigning the value of <TT>a-b</TT> to <TT>c</TT>, the value of the assignment is <TT>a</TT> (1) minus <TT>b</TT> (1), or <TT>0</TT>. Because <TT>0</TT> is evaluated as <TT>FALSE</TT>, the <TT>if</TT> fails and nothing is printed.</DL><H3 ALIGN="CENTER"></H3><H3><A NAME="Heading15"></A><FONT COLOR="#000077">Day 5</FONT></H3><H4 ALIGN="CENTER"><A NAME="Heading16"></A><FONT COLOR="#000077">Quiz</FONT></H4><DL> <DD><B>1. What are the differences between the function prototype and the function defi-nition?</B><BR> <BR> The function prototype declares the function; the definition defines it. The prototype ends with a semicolon; the definition need not. The declaration can include the keyword <TT>inline</TT> and default values for the parameters; the definition cannot. The declaration need not include names for the parameters; the definition must.<BR> <B><BR> 2. Do the names of parameters have to agree in the prototype, definition, and call to the function?</B><BR> <BR> No. All parameters are identified by position, not name.<BR> <BR> <B>3. If a function doesn't return a value, how do you declare the function?</B><BR> <BR> Declare the function to return <TT>void</TT>.<BR> <BR> <B>4. If you don't declare a return value, what type of return value is assumed?</B><BR> <BR> Any function that does not explicitly declare a return type returns <TT>int</TT>.<BR> <BR> <B>5. What is a local variable?</B><BR> <BR> A local variable is a variable passed into or declared within a block, typically a function. It is visible only within the block.<BR> <BR> <B>6. What is scope?</B><BR> <BR> Scope refers to the visibility and lifetime of local and global variables. Scope is usually established by a set of braces.<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -