01_Introduction to Computers and Program... 2453KB Sep 05 2016

Download Report

Transcript 01_Introduction to Computers and Program... 2453KB Sep 05 2016

CHAPTER 1
C++ Programming
- Introduction
Lecturer:
Dept. of Electrical and Electronic Engineering
1
OBJECTIVES:
In this chapter, you will learn about:
 Computer systems
 Simple program logic
 The steps involved in the program development cycle
 Pseudocode statements and flowchart symbols
 Using a sentinel value to end a program
 Programming and user environments
 The evolution of programming models
2
1.1 COMPUTER SYSTEMS
What is a COMPUTER?

How is a computer defined?
 A computer is an electronic device that stores and processes data

Includes both hardware and software
 Hardware
 Physical aspect of the computer that can be seen
 Software
 Invisible instructions that control the hardware and make it
work

Computer programming consists of writing instructions for
computer to perform
3
1.1 COMPUTER SYSTEMS
Component of a Computer (HW)
4
1.1 COMPUTER SYSTEMS
Component of a Computer (HW)
System Unit
CPU
Motherboard
RAM
Expansion
cards
Output devices
monitor
Communication devices
Storage Devices
Input devices
Optical disc
drive
mouse
Hard disk
drive
keyboard
Modem, network interface card
5
(NICs), cable, wireless, etc…
1.1 COMPUTER SYSTEMS
Component of a Computer (HW) – 1. System Unit
The motherboard also known as the main board or system board or planar
board. It contains most of the important components, including the chipset,
which controls essential functions inside a Computer. The key components of a
motherboard are: CPU socket / slot, BIOS - ROM chip, Chipset (North / South
Bridge or memory and I/O Controller hubs), Expansion slots, and Memory area
- SIMMs or DIMMs (RAM).
The motherboard directly interacts with other components of the Computer, and
is responsible for the stability, feature support, expandability and upgrading
ability of any system to a maximum extent.
6
1.1 COMPUTER SYSTEMS
Component of a Computer (HW) – 1. System Unit
Processor:
 AKA Central Processing Unit (CPU)
 A CPU is on a chip called a microprocessor
 CPU interprets and carries out the basic instructions that operate the
computer
 A Place where calculations and comparisons are performed
1.


The brain of a computer,
retrieve instructions from
memory and executes them
It continuously follows the
fetch-decode-execute cycle:
7
1.1 COMPUTER SYSTEMS
Component of a Computer (HW) – 1. System Unit
1.
Processor:
 The CPU contains:
i. Control unit controls and coordinates the actions of the other
components
ii. Logic unit performs numeric operations(+,-x,/) and logical operations
(comparisons)

Processor Speed:
 The speed of a CPU is controlled by the system clock
 The system clock generates an electronic pulse at regular intervals
 The faster the clock speed, the more instructions are executed in a
given period of time.
8
1.1 COMPUTER SYSTEMS
Component of a Computer (HW) – 1. System Unit
2.
Memory:
 Store data and program instructions for the CPU to execute.
 Memory unit is an ordered sequence of bytes, each holding eight bits.
 Using a byte as minimum storage unit
1.
•
•
Random access memory (RAM):
Hold changing data and programs which do not have to be
permanently available.
require a constant source of power to retain the information
(volatile memory)
2. Read only memory (ROM):
•
Contains program that always present – for example BIOS –
code needed to start computer.
•
does not require a constant source of power to retain the
information (non volatile memory)
9
1.1 COMPUTER SYSTEMS
Component of a Computer (HW) – 2. Storage Device

Programs and data are permanently stored on storage devices and are moved
to memory when the computer actually uses them.

Non-volatile – data retain when power off

Example
 hard disks and floppy disk
 CD-R and CD-RW
 Tape
10
1.1 COMPUTER SYSTEMS
Component of a Computer (HW) – 3. Input Device

Hardware used to enter data and instructions

Examples:
1. Keyboard:
 Similar to typewriter keyboard
 With extra keys for certain special functions
