56
Beginning Problem-Solving Concepts for the Computer Lesson 2 McManus COP1000 1

Beginning Problem-Solving Concepts for the Computer

  • Upload
    adsila

  • View
    179

  • Download
    1

Embed Size (px)

DESCRIPTION

Beginning Problem-Solving Concepts for the Computer. Lesson 2. 3 Types of Problems. Computational problems involving some kind of mathematical processing Logical Problems involving relational or logical processing Repetitive - PowerPoint PPT Presentation

Citation preview

Page 1: Beginning Problem-Solving Concepts for the Computer

COP1000 1

Beginning Problem-Solving Concepts for the Computer

Lesson 2

McManus

Page 2: Beginning Problem-Solving Concepts for the Computer

COP1000 2

3 Types of Problems

McManus

Computational• Problems involving some kind of mathematical processing

Logical• Problems involving relational or logical processing

Repetitive• Problems involving repeating a set of mathematical and/or logical instructions.

Page 3: Beginning Problem-Solving Concepts for the Computer

COP1000 3

Fundamental Concepts

• The building blocks of equations and expressions– Constants– Variables– Operators– Functions (predefined)

• more about user-defined functions later

McManus

Page 4: Beginning Problem-Solving Concepts for the Computer

COP1000 4

Constants

• A value– A single specific alphabetical and/or numeric

value – Does not change during the processing of all

the instructions in a solution• Can be of any data type

– Numeric, alphabetical or special symbols• Examples

– 3, 5, 10, “Mulder”, “Scully”, “+”, “-”, “/”

McManus

Page 5: Beginning Problem-Solving Concepts for the Computer

COP1000 5

Constants

• Can be named, in some programming languages

• Provides protection to the constant value and other areas of the program– Cannot be changed once given initial

value– Note: no spaces allowed within names– Examples

• SALES_TAX_RATE = 6• COMMISSION_RATE = 6McManus

Constant names are usually in

ALL CAPS

Page 6: Beginning Problem-Solving Concepts for the Computer

COP1000 6

Variables

• References the memory or storage location where the value is stored

• Values – may change during

processing– may be of any data

type• Data type defines how

big the storage location will be

• Example:– DaysInWeek is

the name of the storage location

– 7 is the valuestored in thatlocation

McManus

7

DaysInWeek

Page 7: Beginning Problem-Solving Concepts for the Computer

COP1000 7

Rules for Naming Variables 1

• Variables are associated with a symbolic name (also known as an identifier)

• A name is assigned to each variable used in a solution.– Examples

• Age, LastName, Address, PayRate

McManus

Page 8: Beginning Problem-Solving Concepts for the Computer

COP1000 8

Rules for Naming Variables 2

• Use mnemonic terms– the name is meaningful– Use PayRate not Z– If the code requires a comment, it’s bad

code because it does not reveal its intent.

Int d; //elapsed time in days

Int elapsedTimeInDays;

McManus

Page 9: Beginning Problem-Solving Concepts for the Computer

COP1000 9

Rules for Naming Variables 3

• Do not use spaces• Do not use special symbols, except

the underscore– Examples

• PayRate, Pay_Rate, PAYRATE, or PAY_RATE

• Use appropriate naming convention

McManus

Page 10: Beginning Problem-Solving Concepts for the Computer

COP1000 10

Rules for Naming Variables 4

• Be consistent! – Some languages are case sensitive– Either combine terms (PayRate) or

include an underscore to separate (Pay_Rate)

McManus

Page 11: Beginning Problem-Solving Concepts for the Computer

COP1000 11

So, what do we store?

Data or Information?

McManus

Page 12: Beginning Problem-Solving Concepts for the Computer

COP1000 12

Data Vs. Information

• Data– The raw, organized facts– PayRate, HoursWorked

• Information– The meaningful output– The Paycheck

McManus

Page 13: Beginning Problem-Solving Concepts for the Computer

