Sunteți pe pagina 1din 59

C++ for

Engineers and Scientists


Second Edition

Chapter 1
Introduction
Objectives

Introduction to Programming
Problem Solution and Software Development
Algorithms
Common Programming Errors
Computer Hardware and Storage Concepts

C++ for Engineers and Scientists, Second Edition 2


Introduction to Programming

A Program: A set of codes that instructs the


computer to carry out some processes to
produce a specific result
Also called software
Programming: the process of writing
programs, or softwares

C++ for Engineers and Scientists, Second Edition 3


Introduction to Programming: Machine Language
Machine Language programs, also called
executables, a set binary coded instructions,
which consist of zeros (0) and ones(1).
The first generation of computers was coded
in machine language that was specific to each
model of computer.
Machine language was not user-friendly. That
is the user had to remember a long list of
codes, numbers or operation codes and know
where instructions were stored in computer
memory.
Writing in machine language is tedious!
C++ for Engineers and Scientists, Second Edition 4
Introduction to Programming: Assembly Languages

Assembly Language: programming language


with symbolic names for opcodes, and decimals
or labels for memory addresses
Example:
ADD 1, 2
MUL 2, 3
Assembly language programs must be
translated into machine instructions, using an
assembler
Its specific to particular computers
C++ for Engineers and Scientists, Second Edition 5
Assembly Language VS Machine Language

Machine Language Assembly Language


100101 ADD
011001 SUB
001101 MPY
100111 CMP
100011 JMP
110011 JNZ

C++ for Engineers and Scientists, Second Edition 6


Introduction to Programming:
Assembly Language (continued)

C++ for Engineers and Scientists, Second Edition 7


Introduction to Programming:
Low- and High-Level Languages

Low-level languages: languages that use


instructions tied directly to one type of computer
Examples: machine language, assembly
language
High-level languages: instructions resemble
written languages, such as English,and can be
run on a variety of computer types
Examples: Visual Basic, C, C++, Java

C++ for Engineers and Scientists, Second Edition 8


Introduction to Programming: Low-
and High-Level Languages (continued)
Source code: the programs written in a high-
or low-level language
Source code must be translated to machine
instructions in one of two ways:
Interpreter: each statement is translated
individually and executed immediately after
translation
Compiler: all statements are translated and
stored as an executable program, or object
program; execution occurs later

C++ for Engineers and Scientists, Second Edition 9


Introduction to Programming: Low-
and High-Level Languages (continued)

Large C++ programs may be stored in two or


more separate program files due to
Use of previously written code
Use of code provided by the compiler
Modular design of the program (for reusability of
components)
Linker: combines all of the compiled code
required for the program

C++ for Engineers and Scientists, Second Edition 10


Introduction to Programming:
Procedural and Object Orientations
Programs can also be classified by their
orientation:
-Procedural: a series of steps, each of which
performs a calculation, retrieves input, or
produces output.
Concepts like assignments, loops, sequences
and conditional statements are the building blocks
of procedural programming.
Major procedural programming languages are
COBOL, FORTRAN, C and C++.

C++ for Engineers and Scientists, Second Edition 11


Introduction to Programming: Procedural
and Object Orientations

Object-oriented (OO) Programming: reusable


objects, containing code and data, are
manipulated
The fundamental building blocks of OO
Programming are object modeling, classification
and inheritance.
Major object-oriented languages are C++ , Java
etc.

C++ for Engineers and Scientists, Second Edition 12


Introduction to Programming: Procedural
and Object Orientations (continued)

Figure 1.2
Creating an
executable C++
program.

C++ for Engineers and Scientists, Second Edition 13


Introduction to Programming:
Application and System Software
Application software: programs written to
perform particular tasks for users
System software: collection of programs to
operate the computer system
System software must be loaded first; called
booting the system
Bootstrap loader: a permanent, automatically
executed component to start the boot process

C++ for Engineers and Scientists, Second Edition 14


Introduction to Programming:
Application and System Software
(continued)
Operating system: the set of system programs
used to operate and control a computer; also
called OS
Tasks performed by the OS include
Memory management
Allocation of CPU time
Control of input and output
Management of secondary storage devices

C++ for Engineers and Scientists, Second Edition 15


Introduction to Programming:
Application and System Software
(continued)
Multi-user system: a system that allows more
than one user to run programs on the computer
simultaneously
Multitasking system: a system that allows
each user to run multiple programs
simultaneously; also called multiprogrammed
system

C++ for Engineers and Scientists, Second Edition 16


Introduction to Programming:
The Development of C++
The purpose of most application programs is
to process data to produce specific results

Figure 1.3 Basic procedural operations.

C++ for Engineers and Scientists, Second Edition 17


Introduction to Programming:
The Development of C++ (continued)

Early procedural languages included


FORTRAN: Formula Translation
ALGOL: Algorithmic Language
COBOL: Common Business Oriented
Language
BASIC: Beginners All-purpose Symbolic
Instruction Code
Pascal
C
C++ for Engineers and Scientists, Second Edition 18
Introduction to Programming:
The Development of C++ (continued)