(function keys, numeric keypad, arrow keys, insert, delete, page up, page
down keys)
2.
Mouse:
 A pointing device that used to move an electronic pointer called a cursor
around the screen
11
1.1 COMPUTER SYSTEMS
Component of a Computer (HW) – 4. Output Device

Hardware that conveys information to one or more people

Examples:
1. Monitor:
 size measured diagonally, like a television screen
 Mostly with multimedia capabilities: text, graphics, video, etc.
 Maximum resolution :
indicating the number of picture elements, called pixels, that it can
display (such as 1280 by 1024)
 High resolution (more pixels) produces sharper pictures
2.
Printer
12
1.1 COMPUTER SYSTEMS
Component of a Computer (HW) – 5. Communication Device

Hardware component that enables a computer to send and receive data,
instructions, and information

Examples:
1. Modem (use phone line)
2. DSL( digital subscriber line)
3. Cable modem (use TV cable line)
4. Network interface card (connect computer to LAN)
13
1.1 COMPUTER SYSTEMS
Component of a Computer (HW) – 5. Communication Device
1.
Modulator-Demodulator, aka Modem (use phone line)
 Data transfer devices allow information to be sent and received between
computers

Allows information to be moved across a telephone line

A data transfer device has a maximum data transfer rate

A modem, for instance, may have a data transfer rate of
56,000 bps (bits per second)
14
CHECK POINT









1.1 What are hardware and software?
1.2 List five major hardware components of a computer.
1.3 What does the acronym “CPU” stand for?
1.4 What unit is used to measure CPU speed?
1.5 What is a bit? What is a byte?
1.6 What is memory for?
What does RAM stand for?
Why is memory called RAM?
1.7 What unit is used to measure memory size?
1.8 What unit is used to measure disk size?
1.9 What is the primary difference between memory and a
device?
storage
15
1.1 COMPUTER SYSTEMS
Component of a Computer (SW) – Programs

Also called a software,

Consists of a series of instructions that tells the computer
what to do and how to do it

Without programs, a computer is an empty machine

Computers ONLY understand machine language
19
1.1 COMPUTER SYSTEMS
Component of a Computer (SW) – Programs

Types of Software:
1. System software:
 control or maintain operations of the computer and its devices
 Serves as the interface between user, application software & computer’s
hardware
 Example: OS, utility program
2.
Application software
 Programs designed to make users more productive
 Exp: word processing, spreadsheet, presentation graphic, database,
internet browser, etc.
20
1.1 COMPUTER SYSTEMS
Component of a Computer (SW) – Programs

Types of System Software:
i.
Operating system:

Program that coordinates all activities among computer hardware

devices
Application programs, cannot run without an operating system
Example: Windows 98, NT, 2000, XP, or Me

ii.
Utility program:


maintenance-type tasks
Example: managing disk drives, printers, other devices
21
1.1 COMPUTER SYSTEMS
Component of a Computer (SW) – Programs

The major tasks of O.S:
1. Controlling and monitoring system activities

2.
Allocating and assigning system resources

3.
Sending output to the monitor, keeping track of files and directories on
the disk
Determine what computer resources (CPU, memory, disks) a program
needs
Scheduling operations


Scheduling programs to use the system resources efficiently.
Support techniques such as multiprogramming, multithreading, and
multiprocessing
22
1.2 PROGRAMMING LANGUAGE

Programming Language: used to write computer instructions

Syntax: rules governing word usage and punctuation in the language (like
spelling & grammar)
Human language: English, Spanish, French, etc…
23
1.3 CATEGORIES OF PROGRAMMING LANGUAGES
Low-level
language
Machine-dependent
- runs only on one type of computer
High-level
language
Often machine-independent
- can run on many different types of
computers and operating systems
Not easily portable
1 language instruction
= 1 machine instruction
1 language instruction
= multiple machine instruction
Machine and assembly languages
are low-level
24
1.3 CATEGORIES OF PROGRAMMING LANGUAGES