COP1000 13

Data Types

• Most common primitive data types– Numeric– Character– Logical

• Most languages include other data types– Date– Currency– Strings– User-defined

McManus

Page 14: Beginning Problem-Solving Concepts for the Computer

COP1000 14

Rules for Data Types

– All data to be used in calculations must be declared as a numeric data type

– Each data type uses a data set or domain

– Integers - -32768 -> 0 -> 32767 but is programming language dependent

– Characters – all alphanumeric characters included in the computer’s coding scheme

– Logical – the words “true” and “false”

– Data types cannot be mixed• Called data typing

– Programmer designates the data type McManus

Page 15: Beginning Problem-Solving Concepts for the Computer

COP1000 15

Data Types & Data Sets

Data Type Data Set ExamplesInteger All whole numbers 3456

-43

Real All real numbers (whole + decimal)

3456.78-123.45

0.000123

Character (uses quotation marks)

All letters, numbers, and special symbols

“A”, “a”, “1”, “+”, “%”

String (uses quotation marks)

Combinations of more than one character

“Mulder”“123-45-6789”

Logical True or False TrueFalse

McManus

Page 16: Beginning Problem-Solving Concepts for the Computer

COP1000 16

Numeric Types

• Includes all types of numbers– Natural Numbers - The set of positive

(counting) numbers • Ex. {1,2,3,4,5,…}

– Integers - The set of real numbers consisting of the natural numbers, their additive inverses, and zero• Ex. 3, 5, -5, 0, -1 and 32,767

– Real numbers (floating point numbers)• Ex. 3.1459, 1.618039887, -5745 & Scientific

NotationMcManus

Page 17: Beginning Problem-Solving Concepts for the Computer

COP1000 17

Can I Confuse You?

• Which set is larger?

The set of Integers or the set of Real Numbers?

• Well, actually…• There are several different levels of

infinity! Confused?

McManus

Page 18: Beginning Problem-Solving Concepts for the Computer

COP1000 18

Levels of Infinity

Complex Numbers

Real Numbers

Irrationals

Rationals

Integers

Naturals

McManus

Naturals: Counting Numbers

Integers: Naturals, Additive Inverses & 0

Rationals: Numbers expressed as a ratio between 2 Integers; Ex. 1/10

Irrationals:

Reals: All numbers on number line

Complex Numbers: Real Numbers, Imaginary Numbers

Page 19: Beginning Problem-Solving Concepts for the Computer

COP1000 19

Character Types

• Alphanumeric data set– Consists of

• all single-digit numbers, • letters, and • special characters available to the computer

– contained within quotation marks

McManus

Page 20: Beginning Problem-Solving Concepts for the Computer

COP1000 20

Coding Systems

– EBCDIC • Extended Binary Coded Decimal Information

Code– ASCII

• American Standard Code for Information Interchange

• 256 characters (1 byte)– First 128 – standard– Second 128 – specific to computer

– Unicode (2 bytes)• Replacing ASCII as standard on PC’s

McManus

See Appendix

B or online

Page 21: Beginning Problem-Solving Concepts for the Computer

COP1000 21

String Data Type

• Made up of one or more characters.• Contained within quotation marks• Cannot be used within calculations

– There is some debate about using strings…• The new consensus is that if the value will be used in

a mathematical calculation, make it a number, otherwise make it a string.

• The alternative view (older view) is to look at the amount of storage that is needed for the value. A number generally takes up less memory space than a string.

McManus

Page 22: Beginning Problem-Solving Concepts for the Computer

COP1000 22

Strings - Caution

• “123” is not the same thing as 123– The first one is a string– The second one is a number

• Becomes important when deciding when a piece of data is a string or a number

• Ex. Social Security Numbers– The first one contains 11 characters and is left

aligned whereas the second contains 9 numbers and is right aligned

McManus

Page 23: Beginning Problem-Solving Concepts for the Computer

