02slide Elementary Programming
02slide Elementary Programming
02slide Elementary Programming
ComputeArea Run
ComputeAreaWithConsoleInput
Run
ComputeAverage Run
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
11
Identifiers
An identifier is a sequence of characters that
consists of letters, digits, and underscores (_).
An identifier must start with a letter or an
underscore. It cannot start with a digit.
An identifier cannot be a reserved word. (See
Appendix A, C++ Keywords, for a list of
reserved words.)
An identifier can be of any length, but your C++
compiler may impose some restriction. Use
identifiers of 31 characters or fewer to ensure
portability.
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
12
Variables
// Compute the first area
radius = 1.0;
area = radius * radius * 3.14159;
cout << area;
cout << sizeof(int) << " " << sizeof(long) << " " <<
sizeof(double);
int i = 34;
long k = 1000000;
double d = 5.0;
16 digits
cout << "1.0F / 3.0F is " << 1.0F / 3.0F << endl;
+ Addition 34 + 1 35
% Remainder 20 % 3 2
+, -, *, /, and %
5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5
DisplayTime Run
ComputeChange Run
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
30
Trace ComputeChange
Suppose amount is 11.56
int remainingAmount = (int)(amount * 100); remainingAmount 1156
is translated to
FahrenheitToCelsius Run
Elapsed
time
Time ShowCurrentTime
Unix Epoch Current Time
01-01-1970 time(0)
00:00:00 GMT
Run
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
39
Augmented Assignment Operators
Operator Example Equivalent
+= i += 8 i = i + 8
-= f -= 8.0 f = f - 8.0
*= i *= 8 i = i * 8
/= i /= 8 i = i / 8
%= i %= 8 i = i % 8
short i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;
Explicit casting
int i = static_cast<int>(3.0);
(type narrowing)
int i = (int)3.9; (Fraction part
is truncated)
double d = 4.5;
int i = static_cast<int>(d); // d is not changed
SalesTax Run
System
Analysis
System
Design
Implementation
Testing
Deployment
Maintenance
Implementation
Testing
Implementation
Testing
Part of the analysis entails modeling
the systems behavior. The model is
Deployment
intended to capture the essential
elements of the system and to define
Maintenance
services to the system.
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
51
System Design
Requirement
Specification
The process of designing the
systems components.
System
Analysis
System
Design
Implementation
Testing
System
Analysis Input, Process, Output
System
Design
Implementation
Testing
Maintenance
Implementation
Testing
This phase requires the use of a
programming language like Java. Deployment
The implementation involves
coding, testing, and debugging. Maintenance
Implementation
Testing
An independent team of software
engineers not involved in the design Deployment
and implementation of the project
usually conducts such testing. Maintenance
System
Design
Implementation
Testing
For a Java applet, this means
installing it on a Web server; for a Deployment
Java application, installing it on the
client's computer. Maintenance
Implementation
Testing
A software product must continue to
perform and improve in a changing Deployment
environment. This requires periodic
upgrades of the product to fix newly Maintenance
discovered bugs and incorporate changes.
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
57
Problem: Computing Loan Payments
This program lets the user enter the interest
rate, number of years, and loan amount and
computes monthly payment and total payment.
loanAmount monthlyInterestRate
1 1
numberOfYears12
(1 monthlyInterestRate )
ComputeLoan Run
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
58
Common Errors
Common Error 1: Undeclared/Uninitialized Variables
and Unused Variables
Common Error 2: Integer Overflow
Common Error 3: Round-off Errors
Common Error 4: Unintended Integer Division
Common Error 5: Forgetting Header Files