Types of Programming Language:
i. Machine Language
 Binary (0, 1) – on-off circuitry
 Directly recognize by computer
ii.
Assembly Language
 Symbolic code – mnemonic
iii.
High-level Language

English-like phrase, easy
Machine
dependent
Machine
independent
25
1.3 CATEGORIES OF PROGRAMMING LANGUAGES
Compiler or interpreter:
Software that translates into machine language
26
1.3 CATEGORIES OF PROGRAMMING LANGUAGES
Low-Level Language – Machine Language

1st generation

Most primitive (basic)

The only language computer
directly recognizes

The instructions are in the
form of binary code



Example:
1101101010011010
Machine-dependent
27
1.3 CATEGORIES OF PROGRAMMING LANGUAGES
Low-Level Language – Assembly Language

2nd generation

Symbolic instruction codes,
meaningful abbreviations and
codes (English-like phrases) mnemonic

For example, to add two
numbers in assembly code
 ADDF3 R1, R2, R3

An assembler is used to convert
assembly language programs
into machine code

Machine-dependent

Example:
28
1.3 CATEGORIES OF PROGRAMMING LANGUAGES
Low-Level Language – Assembly Language
Assembly
code
Assembler
Machine code
29
1.3 CATEGORIES OF PROGRAMMING LANGUAGES
High-Level Language

English-like and easy to
learn program

Popular high-level
languages


To overcome the
platform-specific
problem and make
programming easier.






For example, computes
the area of a circle with
radius 5 in high-level
languages





Java
COBOL (Common Business
Oriented language)
FORTRAN (Formula
Translation)
Pascal
Ada
BASIC
C
Visual Basic
Delphi
C++
Area = 5 * 5 * 3.1415
30
1.3 CATEGORIES OF PROGRAMMING LANGUAGES
High-Level Language
Source program is a program written in a high-level language
 Compiler is used to translate the source program into a machinelanguage program called object program
 Object file is linked with other supporting library code to form an
executable file

31
CHECK POINT








1.10 What language does the CPU understand?
1.11 What is an assembly language?
1.12 What is an assembler?
1.13 What is a high-level programming language?
1.14 What is a source program?
1.15 What is an interpreter?
1.16 What is a compiler?
1.17 What is the difference between an interpreted
and a compiled language?
language
32
1.4 SIMPLE PROGRAM LOGIC

Program execute or run
Program execution process: IPO

Programs with syntax errors cannot execute

36
1.4 SIMPLE PROGRAM LOGIC

Types of Errors:
i. Syntax errors:
 Sth that violates programming rules
 Program cannot run
ii. Logic errors (semantic errors):
 Errors in program logic produce incorrect output
 Program executed with wrong result.

Logic: Sequence of specific instructions in specific order
Logic errors are more difficult to locate than syntax errors
37
1.4 SIMPLE PROGRAM LOGIC

What’s wrong with this logic for making a cake?
38
1.4 SIMPLE PROGRAM LOGIC

Logic for multiplying a number by 2
(includes input, processing and output statements)
Get inputNumber.
Compute calculatedAnswer as inputNumber times 2.
Print calculatedAnswer.
39
1.4 SIMPLE PROGRAM LOGIC
40
1.5 PROGRAM DEVELOPMENT CYCLE

Six programming phases:
1
7
2
6
3
5
4
41
1.5 PROGRAM DEVELOPMENT CYCLE
1. Understand the problem


One of the most difficult aspects of programming
Users or end users





People for whom a program is written
Users may not be able to articulate their needs well
User needs may be changing frequently
Programmers may have to learn the user’s functional job tasks
Failure to understand the problem is the major cause of most project
failures
42
1.5 PROGRAM DEVELOPMENT CYCLE
2. Planning the logic



Plan the steps that the program will take
Heart of the programming process
Most common planning tools