COP1000 23

Comparing Characters & Strings

– The computer’s particular coding scheme (ASCII, Unicode, or EBCDIC) converts the character to its associated ordinal number• Note: Uppercase letters have a smaller

ordinal values than lowercase letters– “A” = 193 whereas “a” = 129 (Unicode 16-bit)– “A” = 65 whereas “a” = 97 (ASCII 8-bit)

– Once converted, each character, one at a time, can be compared

McManus

Page 24: Beginning Problem-Solving Concepts for the Computer

COP1000 24

Concatenation

• Joins character or string data together– Operators: + and &

• Dependent on programming language• & can mix data types• + cannot mix data types (also known as Join

in databbases)• Examples

– FName & “ ” & MI & “ ” & LName– “James” + “ ” + “T.” + “ ” + “Kirk” = “James T.

Kirk”– “James” & “ ” & “T.” & “ ” & “Kirk” = “James T.

Kirk”» Note the space at the end of the T.

McManus

Page 25: Beginning Problem-Solving Concepts for the Computer

COP1000 25

Logical (Boolean) Data Type

• Consists just two pieces of data– True and False– Different programming languages also

allow• Yes and No• 1 (True) and 0 (False) • On and Off

McManus

Page 26: Beginning Problem-Solving Concepts for the Computer

COP1000 26

Other Pre-defined Data Types

• Dependent on programming language, numerous other pre-defined data types are available.– Examples

• Dates 01/01/1900 January 1, 1900

• Times03:35:05 PM 15:35:05• Currency 3000.00

– Note: This is how the data will be stored, not how it is formatted.

McManus

Page 27: Beginning Problem-Solving Concepts for the Computer

COP1000 27

User-Defined Data Types

• User-defined Data types– Defined by the user as a new data type

• Example– Record

McManus

Employee_RecordFnameLnameSSNBirthDateHireDateTerminationDate

PayRateEnd

Page 28: Beginning Problem-Solving Concepts for the Computer

COP1000 28

Functions

• Small sets of instructions that perform specific tasks and return values.

• Two types:– Pre-defined

• Built into a computer language or application

– User-defined • More about these later

McManus

Page 29: Beginning Problem-Solving Concepts for the Computer

COP1000 29

Functions

• Benefits– Reduces the amount of code that needs

to be written, thus reducing errors of repetition

– Shortens the development time– Improves readability

McManus

Page 30: Beginning Problem-Solving Concepts for the Computer

COP1000 30

Function Parameters

• Data usually placed within parentheses that the function uses without altering the data– In Sqrt(n), the n represents the

parameter, in this case a number– In Value(s), the s represents a string

McManus

Page 31: Beginning Problem-Solving Concepts for the Computer

COP1000 31

Function Types

• Mathematical • String• Conversion• Statistical • Utility• Specific to each programming

language…a partial list follows

McManus

Page 32: Beginning Problem-Solving Concepts for the Computer

COP1000 32

Mathematical Functions

McManus

Function Form Example Result

Square Root Sqrt(n) Sqrt(4) 2

Absolute Abs(n) Abs(-3), Abs(3) 3

Rounding Round(n,n1) Round (3.7259,2) 3.73

Integer Integer(n) Integer(5.7269) 5

Random Random Random 0.239768

Sign of a number Sign(n)

Sign(7.39)Sign(0)Sign(-7.39)

10

-1

Random is a random number selected between 0 and 1

Page 33: Beginning Problem-Solving Concepts for the Computer

COP1000 33

String Functions

McManus

Function Form Example Result

Middle String Mid(s, n1, n2)Mid(S,3,2) where S = "Thomas" "om"

Left String Left(s, n)Left(S,3) where S = "Thomas" "Tho"

Right String Right(s, n)Right(S, 3) where S = "Thomas" "mas"

Length String Length(s)Length(S) where S = "Thomas" 6

Page 34: Beginning Problem-Solving Concepts for the Computer

