calculators are used to increase speed and accuracy of numerical computations the abacus has roots...

62
Calculators are used to increase speed and accuracy of numerical computations The abacus has roots dating back over 5,000 years Mechanical calculators have been relatively commonplace since late 19 th century What is a computer? A mechanical or electronic device Stores, retrieves, manipulates large amounts of data (information) at high speed, with great accuracy Does not need human intervention Carries out instructions from a program

Upload: maximillian-malone

Post on 27-Dec-2015

235 views

Category:

Documents


2 download

TRANSCRIPT

Calculators are used to increase speed and accuracy of numerical computations

The abacus has roots dating back over 5,000 years

Mechanical calculators have been relatively commonplace since late 19th century

What is a computer?A mechanical or electronic device

Stores, retrieves, manipulates large amounts of data (information) at high speed, with great accuracy

Does not need human intervention

Carries out instructions from a program

Early Computers– 1945 – 1950’s: First generation computers

• used vacuum tubes to do internal switching needed for computations

• 1955: about 300 computers in the world built mostly by IBM and Remington Rand, based on vacuum tubes.

– Late 1950s: invention of the transistor was one of most important inventions of 20th Century

• computers based on the transistor are the first solid-state computers.

– Early 1960’s: DEC created the minicomputer (Servers) – about the size of a file cabinet.

• Used small packages of transistors called integrated circuits

– Mainframes, such as the IBM 360 are prominent in large companies and Universities.

The Personal Computer– 1970s: The personal computer becomes

available with invention of the microchip– 1974: The microchip, along with the invention

of the microprocessor led to creation of first personal computer

– Bill Gates and Paul Allen founded Microsoft Corporation

– Stephen Wozniak and Steven Jobs founded Apple Computer, Inc.

Computers Today

• Currently:– PCs: 95% use Microsoft Windows operating

system with a huge array of available software– Minicomputers (servers) are still popular with

small business and universities.– Mainframes are in use at large corporations.– Supercomputers are very powerful and

specialized • Used for massive computing problems by big

corporations and government departments.

Components and Operations• Hardware: equipment, or devices

• Software: programs that contain instructions which tell the computer what to do – created by programmers

• Hardware and software: the two major

• components of any computer system

• Major operations in a computer:– Input– Processing– Output

• Input devices: allow data to enter the computer– Mouse, keyboard, scanner

• Output devices: allow processed data to be exported out of the computer– Printer, monitor, speakers

• Processing: working on the data; such as:– Organizing data – Checking data for accuracy – Mathematical or other manipulations on data

• Central Processing Unit (CPU): hardware that performs the tasks

• Programming language: • Special language (like French or German)

• Understood by the computer

• Tells the computer what to do

• Used by a programmer to write a program– Visual Basic, Java, C#, C++, COBOL

• Machine language: • Language of the computer

• Every “Programmer” language is converted to machine language

• Consists of bits on or off only

• Compiler or interpreter: • Software that translates programming languages to machine

language

• Components of a computer

– Central Processing Unit (CPU)

– Internal memory• RAM (Random Access Memory)• ROM (Read Only Memory)

– Mass storage devices• Magnetic, optical, and solid-state

– Input devices such as keyboard and mouse– Output devices such as monitor and printer

Central Processing Unit (CPU)

• Often called the brain of the computer– Carries out program instructions ( in memory)– Performs arithmetic and logical operations

Arithmetic Logical Unit (ALU)– Controls other computer components

(operating system)

• Consists of millions of transistors on a single microchip that plug into the motherboard

Internal Memory

• ROM: read-only memory– Contains instructions used by computer

during startup– Cannot be altered by computer user

• RAM: random-access memory– Is a “scratch pad” for user as he or she works– Can be read from and written to– Operating System/Applications reside

• Storage categories: internal and external

• Internal storage: – Main memory, random access memory (RAM)– Located inside the computer system– Volatile: contents are lost when power goes

down

• External storage:– Persistent: contents are relatively permanent– Floppy drive, hard drive, flash media, magnetic

tape– Located outside the computer system

Mass Storage Devices

• Magnetic storage– Hard disks, floppy disks, zip drives– Hard disks are internal, others are external

• Optical storage– CDs and DVDs

• Solid-state storage– Flash drives plug into a USB port

Software• System Software

– Control system resources– Operating systems – Windows, DOS, Linux, UNIX, dll,

utilities, drivers

• Application Software – Interract with a user– Word Processors (MS Word)– Database Managers (Access)– Spreadsheets (Excel)– Photo Editors– Web browsers, email programs (Netscape)

What is Programming?

• A program is a list of instructions that is executed by a computer to accomplish a particular task.

• Creating those instructions is programming by a programmer

• Coding the program:– Select the programming language– Write the instructions

• Using software to translate the program into machine language:– Programmers write instructions in English-like

high-level languages– Compilers or interpreters change the programs

into low-level machine language that can be executed

– Syntax errors are identified by the compiler or interpreter

Programming Languages– Machine Languages

• 0110110111110111 0000000100000000 0000000100000000