Flowcharts: a pictorial representation of the logic steps
Pseudocode: English-like representation of the logic
IPO charts: input, processing, and output chart
TOE charts: tasks, objects, and events chart
Desk-checking

Walking through a program’s logic on paper before you actually write the
program
43
1.5 PROGRAM DEVELOPMENT CYCLE
3. Coding the logic


Writing the instructions
Hundreds of programming languages available



Choose based on features
Similar in their basic capabilities
Easier than the planning step
44
1.5 PROGRAM DEVELOPMENT CYCLE
4. Translate into machine language (compiler)

Translator program



Compiler or interpreter
Changes the programmer’s English-like high-level programming language
into the low-level machine language
Syntax error




Misuse of a language’s grammar rules
Identified by the compiler or interpreter
Programmer corrects listed syntax errors
Might need to recompile the code several times
45
1.5 PROGRAM DEVELOPMENT CYCLE
4. Translate into machine language (compiler)

Creating an executable program
46
1.5 PROGRAM DEVELOPMENT CYCLE
5. Testing the program

Testing the program:




Logical error


Execute it with sample data and check results
Identify logic errors and correct them
Choose test data carefully to exercise all branches of the logic
Results when a syntactically correct statement, but the wrong one for the
current context, is used
Debugging

process of finding and correcting program errors
47
1.5 PROGRAM DEVELOPMENT CYCLE
6. Deploy the program


Do this after testing is complete and all known errors have been
corrected
May require coordination with other related activities or software
7. Maintaining the program

Maintenance


Common first programming job


Making changes after the program is put into production
Maintaining previously written programs
Make changes to existing programs

Repeat the development cycle
48
1.5 PROGRAM DEVELOPMENT CYCLE
49
1.6 USING PSEUDOCODE STATEMENTS &
FLOWCHART SYMBOLS

Pseudocode: English-like representation of the logic

Flowchart: pictorial representation of the logic
50
1.6 USING PSEUDOCODE STATEMENTS &
FLOWCHART SYMBOLS
Writing Pseudocode

Pseudocode representation of a number-doubling problem
start
input myNumber
set myAnswer = myNumber * 2
output myAnswer
stop

Beginning statement: start
Terminating statement: stop

Flexible planning tool

51
1.6 USING PSEUDOCODE STATEMENTS &
FLOWCHART SYMBOLS
Drawing flowchart

Create a flowchart



Input & Output symbol



Draw geometric shapes that contain the individual statements
Connect shapes with arrows
Indicates input & output operation
Parallelogram
Processing symbol


Processing statements such as arithmetic
Rectangle
52
1.6 USING PSEUDOCODE STATEMENTS &
FLOWCHART SYMBOLS
Drawing flowchart

Flowlines


Arrows that connect steps
Terminal symbols



Start/stop symbols
Shaped like a racetrack
Also called lozenges
53
1.6 USING PSEUDOCODE STATEMENTS &
FLOWCHART SYMBOLS
Drawing flowchart

Flowchart and pseudocode of program that doubles a number
54
1.6 USING PSEUDOCODE STATEMENTS &
FLOWCHART SYMBOLS
Drawing flowchart
Repeating Instructions:

Previous program




Only works for one number
Not feasible to run repeatedly (1000 times)
Not feasible to add 10,000 lines of code to a program
Solution:


Create a loop (repetition of a series of steps) instead
Avoid an infinite loop (repeating flow of logic that never ends)
55
1.6 USING PSEUDOCODE STATEMENTS &
FLOWCHART SYMBOLS
Drawing flowchart
Repeating Instructions:

Flowchart of infinite number-doubling program
56
1.6 USING PSEUDOCODE STATEMENTS &
FLOWCHART SYMBOLS
57
1.7 USING A SENTINEL VALUE TO END A PROGRAM

Decision: Testing a value

Flowchart decision symbol:



diamond shape,
with two flow lines: Yes & No
Sentinel value (dummy value):