COP1000 34

Conversion Functions

McManus

Function Form Example Result

Valued Conversion Value(s) Value("-12.34") -12.34

String Conversion String(n) String(-12.34) "-12.34"

Page 35: Beginning Problem-Solving Concepts for the Computer

COP1000 35

Statistical Functions

McManus

Function Form Example Result

Average Average(list) Average(5, 3, 8, 6) 5.5

Maximum Max(list) Max(5, 3, 8, 6) 8

Minimum Min(list) Min(5, 3, 8, 6) 3

Summation Sum(list) Sum (5, 3, 8, 6) 22

Page 36: Beginning Problem-Solving Concepts for the Computer

COP1000 36

Utility Functions

McManus

Date Date Date 9/14/2002

Time Time Time 9:22:38

Now Date/Time Time 5/17/2016 9:22

Error is a special function that appears in most languages and upon execution return control to the program when (not if) system errors occur. Such as Disk Errors…

Page 37: Beginning Problem-Solving Concepts for the Computer

COP1000 37

Operands, Resultants & Operators

• Operands– The data that the

operator connects and processes

• Resultant– The answer that

results when the operation is executed

• Operators– The data connectors

within expressions and equations.

– Two types of operations• Unary

– Negation• Binary

– Addition, Subtraction, Multiplication, Floating Point Division and Integer Division

McManus

Page 38: Beginning Problem-Solving Concepts for the Computer

COP1000 38

Operator Types

• Mathematical– +, -, *, /– \, Mod {Integer Division & Modulo}– ^, {Exponentiation} and – functions

• Relational– <, >, <=, >=, =, <>

• Logical– And, Or, Not, XOR, EQV

McManus

Page 39: Beginning Problem-Solving Concepts for the Computer

COP1000 39

First: Two Special Math Operators

• Integer Division (\)– Regular Floating

Point Division, except only the whole number part of the answer is returned.

– If given a Floating Point number, the number is rounded—first—before division occurs.

• Modulo Division (Mod) or Modulus– Remainder part of

answer is returned

McManus

Page 40: Beginning Problem-Solving Concepts for the Computer

COP1000 40

The Boolean Data Type

• Traditional:– Returns either the keywords True or

False• Variation:

– Returns a non-zero or zero value.• -9, -1, 1, 2 are equivalent to True• 0 is equivalent to False

• Is stored in two bytes

McManus

Page 41: Beginning Problem-Solving Concepts for the Computer

COP1000 41

Relational Operators

• Perform comparisonsvariable - relational operator - variable

– Ex. If Y > Z then ...

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

McManus

Page 42: Beginning Problem-Solving Concepts for the Computer

COP1000 42

Logical (Boolean) Operators

• And, Or, Not, XOR, EQV• Boolean Expressions are used

– To create more complicated Boolean Expressions.

– If a = b AND c < d Then …• They are defined by using Truth

Tables…– Know the significance of each!

McManus

Hint!These are

on the Test!

Page 43: Beginning Problem-Solving Concepts for the Computer

COP1000 43

The AND Operator

• Significance: The only time the result is True is if both A and B are True.

McManus

A = B = then A AND BTrue True TrueTrue False FalseFalse True FalseFalse False False

Page 44: Beginning Problem-Solving Concepts for the Computer

COP1000 44

The OR Operator

• Significance: The only time the result is False is if both A and B are False.

McManus

A = B = then A OR BTrue True TrueTrue False TrueFalse True TrueFalse False False

Page 45: Beginning Problem-Solving Concepts for the Computer

COP1000 45

The NOT Operator

• Also called the “logical complement” or “logical negation”

• Significance: Whatever the value of A is, NOT A will be the opposite.

McManus

A = NOT A

True FalseFalse True

Page 46: Beginning Problem-Solving Concepts for the Computer

COP1000 46

The XOR (Exclusive Or) Operator