Early object-oriented language:


C++

C++ for Engineers and Scientists, Second Edition 19


Problem Solution and
Software Development
Software development procedure: method for
solving problems and creating software
solutions
Consists of three phases:
Phase I: Development and Design
Phase II: Documentation
Phase III: Maintenance
Software engineering: discipline concerned
with creating efficient, reliable, maintainable
software
C++ for Engineers and Scientists, Second Edition 20
Problem Solution and Software
Development (continued)

Figure 1.6 The three phases of software development.

C++ for Engineers and Scientists, Second Edition 21


Problem Solution and
Software Development:
Phase I. Development and Design
Program requirement: request for a program
or a statement of a problem
After a program requirement is received, Phase
I begins
Phase I consists of four steps:
Analysis
Design
Coding
Testing
C++ for Engineers and Scientists, Second Edition 22
Problem Solution and Software
Development: Phase I (continued)

Figure 1.7 The development and design steps.


C++ for Engineers and Scientists, Second Edition 23
Problem Solution and Software
Development: Phase I (continued)
Step 1: Analyze the Problem
Analyst must understand
What outputs are required
What inputs will be needed
How the inputs can be used to produce the
desired outputs
Failure to analyze and understand the
requirements leads to a failed solution!

C++ for Engineers and Scientists, Second Edition 24


Problem Solution and Software
Development: Phase I (continued)
Step 2: Develop a Solution
Algorithm: the exact steps used to solve a
problem
Algorithm is usually defined at high level, then
refined to detailed lower levels
Structure level diagrams may be used to
represent the levels of analysis

C++ for Engineers and Scientists, Second Edition 25


Problem Solution and Software
Development: Phase I (continued)

Figure 1.8 First-level structure diagram.

C++ for Engineers and Scientists, Second Edition 26


Problem Solution and Software
Development: Phase I (continued)

Figure 1.9 Second-level refinement structure diagram.


C++ for Engineers and Scientists, Second Edition 27
Problem Solution and Software
Development: Phase I (continued)

Step 3: Code the Solution


Also called writing the program, and
implementing the solution
Program should contain well-defined patterns or
structures of the following types:
Sequence
Selection
Iteration
Invocation

C++ for Engineers and Scientists, Second Edition 28


Problem Solution and Software
Development: Phase I (continued)
Sequence: defines the order in which
instructions are executed
Selection: allows a choice between different
operations, based on some condition
Iteration: allows the same operation to be
repeated based on some condition; also called
looping or repetition
Invocation: involves invoking a set of
statements when needed

C++ for Engineers and Scientists, Second Edition 29


Problem Solution and Software
Development: Phase I (continued)

Step 4: Test and Correct the Program


Testing: method to verify correctness and that
requirements are met
Bug: a program error
Debugging: the process of locating an error,
and correcting and verifying the correction
Testing may reveal errors, but does not
guarantee the absence of errors

C++ for Engineers and Scientists, Second Edition 30


Problem Solution and Software
Development: Phase I (continued)

Testing requires the use of meaningful,


representative test data
Impossible to test all possible combinations of
data and operations in a complex program
Testing plans must be developed to ensure
good coverage in testing
Testing usually requires more time than other
steps in Phase I

C++ for Engineers and Scientists, Second Edition 31


Problem Solution and Software
Development: Phase I (continued)

Relative effort for steps in Phase I

C++ for Engineers and Scientists, Second Edition 32


Problem Solution and
Software Development:
Phase II. Documentation
Many documents may be required, including
Program description
Algorithm development and changes
Well-commented program listing
Sample test runs
Users manual

C++ for Engineers and Scientists, Second Edition 33


Problem Solution and Software
Development: Phase III. Maintenance
Maintenance includes
Ongoing correction of newly discovered bugs
Revisions to meet changing user needs
Addition of new features
Maintenance usually the longest phase, and
may be the primary source of revenue
Good documentation vital for effective
maintenance

C++ for Engineers and Scientists, Second Edition 34


Problem Solution and Software
Development: Backup

Backup: process of making copies of program


code and documentation on a regular basis
Backup copies = insurance against loss or
damage
Consider using off-site storage for additional
protection

C++ for Engineers and Scientists, Second Edition 35


Algorithms

Algorithm: the step-by-step sequence of


instructions that describe how the data is to be
processed to produce the desired output
Programming = the translation of the selected
algorithm into a language the computer can use

C++ for Engineers and Scientists, Second Edition 36


Algorithms (continued)
Example: Calculate the sum of all whole numbers from
1 through 100

C++ for Engineers and Scientists, Second Edition 37


Algorithms (continued)

C++ for Engineers and Scientists, Second Edition 38


Algorithms (continued)

C++ for Engineers and Scientists, Second Edition 39


Algorithms (continued)

Pseudocode: English-like phrases used to