– Assembly Languages• One programmer instruction converted to 1 machine level

instruction• ADD A,B

– High-level Languages• Looks more like English words and algebraic expressions• Examples of high level languages• Programmer instruction converted to many machine level

» ADA BASIC

» C++ FORTRAN

» COBOL Java

» Pascal Visual Basic

Programmer written computer code

Translator (Interpreter) Program

(browser)

Compiler Assembler

Many machine Language Instructions

One Machine Language Instruction

Execute

.exe file

Programmer written computer code

Execute

FUNCTIONBrowsersnetscapeMS ExplorerJavascriptPHPASP

MS Word, Windows XP, MS Excel, Vista, MS Explorer

Machine Language Generation

Writing Programs

• To write a program in a high-level language, you need:– Appropriate software– A text editor to type and edit program

statements– A debugger to help find errors in program

code– A compiler or interpreter to translate the

program into machine language

Error Types

• Logic errors are more difficult to locate than syntax errors

Syntax: the rules governing word usage and punctuation in the language(format, recipe, formula)

• A program must be free of syntax errors to be run, or executed, on a computer

• The program will not compile or be translated• A message will point out the offending instruction

• To function properly, the logic must be correct

• The program will still execute• The output may be incorrect• The program may exit prematurely• The program may ABEND (Abnormal Termination)• The program may execute with no errors/problems

Logic

• A program must be free of syntax errors to be run, or executed, on a computer

• To function properly, the logic must be correct• What’s wrong with this logic for making a cake?

Programming Process

• Seven programming phases:– Define the Problem– Outline the Solution– Develop the Outline into an Algorithm– Test the Algorithm for Correctness– Code the Algorithm in a specific language– Run the program on the computer– Document and maintain the program

• Define the Problem: – May be the most difficult phase– 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– If this is not correct – the rest of the

project cannot be correct

• Outline the Solution:– Plan the high-order steps that the program will

take– Walk through the logic before coding by desk-

checking the logic

• Develop the Outline into an Algorithm:– Use tools such as flowcharts and pseudocode– Flowchart: a pictorial representation of the

logic steps– Pseudocode: English-like representation of the

logic

• Test the Algorithm for Correctness:– Walk through the logic before coding by desk-

checking the logic– Make corrections to the Flowchart/Pseudocode

as needed

• Code the algorithm in a specific language:– Select the programming language– Write the instructions

• Using software to translate the program into machine language:– Programmers write instructions in English-like high-level

languages– Compilers or interpreters change the programs into low-

level machine language that can be executed– Syntax errors are identified by the compiler or

interpreter

• Run the program on the computer:– Test for Syntax errors (when compiling)– Test for logic errors by using selected data

• Document and maintain the program:– Create user documentation– Create technical documentation– Create documentation library– Monitor processing and outputs

Syntax Error Cycle

• Testing the program:– 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

• Putting the program into production– Do this after testing is complete and all known errors

have been corrected– May require coordination with other related activities

or software

• Variable: a memory location whose contents can vary; also called an identifier

• Each programming language has it own rules for naming identifiers, including:– Legal characters– Maximum length– Use of upper or lower case

• Variable name must be a single word, but can be formed from several words– rate, interestRate, interest_rate

• Constant: value does not change– Values that do not change while the program is running– Have identifiers, and can be used like variables for calculations,

but cannot be assigned new values

• Literal: is use of data itself (ex: pi – 3.1474)

Data Types

• Basic data types:– Text– Numeric

• Numeric data – stored by numeric variables

• Text data – stored by string, text, or character variables –

– surrounded by “ “ (ie. “color”)

Types of Data• Numeric Data

– Integer data (whole numbers)• 10 25 -45 0

– Real (Floating point) data (numbers that have a decimal point)

• 23.5 -5.0

– Note: 5 and 5.0 are stored differently in a computer even though they have the same value. The first, 5 is an Integer but the second, 5.0 is a Real number.

• Character data (alphanumerics)– All the characters you can type at the keyboard– The type is String or Character

• Numeric data types:

– Integer: whole numbers only

– Floating-point: fractional numeric values with decimal points

• Numeric Data– Integer data (whole numbers)

• 10 25 -45 0

– Real (Floating point) data (numbers that have a decimal point)

• 23.5 -5.0

– Note: 5 and 5.0 are stored differently in a computer even though they have the same value. The first, 5 is an Integer but the second, 5.0 is a Real number.

• Character data (alphanumerics)– All the characters you can type at the keyboard– The type is String or Character

Concatenation

• Concatenation takes two strings and joins them to create a string result

• The concatenation operator is symbolized, in pseudocode, with a + sign

• Example: if:• String1 = “yellow” and String2 = “duckie” • then the statement:• Set MyFriend = String1 + String2 • results in: • MyFriend = “yellow duckie”

– 1 bit (0 or 1)– 4 bits = nibble– 1 byte = 8 bits = 2 nibbles

• Is the storage for one character

– 1 word = 4 bytes– 1 K = 1 KB = 1 kilobyte = 1,0240 bytes = 210 bytes