• Significance: The only time the result is True is if A and B are different and False if both A and B are the same.

McManus

A = B = then A XOR BTrue True FalseTrue False TrueFalse True TrueFalse False False

Page 47: Beginning Problem-Solving Concepts for the Computer

COP1000 47

The EQV (Equivalent) Operator

• Significance: The only time the result is True is if A and B are the same and False if both A and B are the different.

McManus

A = B = then A EQV BTrue True TrueTrue False FalseFalse True FalseFalse False True

Page 48: Beginning Problem-Solving Concepts for the Computer

COP1000 48

Hierarchy of Operations

• Data and Operators are combined to form expressions and equations

• To evaluate these in the proper order, a hierarchy of operations, or Order of Precedence, is used.– Note: Different programming languages

use different Orders of Precedence…– Note: Whenever you want to clarify an

equation, use the parentheses!

McManus

Page 49: Beginning Problem-Solving Concepts for the Computer

COP1000 49

The Order of Precedence

McManus

OPERATOR TYPE NAME ( ) Parentheses Parentheses ^ Arithmetic Exponent (Powers) * / Arithmetic Multiplcation, floating-point division\ Arithmetic Integer division

Mod Arithmetic Modulus + - Arithmetic Addition, subtraction

= <> <= >=

> <

Comparison (all have same level of precedence

Equal to, not equal to, less than or equal to, greater than or equal to, greater than, less than

NOT Logical Logical Negation AND Logical Logical And OR Logical Logical Or

XOR Logical Logical Exclusive Or EQV Logical Logical Equivalent

Page 50: Beginning Problem-Solving Concepts for the Computer

COP1000 50

Example

• Evaluate the following:• A = True, B = False, C = True, D =

False

• To do this, we create what’s called an Evaluation Tree…

McManus

A or B or C and D and A and B and (not C) or (not D)

Page 51: Beginning Problem-Solving Concepts for the Computer

COP1000 51

The Evaluation Tree

McManus

A or B or C and D and A and B and (not C) or (not D)

False True

False False False

False True

True True

1

2

4

3

6

5

7

8

Page 52: Beginning Problem-Solving Concepts for the Computer

COP1000 52

Expressions & Equations

• Expressions– Process the data and the operands,

through the use of the operators– Does not store the resultant (answer)– Examples

• Price * SalesTaxRate• (Hours – 40) * Wage * 1.5• Height * Width

McManus

Page 53: Beginning Problem-Solving Concepts for the Computer

COP1000 53

Equations

• Same as an expression, except the equation stores the resultant (answer) in a memory location (must be on the left side of Assignment Operator)

• “takes on the value of” not “equals”– Examples

– SalesTax is the memory location where the answer will be stored, the resultant

• Often called Assignment Statements

McManus

SalesTax = Price * SalesTaxRateOvertimePay = (Hours – 40) * Wage * 1.5Area = Height * Width

Page 54: Beginning Problem-Solving Concepts for the Computer

COP1000 54

More about Equations

• Destructive Read-In– The value stored in

the variable on the left-hand side of the equals sign is replaced by the result of the expression on the right-hand side.

• Non-Destructive Read-In

• Values used on the right-hand side are protected from being changed.

McManus

SalesTax = = Price ** SalesTaxRate D R-I ND R-I ND R-I

Ahh..but what about this one? Count = Count + 1

Page 55: Beginning Problem-Solving Concepts for the Computer

COP1000 55

Straight-Line Form

• Algebraic Expressions cannot be represented in the computer.

• They must be converted to a form the computer will recognize.– Thus, Straight Line Form

McManus

2

2

)()2(

)(2

YXYX

YXY

= ( 2 *(X – Y ) ) / ( ( ( 2 * X + Y )^2 ) / ( ( X – Y )^2 ) )

straight line form

Page 56: Beginning Problem-Solving Concepts for the Computer

COP1000 56

Next?

McManus