Does not represent real data
Signal to stop
Can be used with input from files or from users
58
1.7 USING A SENTINEL VALUE TO END A PROGRAM

Flowchart of number-doubling program with
sentinel value of 0
59
1.7 USING A SENTINEL VALUE TO END A PROGRAM

End-of-file (EOF) marker:



Code stored in the file that marks the end of the data
Usually used instead of a sentinel value for file input
Flowchart using eof
61
1.7 USING A SENTINEL VALUE TO END A PROGRAM
62
1.8 THREE BASIC STRUCTURE

Structure

Basic unit of programming logic

3 basic types of structures:
Sequence structure
Selection structure (decision structure)
Loop structure
i.
ii.
iii.
63
1.8 THREE BASIC STRUCTURE

1. Sequence control structure

Control structure that shows
actions following each other in
order

depicts logical order of program
instructions
64
1.8 THREE BASIC STRUCTURE

2. Selection control structure


Tells program which action to take, based on a certain condition
Two types
 Case control structure
— Yields one of three or more possibilities

If-then-else control structure
— yields one of two possibilities: true or false
65
1.8 THREE BASIC STRUCTURE

2. Selection control structure

Case control structure
— yields one of three or more possibilities
66
1.8 THREE BASIC STRUCTURE

2. Selection control structure

If-then-else control structure
— yields one of two possibilities: true or false
67
1.8 THREE BASIC STRUCTURE

3. Repetition control structure

Enables program to perform one or more actions repeatedly
 While control structure—repeats as long as condition is
true
 Do-while control structure—repeats until condition is
true
while
Do-while
68
1.9 PROGRAMMING & USER ENVIRONMENT

Many options for programming and user environments




Planning

Flowchart

Pseudocode
Coding

Text editors
Executing

Input from keyboard, mouse, microphone
Outputting

Text, images, sound
69
1.9 PROGRAMMING & USER ENVIRONMENT
Programming environment

Use a keyboard to type program statements into an editor

Plain text editor

Similar to a word processor but without as many features

Text editor that is part of an integrated development environment (IDE)

Software package that provides an editor, a compiler, and other
programming tools
70
1.9 PROGRAMMING & USER ENVIRONMENT
Programming environment

Text Editor: A C# number-doubling program in Notepad
71
1.9 PROGRAMMING & USER ENVIRONMENT
Programming environment

IDE: A C# number-doubling program in Visual Studio
72
1.9 PROGRAMMING & USER ENVIRONMENT
User environment

Ways of executing a program:

Command line:

Location on your computer screen where you type text entries to
communicate with the computer’s operating system

Graphical User Interface (GUI):

Allows users to interact with a program in a graphical environment
Command line
GUI
73
1.9 PROGRAMMING & USER ENVIRONMENT
74
1.10 EVOLUTION OF PROGRAMMING MODELS




Programming began in the 1940s, using memory addresses and
machine code directly
Higher level languages were developed to allow English-like
instructions
Older programs were “monolithic,” and ran from beginning to end
Newer programs contain modules that can be combined to form
programs
75
1.10 EVOLUTION OF PROGRAMMING MODELS

Two major programming techniques:

Procedural programming

focuses on the procedures that programmers create

Object-oriented programming

Focuses on objects, or “things,” and describes their
features (or attributes) and their behaviors

Both techniques employ reusable program modules

This text

Focuses on procedural programming techniques
76
1.10 EVOLUTION OF PROGRAMMING MODELS
77
SUMMARY









Hardware and software accomplish three major operations: input,
processing, and output
Logic must be developed correctly
Logical errors are much more difficult to locate than syntax errors
Program development cycle:
 understanding, planning, coding, translating, testing, employ, and
maintain.
Use flowcharts, pseudocode, IPO charts, and TOE charts to plan the logic
Avoid infinite loops by testing for a sentinel value
Use a text editor or an IDE to enter your program statements
Procedural programmers concentrate on the actions performed with data.
Object-oriented programmers focus on objects and their behaviors and
attributes
78