37
1 Overview of Programming and Problem Solving Chapter 1

1 Overview of Programming and Problem Solving Chapter 1

Embed Size (px)

Citation preview

1

Overview of Programming and Problem Solving

Chapter 1

2

WHAT IS A COMPUTER AND WHAT DOES IT DO?WHAT IS A COMPUTER AND WHAT DOES IT DO? An electronic machine, operating

under the control of instructions stored in its own memory, that can accept data (input), manipulate data according to specified rules (process), produce results (output), and store the results for future use.

3

Components of a Computer System

Hardware - the physical components of a computer

Software - sets of instructions to solve a task (computer programs).

4

Input Devices Output Devices Central Processing Unit Memory Unit Storage Devices (Auxilliary storage

or secondary storage)

THE COMPONENTS OF A COMPUTER (computer hardware)

THE COMPONENTS OF A COMPUTER (computer hardware)

5

Computer Components

Arithmetic Logic Unit

Control Unit

Auxiliary StorageDevice

Memory Unit ( RAM & Registers )

Central Processing Unit ( CPU ) Input Device

Output Device

Peripherals

6

Memory Unit

is an ordered sequence of storage cells, each capable of holding a piece of information

each cell has its own unique address

the information held can be input data, computed values, or your program instructions.

7

Memory Organization two circuit states correspond to 0 and 1

bit (short for binary digit) refers to a single 0 or 1. Bit patterns represent both the computer instructions and computer data

1 byte = 8 bits

1 KB = 1024 bytes

1 MB = 1024 x 1024 = 1,048,576 bytes ~ 1 million bytes

1 GB = 1024 X 1024 X 1024 ~ 1 billion bytes

8

Central Processing Unit

has 2 components to execute program instructions

Arithmetic/Logic Unit performs arithmetic operations, and makes logical comparisons.

Control Unit controls the order in which your program instructions are executed.

9

Peripherals are input, output, or auxiliary storage

devices attached to a computer

Input Devices include keyboard and mouse.

Output Devices include printers, video display, LCD screens, speakers, and digital cameras.

Auxiliary Storage Devices include disk drives, scanners, CD-ROM and DVD-ROM drives.

10

Personal Computer

11

NETWORKS AND THE INTERNETNETWORKS AND THE INTERNET

Network Sharing

resources Local area

network - LAN

Wide area network

Modem

12

COMPUTER SOFTWARECOMPUTER SOFTWARE

Computer programs A series of instructions that tells the

hardware of a computer how to perform tasks

13

TYPES OF COMPUTER SOFTWARETYPES OF COMPUTER SOFTWARE System Software

Examples: Operating system Utility programs

Application Software Examples:

Word Processing Software Programs written to solve a task in CSCI

1170

14

What is Computer Programming?

It is the process of planning a sequence of steps (called instructions) for a computer to follow.

STEP 1

STEP 2

STEP 3

. . .

15

Programming Life Cycle Phases

1 Problem-Solving

2 Implementation

3 Maintenance

16

Phase 1:Problem-Solving

ANALYZE the problem and SPECIFY what the solution must do

develop a GENERAL SOLUTION (ALGORITHM) to solve the problem

VERIFY that your solution really solves the problem

17

Problem Solving Techniques

ASK QUESTIONS -- about the data, the process, the output, error conditions.

LOOK FOR FAMILIAR THINGS -- certain situations arise again and again.

SOLVE BY ANALOGY -- it may give you a place to start.

USE MEANS-ENDS ANALYSIS -- Determine the I/O and then work out the details.

18

Sample Problem

A programmer needs an algorithm to determine a person’s transaction at a bank. Suppose that the only transactions are processing a check on the person’s account and making a deposit.

19

Bank Transactions

Suppose the beginning balance is 1000.00 and a check comes through for 80.00.

Then the new balance is 1000 – 80 = 920.00

Suppose the beginning balance is 80.00 and a check comes through for 100.00. What happens?

What happens when the person deposits a certain amount?

20

If the transactions is a deposit, balance = old balance + deposit amount

Elseif check amount < balance

balance = old balance – check amountElse ??

Bank Transactions, In General

21

An Algorithm is . . .

a step-by-step procedure for solving a problem in a finite amount of time.

22

Design for a Bank Transaction

Get the transaction type (deposit or check)1. Get the old balance 2. If the transaction type is deposit,

1. Get the deposit amount 2. Balance = old balance + deposit amount

3. If the transaction type is check, 1. Get the check amount2. If the check amount < old balance

balance = old balance – check amountprint the new balance

Elseprint a message saying that there were insufficient fundsbalance = old balance – check bounce feesend a notice to the customer of NSF

23

Phase 2: Implementation

After an algorithm is written, what next?

The algorithm is used as a basis for creating a computer program.

A computer program is a set of instructions written in a computer programming language.

24

What is a Programming Language?

It is a language with strict grammar rules, symbols, and special words used to construct a computer program.

25

Implementation Phase: Test

translating your algorithm into a programming language is called CODING

TESTING your program means running (executing) your program on the computer, to see if it produces correct results

if it does not, then you must find out what is wrong with your program or algorithm and fix it--this is called debugging

26

Phase 3: Maintenance

USE and MODIFY the program to meet changing requirements or correct errors that show up in using it

maintenance begins when your program is put into use and accounts for the majority of effort on most programs

27

Programming Life Cycle1 Problem-Solving Phase

Analysis and Specification General Solution ( Algorithm ) Verify

2 Implementation Phase Concrete Solution ( Program ) Test

3 Maintenance Phase Use Maintain

28

Programming Languages: Machine Language

is not portable

runs only on specific type of computer

is made up of binary-coded instructions (strings of 0s and 1s)

is the language that can be directly

used by the computer

29

High Level Languages

are portable

user writes program in language similar to natural language

examples -- FORTRAN, COBOL, Pascal, Ada, Modula-2, C++, Java

most are standardized by ISO/ANSI to provide an official description of the language

30

Three C++ Program Stages

other code from libraries,

etc.

other code from libraries,

etc.

written in machine language

written in machine language

written in machine language

written in machine language

written in C++

written in C++

via compiler via linker

SOURCE OBJECT EXECUTABLE

myprog.cpp myprog.obj myprog.exe

31

Basic Control Structures a sequence is a series of statements

that execute one after another

selection (branch) is used to execute different statements depending on certain conditions

Looping (repetition) is used to repeat statements while certain conditions are met.

a subprogram is used to break the program into smaller units

32

SEQUENCE

Statement Statement Statement . . .

33

SELECTION (branch)IF Condition THEN Statement1 ELSE Statement2

Statement1

Statement Statement2

Condition . . .

True

False

34

LOOP (repetition)

Statement

Condition. . .

False

True

WHILE Condition DO Statement1

35

SUBPROGRAM (function)

SUBPROGRAM1 . . .

SUBPROGRAM1 a meaningful collection of SEQUENCE, SELECTION, LOOP, SUBPROGRAM

36

Some C++ History

1972 : Dennis Ritchie at Bell Labs designs C and 90% of UNIX is then written in C

Late 70’s : OOP becomes popular

Bjarne Stroustrup at Bell Labs adds features to C to form “C with Classes”

1983 : Name C++ first used

1998 : ISO/ANSI standardization of C++

37

Computing Profession Ethics

copy software only with permission from the copyright holder

give credit to another programmer by name whenever using his/her code

use computer resources only with permission

guard the privacy of confidential data

use software engineering principles to develop software free from errors