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
Dec 22, 2015
Chapter 2Beginning Problem-Solving Concepts for the Computer
CMPF144 Introduction to Problem Solving and Basic CMPF144 Introduction to Problem Solving and Basic ComputerComputer
1
2
OverviewOverview
Constants and Variables Data Types Functions Operators Expressions and Equations
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
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
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
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
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
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
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
Numeric Data Types and Their Numeric Data Types and Their Data SetsData Sets
10
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
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
Character / String Data Types and Character / String Data Types and Their Data SetsTheir Data Sets
13
LogicalLogical Only consists of two value – True or False Used to make yes-or-no decisions Example:
result = True
badInput = “yes”
CreditOK = 1
14
Logical Data Types and Logical Data Types and Their Data SetsTheir Data Sets
15
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
Examples of Data TypesExamples of Data Types
17
Examples of Data Types Examples of Data Types (cont.)(cont.)
18
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
Examples of FunctionsExamples of Functions
20
Examples of Functions Examples of Functions (cont.)(cont.)
21
22
Examples of Functions (cont.)Examples of Functions (cont.)
23
Examples of Functions (cont.)Examples of Functions (cont.)
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
Mathematical Operators Mathematical Operators and Their Computer and Their Computer
SymbolsSymbols
25
Relational Operators and Relational Operators and Their Computer SymbolsTheir Computer Symbols
26
Logical Operators and Their Logical Operators and Their Computer SymbolsComputer Symbols
27
NOT Logical OperatorsNOT Logical Operators
28
AND Logical OperatorsAND Logical Operators
29
OR Logical OperatorsOR Logical Operators
30
Hierarchy of OperationsHierarchy of Operations
31
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
Expressions and Equations Expressions and Equations (cont.)(cont.)
33
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
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
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
Evaluating a Mathematical Evaluating a Mathematical ExpressionExpression
37
Evaluating a Relational Evaluating a Relational ExpressionExpression
38
Evaluating a Logical Evaluating a Logical ExpressionExpression
39
Evaluating Equation That Uses Evaluating Equation That Uses Both Relational and Logical Both Relational and Logical
OperatorsOperators
40
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
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.)
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.)
Developing a Table of All Developing a Table of All PossiblePossible
Resultants of a Logical Resultants of a Logical ExpressionExpression
44