Programming Languages Chapter 2 Programming From Problem Analysis Program Design Eighth Edition Basic Elements Guide

subject Type Homework Help
subject Pages 10
subject Words 2821
subject Authors D. S. Malik

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
page-pf1
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 2-1
Chapter 2
Basic Elements of C++
A Guide to this Instructor’s Manual:
We have designed this Instructor’s Manual to supplement and enhance your teaching
experience through classroom activities and a cohesive chapter summary.
At a Glance
Instructor’s Manual Table of Contents
Overview
Objectives
Teaching Tips
Quick Quizzes
Class Discussion Topics
Additional Projects
Additional Resources
Key Terms
page-pf2
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 2-2
Lecture Notes
Overview
Chapter 2 teaches your students the basics of C++. Learning a programming language is
similar to learning to be a chef or learning to play a musical instrument. All three
require direct interaction with the tools; in other words, you cannot become proficient
by simply reading books on the topics. In this chapter, your students will begin
acquiring a fundamental knowledge of C++ by learning about data types, functions,
identifiers, assignment statements, arithmetic operations, and input/output operations.
They will then write and test programs using these concepts to verify their knowledge
of the material.
Objectives
In this chapter, the student will:
Become familiar with the basic components of a C++ program, including functions,
special symbols, and identifiers
Explore simple data types
Discover how to use arithmetic operators
Examine how a program evaluates arithmetic expressions
Become familiar with the string data type
Learn what an assignment statement is and what it does
Learn about variable declaration
Discover how to input data into memory using input statements
Become familiar with the use of increment and decrement operators
Examine ways to output results using output statements
Learn how to use preprocessor directives and why they are necessary
Learn how to debug syntax errors
Explore how to properly structure a program, including using comments to document a
program
Become familiar with compound statements
Learn how to write a C++ program
Teaching Tips
Introduction
1. Define the terms computer program and programming.
2. Use the recipe analogy to give students an idea of the process of programming.
page-pf3
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 2-3
A Quick Look at a C++ Program
1. Note that every C++ program must have a function called main. Use Example 2-1 to
illustrate a basic main function. Walk through this example and point out the meaning
of each line.
2. Discuss the purpose of an output statement and what it produces.
3. Point out the use of comments.
4. Briefly introduce the #include directive.
5. Use Figure 2-1 to describe the various parts of a C++ program.
6. Use Figures 2-2 and 2-3 to describe how memory is allocated and used to store values.
The Basics of a C++ Program
2. Define the terms syntax rules and semantic rules as they relate to a programming
language and explain the difference between the two.
Teaching
Tip
Emphasize that compilers check for syntax but not semantic errors. Give an
example of each type of error.
Comments
2. Describe the two forms of comments shown in the textbook.
Teaching
Tip
Reassure students that although most of this example probably looks confusing,
they will soon understand it and be comfortable with it.
page-pf4
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 2-4
Teaching
Tip
The importance of documenting a program cannot be underestimated. It is highly
important for ensuring that the next programmer to be responsible for
maintaining the code will be able to understand what the code is supposed to do.
Special Symbols
Reserved Words (Keywords)
1. Discuss the word symbols, or keywords, used in C++, using Appendix A as a guide.
Identifiers
1. Define the term identifier as a name for something, such as a variable, constant, or
3. Use Table 2-1 to review the rules of identifier naming.
Teaching
Tip
Discuss the difference between C++ conventions and rules. For example, it is a
rule that a mathematical symbol cannot be used in an identifier name. However,
it is a convention to begin an identifier with a lowercase letter.
Whitespaces
1. Explain that whitespaces (which include blanks, tabs, and newline characters) are used
to separate special symbols, reserved words, and identifiers.
page-pf5
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 2-5
Data Types
1. Explain that C++ categorizes data into different types in order to manipulate the data in
a program correctly. Although it may seem cumbersome at first to be so type-conscious,
emphasize that C++ has these built-in checks to guard against errors.
Teaching
Tip
Explain that C++ is called a strongly typed language because it checks for
operations between inconsistent data types. This results in more robust and error-
free programs. Demonstrate how C++ checks for data types with a simple
program that attempts to add a string and a numeric value.
2. Define the term data type as a set of values together with a set of operations.
3. Mention that C++ data types fall into three categories: simple data types, structured data
types, and pointers. Only the first type is discussed in this chapter.
Simple Data Types
1. Describe the three categories of simple data types in C++: integral, floating-point, and
enumeration.
4. Explain the purpose of the bool data type.
5. Discuss the char data type, including its primary uses. Mention commonly used ASCII
6. Use Table 2-2 to summarize the three simple data types. Point out the difference in the
amount of memory storage required, but inform students that this is system-dependent.
Floating-Point Data Types
1. Use Table 2-3 to explain how C++ represents real, or floating-point, numbers. Mention
2. Define the terms precision, single precision, and double precision.
page-pf6
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 2-6
Teaching
Tip
Demonstrate how to find the values of float and double on a particular
system by running the program with the header file <cfloat> in Appendix F.
Encourage students to try running this program on their own computers and
comparing the results.
Quick Quiz 1
1. What is an enumeration type?
2. The maximum number of significant digits in a number is called the
____________________.
3. The data type ____________________ has only two values: true and false.
4. The ____________________ data type, the smallest integral data type, is used to
represent characters.
Answer: char
Data Types and Variables, and Assignment Statements
2. Explain the concept and syntax of an assignment.
Arithmetic Operators, Operator Precedence, and Expressions
1. Discuss the five arithmetic operators in C++ that are used to manipulate integral and
2. Define the terms unary and binary operators, and discuss the difference between them.
Order of Precedence
page-pf7
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 2-7
Expressions
1. This section discusses integral and floating-point expressions in detail.
2. Describe the three types of arithmetic expressions in C++.
Mixed Expressions
1. Discuss the two rules for evaluating mixed expressions and illustrate these rules in
practice using Example 2-8.
Quick Quiz 2
1. A(n) ____________________ operator has only one operand.
2. You can use ____________________ to override the order of precedence rules.
3. Describe the associativity of arithmetic operators.
4. An expression that has operands of different data types is called a(n)
Type Conversion (Casting)
1. Explain how C++ avoids the hazards that result from implicit type coercion, which
occurs when one data type is automatically changed into another data type.
2. Illustrate the form of the C++ cast operator using Example 2-9.
Teaching
Tip
Students may feel a bit overwhelmed after the discussion of the static_cast
operator. Ask them to run the program from Example 2.9. They should
experiment with removing the static_cast operator from various statements,
as well as changing the variable values. Ask them to report on any unpredictable
results.
page-pf8
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 2-8
string Type
1. Introduce the C++ data type string, which is a programmer-defined data type
available in the C++ library. Define a string as a sequence of zero or more characters.
Teaching
Tip
Emphasize that the first position of a character in a string is 0, not 1. This will be
helpful when manipulating both strings and arrays later on the text.
Variables, Assignment Statements, and Input Statements
1. Explain that data for a C++ program must be input into main memory. Mention the two-
step process to store data in the computer’s memory.
Allocating Memory with Constants and Variables
1. Emphasize that when allocating memory, the programmer must instruct the computer
2. Define the term named constant and describe the syntax for declaring a named constant.
4. Give a formal definition of a simple data type.
Putting Data into Variables
Assignment Statement
1. Discuss the C++ assignment statement, including its syntax, variable initialization, and
the associativity rules of the assignment operator.
page-pf9
3. Use Example 2-14 to discuss the importance of doing a walk-through (tracing values
through a sequence of steps) when writing code.
Teaching
Tip
Building a table showing the values of variables at each step of the program is
very helpful for students to understand the nature of variables.
Saving and Using the Value of an Expression
1. Explain the steps involved in saving the value of an expression using Example 2-15.
Declaring and Initializing Variables
2. Emphasize that it is a good practice to initialize variables while they are being declared.
Use one or more examples to illustrate how to do this in C++.
Input (Read) Statement
1. This section teaches your students to read data into variables from a standard input
Variable Initialization
1. Reiterate that a variable can be initialized either through an assignment statement or a
read statement. Explain why the read statement option is more versatile. Use Example
2-19 to illustrate both types of initialization.
Teaching
Tip
Programmers (and instructors) have various approaches or preferences regarding
variable declaration and initialization. Share your views on the topic. Do you
think the best approach is to always initialize variables for consistency? Do you
prefer initializing variables directly before the block of code that uses them, or
initializing them during declaration?
Quick Quiz 3
1. What is a named constant?
page-pfa
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 2-10
© 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a
Answer: A memory location whose content is not allowed to change during program
execution
2. What is the syntax for declaring single or multiple variables?
3. True or False: If you refer to an identifier without declaring it, the compiler will
generate an error message.
4. A variable is said to be ____________________ the first time a value is placed in it.
Increment and Decrement Operators
1. Explain the purpose of the C++ increment (++) and decrement (--) operators.
2. Discuss how pre and post versions of these operators affect the results in a program.
Use Example 2-20 to help explain the difference between these versions.
Teaching
Tip
Verify that students are comfortable with using pre- and post-
increment/decrement operators correctly, as it will be useful when working with
control structures.
Output
1. Review how the C++ output statement is coded with the cout object and stream
insertion operator (<<). Review the role of the endl manipulator in output statements
as well.
2. Discuss the use of escape characters (see Table 2-4), such as the newline character, to
format output. Demonstrate how to format output with Examples 2-21 through 2-26.
Teaching
Tip
Outputting strings can be confusing at first. Talk about the various methods to
deal with several lines of string output, and give your opinion as to the best
approach. Emphasize that the Enter key cannot be used to break up a long string
into two lines.
page-pfb
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 2-11
Preprocessor Directives
1. Explain the role of the preprocessor in C++. Discuss the use of header files and the
syntax for including them in a C++ program.
Teaching
Tip
Show your students some of the available operations in the <cmath> header.
Here is one Web site with a description:
http://www.cplusplus.com/reference/cmath/
namespace and Using cin and cout in a Program
1. Briefly explain the purpose of the namespace mechanism in ANSI/ISO Standard
2. Review the using namespace std; statement and its usefulness in programs
using cin and cout statements.
Using the string Data Type in a Program
Creating a C++ Program
1. Discuss the role of the function main in a C++ program. Go over the syntax of a main
function, including declaration, assignment, executable, and return statements. Mention
that named constant definitions and preprocessor directives are written before the main
function.
2. Spend some time stepping through Examples 2-27 through 2-29. Verify that students
Debugging: Understanding and Fixing Syntax Errors
1. Review the sample program on page 84 and the compiler output that is generated when
compiling the program. Walk through the various syntax errors and explain how each
one should be fixed. Note that a syntax error on one line may be the cause of a compiler
error on the following line.
page-pfc
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 2-12
Teaching
Tip
Debugging is one of the most important skills a professional programmer
acquires. Stress to students the importance of learning how to debug their own
programs, including the verification of the results.
Program Style and Form
1. This section covers basic C++ syntax and semantic rules. Review these rules with your
students in order for them to feel comfortable writing a complete functioning C++
program.
Syntax
1. Remind students that syntax rules define what is legal and what is not.
2. Discuss some common syntax errors. Emphasize that syntax errors should be corrected
Use of Blanks
1. Discuss when blanks should and should not be used in a C++ program.
Use of Semicolons, Brackets, and Commas
1. Explain the purpose and meaning of semicolons, brackets, and commas in C++
programs. Define the term statement terminator.
Semantics
1. Define the term semantics.
2. Reiterate that a program may run without compiler errors and still have semantic errors
that generate incorrect results. Use the example in the text to illustrate.
Naming Identifiers
1. Mention the conventions for naming identifiers, including self-documenting identifiers
and run-together words.
Prompt Lines
page-pfd
2. Explain why well-written prompt lines are essential for user input.
Documentation
1. Discuss why documenting a program through comments is critical to understanding and
Form and Style
1. Explain the purpose behind formatting and indentation conventions in source code. Use
Example 2-30 to illustrate.
Teaching
Tip
As with naming conventions, discuss your own preferences in terms of form and
style in programming. Use the programming examples at the end of the chapter
to talk about various stylistic elements. Discuss the value of the “art” of
programming.
Quick Quiz 4
1. True or False: The semantic rules of a language tell you what is legal and what is not
legal.
2. The semicolon is also called the ____________________.
3. How can you make run-together words easier to understand?
4. Why are comments important in a program?
More on Assignment Statements
1. Define the terms simple assignment statement and compound assignment statement.
2. Define the C++ compound operators (+=, -=, *=, /=, and %=) and explain how and
page-pfe
3. Step through the “Convert Length” and “Make Change” Programming Examples to
help the students consolidate all the information from this chapter.
Class Discussion Topics
1. As mentioned in this chapter, C++ predefined identifiers such as cout and cin can be
redefined by the programmer. However, why is it not wise to do so?
2. The text mentioned that the char data type can be cast into an int. What are some
possible uses of this functionality?
Additional Projects
1. Learn and report on various compiler errors by modifying one or two of the programs in
this chapter. Try to compile the program. What happens when you fail to initialize a
value for a named constant? What are the error messages when you use a numeric or
string constant in an expression without first giving it a value? Finally, what happens
when you initialize a char data type with a character enclosed in double quotes?
2. Use one of the programs in this chapter to test for invalid user input. The program
should compile with no errors. What happens when you enter an unexpected value
(such as an incorrect data type) when prompted for user input? Test with several sets of
invalid data and document your findings.
Additional Resources
3. C++ Programming Style Guidelines:
page-pff
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 2-15
Key Terms
Arithmetic expression: an expression constructed using arithmetic operators and
statements in a more concise notation
Computer program: a sequence of statements whose objective is to accomplish a task
Data type: a set of values together with a set of operations
Declaration statements: statements that are used to declare things, such as variables
Decrement operator: --; decreases the value of a variable by 1
Identifier: a C++ identifier consists of letters, digits, and the underscore character (_); it
must begin with a letter or underscore
Implicit type coercion: when a value of one data type is automatically changed to
another data type
Increment operator: ++; increases the value of a variable by 1
Output statement: an output on the standard output device via cout and <<
page-pf10
C++ Programming: From Problem Analysis to Program Design, Eighth Edition 2-16
Post-decrement: has the syntax variable--
Post-increment: has the syntax variable++
Precision: the maximum number of significant digits
Run-together word: an identifier that is composed of two or more words that are
combined without caps or underscores
Self-documenting identifiers: identifiers that describe the purpose of the identifier
through the name
Semantic rules: rules that determine the meaning of the instructions
variable
Stream insertion operator: <<; takes information from a variable and puts it into a
stream
String: a sequence of zero or more characters
Syntax rules: rules that describe which statements (instructions) are legal, or accepted

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.