programming in c++ dale/weems/headington chapter 1 overview of programming and problem solving

28
Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Upload: solomon-chandler

Post on 17-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Programming in C++

Dale/Weems/Headington

Chapter 1

Overview of Programming and Problem Solving

Page 2: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

What is Computer Science? The Computing Curriculum 1991 (ACM/IEEE)

Algorithms and Data Structures Architecture Artificial Intelligence and Robotics Database and Information Retrieval Human-Computer Communication Numerical and Symbolic Computation Operating Systems Programming Languages Software Engineering Social and Professional Context

Page 3: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

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.

Page 4: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

High Level Languages

Are portable User writes program in language similar to natural language Compiler translates high-level language program into machine

language Examples of high level languages

FORTRAN, ALGOL, BASIC, COBOL, Pascal, C, Ada, Modula-2, C++

Page 5: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Early History of C++

1969 -- Ken Thompson at AT&T’s Bell Labs begins to design UNIX operating system

Writing UNIX using assembly language on DEC PDP-7

For more powerful PDP-11 wants to write UNIX in a high level language

No appropriate high-level language exists

Page 6: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

More C++ History

1970 -- Thompson designs new B language 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

Page 7: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Three 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

Page 8: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Computer Components

Arithmetic Logic Unit

Memory Unit ( RAM & Registers )

Control UnitInput Device

Output Device

Auxiliary StorageDevice

Central Processing Unit ( CPU )

Page 9: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

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

Page 10: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Binary Numbers System

Place Number System Two Digits; 1 and 0 1st position is 2 to 0 power = 1 2nd position is 2 to 1 power = 2 3rd position is 2 to 2 power = 4 4th position is 2 to 3 power = 8 5th position is 2 to 4 power = 16

Page 11: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Binary Numbers System

000001 in binary is equal to 1 base 10 111111 in binary is equal to 63 base 10 000101 in binary is equal to 5 base 10 010000 in binary is equal to 16 base 10 111001 in binary is equal to 57 base 10 000111 in binary is equal to 7 base 10 000110 in binary is equal to 6 base 10

Page 12: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

How Many Possible Digits?

Binary (Base 2) Numbers use 2 digits: JUST 0 and 1

Decimal (Base 10) Numbers use 10 digits: 0 THROUGH 9

Page 13: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Basic Programming Structures

A sequence is a series of statements to be executed one after another.

Selection (branch) is a structure that executes different statements depending on certain conditions.

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

The subprogram is used to break the program into smaller units.

Page 14: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

SEQUENCE

Statement Statement Statement . . .

Page 15: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

SELECTION (branch)

IF Condition THEN Statement1 ELSE Statement2

Statement1 Statement

Statement2

Condition . . .

True

False

Page 16: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

LOOP (repetition)

Statement

Condition . . .False

True

WHILE Condition DO Statement1

Page 17: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

SUBPROGRAM (function)

SUBPROGRAM1 . . .

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

Page 18: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Programming Life Cycle1 Problem-Solving Phase

Analysis and Specification

General Solution ( Algorithm Development )2 Implementation Phase

Concrete Solution ( Program )

Test3 Maintenance Phase

Use

Maintain

Page 19: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Sample Problem

A programmer needs an algorithm to determine an employee’s weekly wages.

How would the calculations be done by hand?

Page 20: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

One Employee’s Wages During one week an employee works 52 hours at the

hourly pay rate $24.75 How much is the employee’s wages? Assume a 40.0 hour normal work week. Assume an overtime pay rate factor of 1.5

40 x $ 24.75 = $ 990.00

12 x 1.5 x $ 24.75 = $ 445.50___________

$ 1435.50

Page 21: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Problem-Solving Phase What information will be used?

INPUT VALUE from outside the program CONSTANT VALUE assumed in program COMPUTED VALUE produced by program OUTPUT VALUE written to file or screen by program

Page 22: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Problem-Solving Phase

INPUT CONSTANTS OUTPUT

Hourly payRate

Hours worked

Normal work hours ( 40.0 )

Overtime pay rate factor (1.5)

Hourly payRate

Hours worked

Wages

COMPUTED VALUE

Wages

Page 23: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

If hours is over 40.0, thenwages = (40.0 * payRate) + (hours - 40.0) * 1.5 *payRate

otherwise,wages = hours * payRate

Weekly Wages, in General

RECALL EXAMPLE ( 40 x $ 24.75 ) + ( 12 x 1.5 x $ 24.75 ) = $ 1435.50

Page 24: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

An Algorithm is . . .

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

Page 25: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Algorithm to Determine an Employee’s Weekly Wages

1. Get the employee’s hourly payRate

2. Get the hours worked this week

3. Calculate this week’s regular wages

4. Calculate this week’s overtime wages (if any)

5. Add the regular wages to overtime wages (if any) to determine total wages for the week

Page 26: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Implementation Phase:Program

Translate your algorithm into a programming language.

With C++, you use

Documentation -- written explanations Compiler -- translates your program

into machine code Main Program -- may call subalgorithms

Page 27: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Implementation Phase: Test

TEST the program using sample input data for which correct results are already known, or can be manually checked.

Use a variety of sample data to test many situations.

If you find errors, analyze the program and algorithm to determine their source, and make corrections.

Page 28: Programming in C++ Dale/Weems/Headington Chapter 1 Overview of Programming and Problem Solving

Maintenance Phase

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