Top Banner
Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic CMPF144 Introduction to Problem Solving and Basic Computer Computer 1
44

Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Dec 22, 2015

Download

Documents

Berenice Rose
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Chapter 2Beginning Problem-Solving Concepts for the Computer

CMPF144 Introduction to Problem Solving and Basic CMPF144 Introduction to Problem Solving and Basic ComputerComputer

1

Page 2: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

2

OverviewOverview

Constants and Variables Data Types Functions Operators Expressions and Equations

Page 3: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

ObjectivesObjectives1. Differentiate between variables and constants.

2. Differentiate between character, numeric, and logical data types.

3. Identify operators, operands and resultants.

4. Identify and use functions

5. Identify and use operators according to placement in hierarchy chart.

6. Set up & evaluate expressions and equations using variables, constants, operators, and the hierarchy of operations.

3

Page 4: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

ConstantConstant

A value (content in memory cell, can be alphabetic and/or numeric) that will nevernever change during the execution of computer program

is referred to by its name (location of memory cell) Name is given in CAPITAL LETTER to differentiate

from variable Example: PI = 3.142

4

Page 5: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

VariableVariable A value (can be any data type)that mightmight change during

the execution of computer program is referred to by its name also known as identifier there are rules for naming and using variables Example:

number_of_student = ??

total_amount =??

depends on program execution

5

Page 6: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Rules for Naming and Using Rules for Naming and Using VariablesVariables1. Name a variable according to what it represents.

2. Do not use spaces.

3. Start a variable name with a letter.

4. Do not use a dash or any other symbol that is used as a mathematical operator.

5. Consistent usage of variable name.

6. Consistent use of upper, lowercase characters in variable names

7. Use naming convention specified by your company

6

Page 7: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Incorrect Variable NamesIncorrect Variable Names

7

Data Item Incorrect Variable Name Problem Corrected Variable Name

Hours worked Hours Worked Space between words HoursWorked

Name of client CN Does not define data item ClientName

Rate of pay Rate-Pay Uses a mathematical operator

PayRate

Quantity per customer

Quantity/Customer Uses a mathematical operator

QuantityPerCustomer

6% sales tax 6%_sales_tax Starts with a number SixPercentSalesTax@

SalesTax

Client address Client_address_for_client_of_XYZ_corporation_in_California

Too long ClientAddress

Variable name introduced as Hours

Hrs Inconsistent name Hours

Variable name introduced as Hours

Hours_worked Inconsistent name Hours

pp 51, WHAT’s WRONG WITH THIS? Q1pp 51, WHAT’s WRONG WITH THIS? Q1

Page 8: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Data TypeData Type a classification of the type of data that a variable or

constant can hold in computer programming Each data type consists of a set of permitted values,

which is known as data set Common categories of data type:

Numeric Character String Logical Date

8

Page 9: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

NumericNumeric Include all types of numbers Can be used for numeric calculations

2 + 1 = ??

Subtypes include: Integer

o Whole number

o Can be positive of negative

o Example: 5297, -376

Real number o Also known as floating point numbers

o Can be represented in scientific notation

o Example: 0.0054, 2.3E5 (2.3 x 105)9

Page 10: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Numeric Data Types and Their Numeric Data Types and Their Data SetsData Sets

10

Page 11: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Character / StringCharacter / String Consists of all single-digit numbers, letters and special

characters available to the computer Placed within quotation marks

Example: “a”, “2”, “=“

Cannot be used for calculation even though some are numbers “2” + “1” = ??

Uppercase letter is different from lower case letter “A” ≠ “a”

more than one character are combined together to form a string

11

Page 12: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Character / String (cont.)Character / String (cont.)

Can be compared and sorted in alphabetical order (computer gives each character an ASCII number) Banana > Apple

Joan > James

A < a

Can be joined together via concatenation (by using + operator) “Hello” + “World” = “HelloWorld”

“4” + “4” = ??

12

Page 13: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Character / String Data Types and Character / String Data Types and Their Data SetsTheir Data Sets

13

Page 14: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

LogicalLogical Only consists of two value – True or False Used to make yes-or-no decisions Example:

result = True

badInput = “yes”

CreditOK = 1

14

Page 15: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Logical Data Types and Logical Data Types and Their Data SetsTheir Data Sets

15