• Kilo = 1,000 : 1 KB = 1,000 bytes

– 1 Meg = 1 megabyte (MB) = 1 million = 1024 KB – 1 GB = 1 gigabyte (GB) = 1024 MB = 1,000,000,000– Many people approximate to steps of 1000, not 1024

Binary Machine Language –

all data types consist of 1s and 0s

• Data Groupings: File– Character: single symbol (letter, number,

special symbol) • “A”, “7”, “$”

– Field: group of characters forming a single data item• “Smith”

– Record: a group of related fields• Customer record containing name and

address fields

– File: a group of related records• Customer file, containing all customer records

• Data Groupings: Database– Character: single symbol (letter, number,

special symbol) • “A”, “7”, “$”

– Field: group of characters forming a single data item

• “Smith”

– Table: a group of related fields• Customer record containing name and address fields

– Database: collection of related tables, that serve the information needs of the organization

Using Flowchart Symbols and Pseudocode Statements

• Flowchart: pictorial representation of the logic

• Pseudocode: English-like representation of the logic– Example:

• Flowchart input symbol:

• Flowchart processing symbol

• Flowchart output symbol:

• Flowlines: – Connect the steps – Show the sequence of statements– Have arrows to show the direction

• Terminal symbol (start/stop symbol): – Shows the start and end points of the

statements– Lozenge shape

Flowchart vs. Psuedocode

• Back-pointing arrows show statements that will be repeated

• Infinite loop: a sequence of statements that repeats forever with no escape

• Avoid infinite loops by testing for a predetermined value that means “stop processing”

• Decision: testing a value• Flowchart decision symbol: a diamond

shape, with two flowlines, one for Yes and one for No

• Sentinel value (or dummy value)– Does not represent real data– Signal to stop– Can be used with input from files or from users

• 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 connector symbol:– Marks a logic transfer to another location in the

flowchart– Transfer location can be on the same page or on

another page– On-page symbol: a circle with a number or

letter to identify the matching transfer location

– Off-page symbol: a square with a pointed bottom, containing page number and a number of letter to identify the matching transfer location

• Assignment statement: – Assigns a value to a variable– Variable must appear on the left side, value on the

right side of the assignment operator– Right side may be an expression that will be evaluated

before storing the value in the variable

• Assignment operator: the equal sign (=) in most languages

• Variable: – Memory location: has an address and a value– Value (contents) is used for various operations

Arithmetic Operations

• + Addition 2 + 3 = 5

• - Subtraction 7 – 3 = 4

• * Multiplication 5 * 4 =20

• / Division 12 / 3 = 4

• ^ Exponentiation 2 ^ 3 = 8

• % Modulus 14 % 3 = 2

Hierarchy of Operations

• 1st: perform operations inside parentheses (from inside out if more than one)

• 2nd: perform exponentiation

• 3rd: do multiplications, divisions, and modulus from left to right (if there are more than one)

• 4th: do additions and subtractions from left to right (if there are more than one)

Example of Hierarchy of Operations

• 3 * (6 + 2) / 12 – (7 – 5) ^ 2 * 3 = ?

• ( ) first: = 3 * 8 / 12 – 2 ^ 2 * 3

• ^ next: = 3 * 8 / 12 – 4 * 3

• Leftmost * next: = 24 / 12 – 4 * 3

• Division next: = 2 – 4 * 3

• Multiply next: = 2 – 12

• Subtract last: = -10

Relational Operators• Relational operators are the symbols used

in the condition to be evaluated in If statements:

• = equal to• <> not equal to• < less than• > greater than• <= less than or equal to• >= greater than or equal to

Comparison vs. Assignment Operators• The equals sign (=) in this text may have two different

meanings. The difference is very significant. • As an assignment operator, the equals sign sets the

value of an expression on the right side to the variable on the left side.

• As a comparison operator, the equals sign asks the question, “Is the value of the variable on the left side the same as the value of the expression, number, or variable on the right side?”

• Many programming languages distinguish between these two operators as follows:

• a single equals sign (=) signifies the assignment operator• a double equals sign (==) signifies the comparison

operator• This is demonstrated in the examples that follow in the

next slides.

Logical Operators

• Logical operators are used to connect simple conditions into a more complex condition called a compound condition.

• AND OR NOT

Hierarchy of OperationsType Operator Order Performed

Arithmetic operations

are performed first, in order shown

( )

^

* / %

+ -

1st parentheses

2nd exponentiation

3rd: multiplication, division, modulus

4th: addition, subtraction

Relational operations

are performed second

= <> < <= > >=

All relational operators have equal precedence

Logical operations

are performed last, in the order shown

NOT

AND

OR

1st: NOT

2nd: AND

3rd: OR

• Two major programming techniques:– Procedural programming– Object-oriented programming

• Procedural programming: focuses on the procedures that programmers create

• Object-oriented programming: focuses on objects that represent real-world things and their attributes and behaviors

• Both techniques employ reusable program modules

Annotate the Output

• If the output consists of numbers or any data that has no explanatory text with it, you should annotate your output – this means to add some text so the user knows what the output means.