31
Computer Programming Putting the machine under our command

Computer Programming Putting the machine under our command

Embed Size (px)

Citation preview

Page 1: Computer Programming Putting the machine under our command

Computer Programming

Putting the machine under our command

Page 2: Computer Programming Putting the machine under our command

Definition of a Program

A collection of instructions that describe a task, or set of tasks, to

be carried out by a computer

Page 3: Computer Programming Putting the machine under our command

Types of Computer Programs

Firmware

Systems Software

Applications Software

Page 4: Computer Programming Putting the machine under our command

Programming Languages

A programming language is an artificial language that can be

used to control the behavior of a machine.

Page 5: Computer Programming Putting the machine under our command

Early Computer Programming

The first computers were programmed by changing the wiring

Page 6: Computer Programming Putting the machine under our command

Programming Language Background

John von Neumann - stored program

Alan Turing - Turing Machine

Alonzo Church - Lambda calculus

Page 7: Computer Programming Putting the machine under our command

First Generation Languages

Machine level code

Binary

Univac & IBM 701Add reg 1 to reg 2 place results in reg 6 000000 00001 00010 00110 00000 100000

Page 8: Computer Programming Putting the machine under our command

Second Generation Languages

Assembly code

Mnemonic code to represent instructionsMove 61 to register named “al”mov al, 061

Page 9: Computer Programming Putting the machine under our command

Third Generation Languages

Use English-like terms

Compiler to translate to machine level

Fortran, ALGOL, COBOL early examples

COMPUTE NET-PAY = GROSS-PAY - TOTAL-TAX

Page 10: Computer Programming Putting the machine under our command

Fourth Generation Languages

More oriented toward problem solving

State the problem, don’t care how it is solved.

Divided into categories

SQL - Structured Query Language Select * from Employee where Last-Name = “Smith”

Page 11: Computer Programming Putting the machine under our command

Fifth Generation Languages

Set of constraints rather than steps to solve problems

Heuristics

Artificial Intelligence

Prologcat(tom) |- true

Page 12: Computer Programming Putting the machine under our command

Program Development Life Cycle

Step 5Finishing the Project

Step 4Debugging

Step 3Coding

Step 2Making a Plan

Step 1Describing the Problem

Page 13: Computer Programming Putting the machine under our command

Step 1 : Describing the Problem

The problem statement is: The starting point of programming A description of tasks the program is to accomplish How the program will execute the tasks Created through interaction between the programmer and the

user

The program statement includes error handling and a testing plan

Page 14: Computer Programming Putting the machine under our command

PROGRAM GOAL: To compute the total pay for a fixed number of hours worked at a parking garage.

INPUTS: Number of Hours Worked........................ a positive number

OUTPUTS: Total Pay Earned ................................... a positive number

PROCESS: The Total Pay Earned is computed as $7.32 per hour for the first eight hours worked each day. Any hours worked beyond the first eight are billed at $11.73 per hour.

ERROR HANDLING:

The input Number of Hours Worked must be a positive real number. If it is a negative number or other non-acceptable character, the program will force the user to re-enter the information.

TESTING PLAN: INPUT OUTPUT NOTES

8 8*7.32 Testing positive input

3 3*7.32 Testing positive input

12 8*7.32 + 4*11.73 Testing overtime input

–6 Error message/ask user to re-enter value

Handling error

Parking Garage Example

Page 15: Computer Programming Putting the machine under our command

Step 2: Developing an Algorithm

Algorithm development: A set of specific, sequential steps

that describe what the computer program must do

Complex algorithms include decision points:

Binary (yes/no) Loop (repeating actions)

Visual tools used to track algorithm and decision points:

Head off to cafe

Buy textbook

Go to accounting

lecture

Yes No

No

No

Yes

Yes

Wake Up

Check wallet for $

Do I have > $80

Go get gas

Did I get $80 from the ATM?

Do I have my credit card?

Go to the ATM for cash