Page 16: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

DateDate

Is a numeric data type as mathematical operations can be performed onto the value

Allows user to subtract one date from another date

16

Page 17: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Examples of Data TypesExamples of Data Types

17

Page 18: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Examples of Data Types Examples of Data Types (cont.)(cont.)

18

Page 19: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

FunctionsFunctions

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

Requires parameter General syntax : FunctionName (parameter)

Example: Sqrt(N) – What is this?? (╥﹏╥ )

Syntax might vary for different programming language

Can be used repeatedly in a program to shorten the problem-solving time and increase program’s readability

19

Page 20: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Examples of FunctionsExamples of Functions

20

Page 21: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Examples of Functions Examples of Functions (cont.)(cont.)

21

Page 22: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

22

Examples of Functions (cont.)Examples of Functions (cont.)

Page 23: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

23

Examples of Functions (cont.)Examples of Functions (cont.)

Page 24: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

OperatorsOperators Tells the computer HOW to process data (example: add, subtract…) Informs the computer WHAT type of processing (example:

mathematical, logical…) Two main concepts: OPERAND and RESULTANT Types of operators:

Mathematical (standard mathematical calculation)

Relational (to program decision)

Logical (to connect relational expression and to perform operations on logical data)

24

Page 25: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Mathematical Operators Mathematical Operators and Their Computer and Their Computer

SymbolsSymbols

25

Page 26: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Relational Operators and Relational Operators and Their Computer SymbolsTheir Computer Symbols

26

Page 27: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Logical Operators and Their Logical Operators and Their Computer SymbolsComputer Symbols

27

Page 28: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

NOT Logical OperatorsNOT Logical Operators

28

Page 29: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

AND Logical OperatorsAND Logical Operators

29

Page 30: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

OR Logical OperatorsOR Logical Operators

30

Page 31: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Hierarchy of OperationsHierarchy of Operations

31

Page 32: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Expressions and EquationsExpressions and Equations Expression:

Used to process data (operands) through operators

Example: owidth * height

Equation: Also known as assignment statements

Stores the resultant of an expression in a memory location of a computer through equal “=” symbol (assignment operator)

Example: o area = width * height

32

Page 33: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Expressions and Equations Expressions and Equations (cont.)(cont.)

33

Page 34: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Setting Up a Numeric Setting Up a Numeric ExpressionExpression

Mathematical expression:

X (3Y + 4) -

Appropriate computer representation:

4Y X + 6

X * ( 3 * Y + 4) – 4 * Y / ( X + 6)

34

Page 35: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Setting Up a Mathematical Setting Up a Mathematical EquationEquation Example:

Appropriate computer representation:

Only one variable on the left and an expression on the right of the “=” sign

Y + 3 = X ( Z + 5)

Y = X * ( Z + 5) - 3

35

Page 36: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Setting Up a Relational Setting Up a Relational ExpressionExpression Given an expression :

Appropriate computer representation:

Computer can’t understand “is lesser than”

X is lesser than Y + 5

X < Y + 5

36

Page 37: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Evaluating a Mathematical Evaluating a Mathematical ExpressionExpression

37

Page 38: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Evaluating a Relational Evaluating a Relational ExpressionExpression

38

Page 39: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Evaluating a Logical Evaluating a Logical ExpressionExpression

39

Page 40: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Evaluating Equation That Uses Evaluating Equation That Uses Both Relational and Logical Both Relational and Logical

OperatorsOperators

40

Page 41: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Developing a Table of All Developing a Table of All PossiblePossible

Resultants of a Logical Resultants of a Logical ExpressionExpression

One unknown - A. Two combinations:

A can be either True or False

41

Page 42: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Two unknowns - A and B. Four combinations:

B can be either True or False for each value of A.

42

Developing a Table of All PossibleDeveloping a Table of All PossibleResultants of a Logical Expression (cont.)Resultants of a Logical Expression (cont.)

Page 43: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Three unknowns - A, B, and C. Eight combinations.

43

Developing a Table of All PossibleDeveloping a Table of All PossibleResultants of a Logical Expression (cont.)Resultants of a Logical Expression (cont.)

Page 44: Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer 1.

Developing a Table of All Developing a Table of All PossiblePossible

Resultants of a Logical Resultants of a Logical ExpressionExpression

44