describe the algorithm
Formula: description of a mathematical
equation
Flowchart: diagram showing the flow of
instructions in an algorithm
Flowcharts use special symbols

C++ for Engineers and Scientists, Second Edition 40


Algorithms: Flowcharts

C++ for Engineers and Scientists, Second Edition 41


Algorithms: Flowcharts (continued)

C++ for Engineers and Scientists, Second Edition 42


Algorithms: Flowcharts (continued)

Figure 1.12 Flowchart for


calculating the average of three
numbers.

C++ for Engineers and Scientists, Second Edition 43


Algorithms: Flowcharts (continued)

Flowchart for
calculating
the payment of a
person :
Input the three values
into the variables Name,
Hours, Rate.
Calculate Pay = Hours *
Rate.
Display Name and Pay.

C++ for Engineers and Scientists, Second Edition 44


Algorithms: Flowcharts (continued)
Example: The program computes the sum, average and product of three numbers:

C++ for Engineers and Scientists, Second Edition 45


Algorithms: Flowcharts (continued)

In flowcharting, the diamond-shaped symbol is used to


indicate a decision.

The question is placed inside the symbol and each


alternative answer to the question is used to label the
exit arrow which leads to the appropriate next step of
the algorithm.
The decision symbol is the only symbol that may have
more than one exit.
The example below shows the flowchart for a program
that reads two numbers and displays the numbers read
in decreasing order:
C++ for Engineers and Scientists, Second Edition 46
Algorithms: Flowcharts (continued)

Read A, B
If A is less than B
BIG = B
SMALL = A
else
BIG = A
SMALL = B
Write (Display) BIG, SMALL

C++ for Engineers and Scientists, Second Edition 47


Algorithms: Flowcharts (continued)
Example of the flowchart symbol for
decision and the flowline that can
connect backward to represent a loop.

This algorithm can be described in


pseudocode as follows:

NUM = 4
Do
SQNUM = NUM*NUM
Print NUM, SQNUM
NUM NUM+1
While (NUM<=9)

*The statements NUM=NUM+! Means


Old value of NUM + 1 becomes new
value of NUM

C++ for Engineers and Scientists, Second Edition 48


Common Programming Errors

Common errors include


Starting to write the program before fully
understanding the requirements and the
algorithm to be used
Failing to make backups periodically
Failing to provide complete, precise instructions
in the programming language

C++ for Engineers and Scientists, Second Edition 49


Summary

Software: programs used to operate a


computer
Programming language types:
Low-level languages
Machine language (executable) programs
Assembly languages
High-level languages
Compiler and interpreter languages

C++ for Engineers and Scientists, Second Edition 50


Summary (continued)

Software engineering: discipline concerned with


creating readable, efficient, reliable, and
maintainable programs
Three phases in software development:
Program development and design
Documentation
Maintenance

C++ for Engineers and Scientists, Second Edition 51


Summary (continued)
Four steps in program development and design:
Analyze the problem
Develop a solution
Code the solution
Test and correct the solution
Algorithm: step-by-step procedure that
describes how a task is performed
Computer program: self-contained unit of
instructions and data used to operate a
computer to produce a desired result
C++ for Engineers and Scientists, Second Edition 52
Summary (continued)

Four fundamental control structures used in


coding:
Sequence
Selection
Iteration
Invocation

C++ for Engineers and Scientists, Second Edition 53


Appendix: Computer Hardware and
Storage Concepts

Computer hardware: components that


support the capabilities of the computer

C++ for Engineers and Scientists, Second Edition 54


Appendix: Computer Hardware and
Storage Concepts (continued)
Components include:
Arithmetic and logic unit (ALU): performs
arithmetic and logic functions
Control unit: directs and monitors overall
operations
Memory unit: stores instructions and data
Input and output (I/O) unit: interfaces to
peripheral devices
Secondary storage: nonvolatile permanent
storage such as hard disks
C++ for Engineers and Scientists, Second Edition 55
Appendix: Computer Hardware and
Storage Concepts (continued)
Central processing unit (CPU), also called
microprocessor, combines the ALU and
control unit on a single chip

Figure 1.15 VLSI chip connections for a desktop computer.


C++ for Engineers and Scientists, Second Edition 56
Appendix: Computer Hardware and
Storage Concepts (continued)

Computer storage:
Bit: smallest unit of data; value of 0 or 1
Byte: grouping of 8 bits representing a single
character
Character codes: collection of patterns of 0s
and 1s representing characters; examples
include
ASCII
EBCDIC
C++ for Engineers and Scientists, Second Edition 57
Appendix: Computer Hardware and
Storage Concepts (continued)

Number codes: patterns used to store


numbers
Twos Complement number code: represents
a decimal number as a binary number of 0s
and 1s

C++ for Engineers and Scientists, Second Edition 58


Appendix: Computer Hardware and
Storage Concepts (continued)

Word: grouping of one or more bytes


Number of bytes in a word determines the
maximum and minimum values that can be
stored:

C++ for Engineers and Scientists, Second Edition 59