Page 16: Computer Programming Putting the machine under our command

Flowchart and Pseudocode

Underlined words are information items that appear repeatedly in the algorithm.

1. Ask the user how many hours they worked today2. If the number of hours worked < = 8, compute total pay without overtime otherwise, compute total pay with overtime pay3. Print total pay

Bold terms show actions that are common in programming, such as reading data, making decisions, printing, and so on.

Flowchart Pseudocode

Page 17: Computer Programming Putting the machine under our command

Step 3: Coding

Coding is translating an algorithm into a programming language

Generations of programming languages

Page 18: Computer Programming Putting the machine under our command

Compilation

Compilation is the process of converting code into machine language

Compiler reads the source code and translates it into machine language

After compilation, programmers have an executable program

Page 19: Computer Programming Putting the machine under our command

Interpreter

Interpreter translates source code into a line by line intermediate form

Each line is executed before the next line is compiled

Programmers do not have to wait for the entire program to be recompiled each time they make a change.

Programmers can immediately see the results of changes as they are making them in the code.

Page 20: Computer Programming Putting the machine under our command

Step 4: Debugging

Running a program to find errors is known as debugging

Sample inputs are used to determine runtime (logic) errors

Debugger: Tool that helps programmers locate runtime errors

Page 21: Computer Programming Putting the machine under our command

Step 5: Finishing the Project

Users test the program (internal testing)Beta version released:

Information collected about errors before final revision

Software updates (service packs):Problems found after commercial release

Documentation created:User manualsUser training

Page 22: Computer Programming Putting the machine under our command

Programming Languages

Selecting the right language:Space availableSpeed requiredOrganizational resources availableType of target application

Visual Basic

C / C++Java HTML

JavaScriptVBScript

ASP / JSP

Flash / XML

Page 23: Computer Programming Putting the machine under our command

The CPU: Processing Digital Information

CPU is the brains of the computer

Different types of CPUs Intel and AMD chips: Used in

most Windows-based PCs Apple systems use different CPU

design

Differentiating CPUs Processing power Clock speed and cache

Page 24: Computer Programming Putting the machine under our command

Instruction Set

All commands that the CPU can executeArithmeticLogicDataControl flow

Page 25: Computer Programming Putting the machine under our command

Instruction Set Architecture

CISC Complex Instruction Set Computer Many different instructions Intel - Windows

RISC Reduced Instruction Set Computer Fewer instructions Motorola/Intel - Macintosh

Page 26: Computer Programming Putting the machine under our command

The CPU Machine Cycle

Fetch The program’s binary code is “fetched” from its temporary location

in RAM and moved to the CPU

Decode The program’s binary code is decoded into commands the CPU

understands.

Execute The ALU performs the calculations.

Store The results are stored in the registers

Page 27: Computer Programming Putting the machine under our command

The System Clock

Located on the motherboardControls the CPU’s processing cyclesClock cycle

Pulse or tick

Clock speedNumber of pulses per secondMeasured in hertz (Hz)

Page 28: Computer Programming Putting the machine under our command

The Control Unit

• Manages the switches inside the CPU

Is programmed by CPU designers to remember the sequence of processing stages for that CPU

Moves each switch to its correct setting (on or off) and then performs the work of that stage

Page 29: Computer Programming Putting the machine under our command

The Arithmetic Logic Unit (ALU)

• Part of the CPU designed to perform mathematical operations (addition, subtraction, multiplication, division, etc.)

• Also performs logical OR, AND, and NOT operations

Is fed data from the CPU registersWord size: Number of bits a computer can work

with at a time

Page 30: Computer Programming Putting the machine under our command

Cache Memory

Small amount of memory located on the CPU chip or near it

Stores recent or frequently used instructions and data

Used for quick access by the CPU

Different levels of cache

Page 31: Computer Programming Putting the machine under our command

Software Horror Stories

http://www.cs.tau.ac.il/~nachumd/verify/horror.html