ebook img

Introduction to C++ Programming PDF

32 Pages·2009·0.2 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Introduction to C++ Programming

© 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved. 2 Introduction to C++ Programming What’s in a name? that which we call a rose By any other name would smell as sweet. —William Shakespeare When faced with a decision, I always ask, “What would OBJECTIVES be the most fun?” —Peggy Walker In this chapter you’ll learn: ■ To write simple computer programs in C++. “Take some more tea,” the March Hare said to Alice, ■ To write simple input and output statements. very earnestly. “I’ve had nothing yet,” Alice replied ■ To use fundamental types. in an offended tone: “so I ■ Basic computer memory concepts. can’t take more.” “You mean you can’t take less,” said the ■ To use arithmetic operators. Hatter: “it’s very easy to take ■ The precedence of arithmetic operators. more than nothing.” —Lewis Carroll ■ To write simple decision-making statements. High thoughts must have high language. Aristophanes —Aristophanes © Copyright 1992–2006 by Deitel & Associates, Inc. All Rights Reserved. © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved. Chapter 2 Introduction to C++ Programming 3 Assignment Checklist Name: Date: Section: Exercises Assigned: Circle assignments Date Due Prelab Activities Matching YES NO Fill in the Blank 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 Short Answer 26, 27, 28, 29, 30, 31, 32 Programming Output 33, 34, 35, 36, 37, 38, 39 Correct the Code 40, 41, 42, 43, 44, 45 Lab Exercises Exercise 1 — Sum, Average, Product, Smallest YES NO and Largest Follow-Up Questions and Activities 1, 2 Exercise 2 — Multiples YES NO Follow-Up Questions and Activities 1, 2, 3, 4 Exercise 3 — Separating Digits YES NO Follow-Up Questions and Activities 1, 2, 3 Debugging YES NO Labs Provided by Instructor 1. 2. 3. Postlab Activities Coding Exercises 1, 2, 3, 4, 5, 6, 7 Programming Challenges 1, 2, 3 © Copyright 1992–2006 by Deitel & Associates, Inc. All Rights Reserved. © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved. Chapter 2 Introduction to C++ Programming 5 Prelab Activities Matching Name: Date: Section: After reading Chapter 2 of C++ How to Program, Seventh Edition, answer the given questions. These questions are intended to test and reinforce your understanding of key concepts and may be done either before the lab or during the lab. For each term in the column on the left, write the corresponding letter for the description that best matches it from the column on the right. Term Description 1. Integer division a) Holds whole number values. ___ 2. Stream extraction operator b) Outputs a newline and “flushes the output buffer.” ___ 3. return c) Appears at the end of every statement. ___ 4. Modulus operator d) An operation that truncates any fractional part of its result. ___ 5. A variable of type int e) Instruction that is performed before the program is ___ 6. Comments compiled. ___ 7. Stream insertion operator f) Prevents a program from compiling. ___ 8. Preprocessor directive g) An operation that yields the remainder after integer ___ 9. std::endl stream manipulator division. ___ 10. Semicolon h) >>. ___ 11. Conditions in if statements i) One of several means to exit a function. ___ 12. Newline escape sequence j) <<. ___ 13. Syntax error k) '\n'. l) Text that documents programs and improves their readability. m) Commonly formed by using equality operators and relational operators. © Copyright 1992–2006 by Deitel & Associates, Inc. All Rights Reserved. © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved. Chapter 2 Introduction to C++ Programming 7 Prelab Activities Name: Fill in the Blank Fill in the Blank Name: Date: Section: Fill in the blanks in each of the following statements: 14. Both and are ignored by the C++ compiler. 15. A backslash is combined with the next character to form a(n) . 16. A(n) is a location in the computer’s memory where a value can be stored for use by a pro- gram. 17. Output and input in C++ are accomplished with of characters. 18. Single-line comments begin with . 19. is the first function executed in a C++ program. 20. All variables in a C++ program must be before they are used. 21. represents the standard input stream. 22. represents the standard output stream. 23. The statement allows a program to make a decision. 24. C++ evaluates arithmetic expressions in a precise sequence determined by the rules of and associativity. 25. operators and operators are commonly used in if conditions. © Copyright 1992–2006 by Deitel & Associates, Inc. All Rights Reserved. © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved. Chapter 2 Introduction to C++ Programming 9 Prelab Activities Name: Short Answer Short Answer Name: Date: Section: In the space provided, answer each of the given questions. Your answers should be as concise as possible; aim for two or three sentences. 26. What is the difference between stream insertion and stream extraction? What is each used for? 27. What is a syntax error? Give an example. 28. What is a logic error? Give an example. 29. What are operator precedence and associativity? How do they affect program execution? © Copyright 1992–2006 by Deitel & Associates, Inc. All Rights Reserved. © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved. 10 Introduction to C++ Programming Chapter 2 Prelab Activities Name: Short Answer 30. What are redundant parentheses? When might a programmer use them? 31. Write an example of a preprocessor directive. 32. What is a variable? How are they used in computer programs? © Copyright 1992–2006 by Deitel & Associates, Inc. All Rights Reserved. © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved. Chapter 2 Introduction to C++ Programming 11 Prelab Activities Name: Programming Output Programming Output Name: Date: Section: For each of the given program segments, read the code and write the output in the space provided below each program. [Note: Do not execute these programs on a computer.] 33. What is the output of the following program? 1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int x; 7 int y; 8 9 x = 30; 10 y = 2; 11 cout << x * y + 9 / 3 << endl; 12 } // end main Your answer: 34. What is output by the following line of code? 1 cout << ( 8 * 4 * 2 + 6 ) / 2 + 4; Your answer: © Copyright 1992–2006 by Deitel & Associates, Inc. All Rights Reserved. © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved. 12 Introduction to C++ Programming Chapter 2 Prelab Activities Name: Programming Output For Programming Output Exercises 35 and 36, use the program in Fig.L2.1. 1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int input; 7 8 cout << "Please enter an integer: "; 9 cin >> input; 10 11 if ( input != 7 ) 12 cout << "Hello" << endl; 13 14 if ( input == 7 ) 15 cout << "Goodbye" << endl; 16 } // end main Fig. L 2.1 | Program used for Programming Output Exercises 35 and 36. 35. What is output by the program in Fig.L2.1? Assume that the user enters 5 for input. Your answer: 36. What is output by the program in Fig.L2.1? Assume that the user enters 7 for input. Your answer: © Copyright 1992–2006 by Deitel & Associates, Inc. All Rights Reserved. © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved. Chapter 2 Introduction to C++ Programming 13 Prelab Activities Name: Programming Output For Programming Output Exercises 37 and 38, use the program in Fig.L2.2. 1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int input; 7 8 cout << "Please enter an integer: "; 9 cin >> input; 10 11 if ( input >= 0 ) 12 cout << "Hello" << endl; 13 14 cout << "Goodbye" << endl; 15 } // end main Fig. L 2.2 | Program used for Programming Output Exercises 37 and 38. 37. What is output by the program in Fig.L2.2? Assume the user enters 2 for input. Your answer: 38. What is output by the program in Fig.L2.2? Assume the user enters -2 for input. Your answer: © Copyright 1992–2006 by Deitel & Associates, Inc. All Rights Reserved. © 2010 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved. 14 Introduction to C++ Programming Chapter 2 Prelab Activities Name: Programming Output 39. What is output by the following program? 1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int x = 3; 7 int y = 9; 8 int z = 77; 9 10 if ( x == ( y / 3 ) ) 11 cout << "H"; 12 13 if ( z != 77 ) 14 cout << "q"; 15 16 if ( z == 77 ) 17 cout << "e"; 18 19 if ( z * y + x < 0 ) 20 cout << "g"; 21 22 if ( y == ( x * x ) ) 23 cout << "ll"; 24 25 cout << "o!" << endl; 26 } // end main Your answer: © Copyright 1992–2006 by Deitel & Associates, Inc. All Rights Reserved.

Description:
Aristophanes After reading Chapter 2 of C++ How to Program, Seventh Edition, answer the e) Instruction that is performed before the program is.
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.