Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer
Overview Constants and Variables Data Types Functions Operators Expressions and Equations
Objectives Differentiate between variables and constants. Differentiate between character, numeric, and logical data types. Identify operators, operands and resultants. Identify and use functions Identify and use operators according to placement in hierarchy chart. Set up & evaluate expressions and equations using variables, constants, operators, and the hierarchy of operations.
Constant A value (content in memory cell, can be alphabetic and/or numeric) that will never 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
Variable A value (can be any data type)that might 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
Rules for Naming and Using Variables Name a variable according to what it represents. Do not use spaces. Start a variable name with a letter. Do not use a dash or any other symbol that is used as a mathematical operator. Consistent usage of variable name. Consistent use of upper, lowercase characters in variable names Use naming convention specified by your company
Incorrect Variable Names 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 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 Hours_worked pp 51, WHAT’s WRONG WITH THIS? Q1
Data 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
Numeric Include all types of numbers Can be used for numeric calculations 2 + 1 = ?? Subtypes include: Integer Whole number Can be positive of negative Example: 5297, -376 Real number Also known as floating point numbers Can be represented in scientific notation Example: 0.0054, 2.3E5 (2.3 x 105)
Numeric Data Types and Their Data Sets
Character / 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
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” = ??
Character / String Data Types and Their Data Sets
Logical Only consists of two value – True or False Used to make yes-or-no decisions Example: result = True badInput = “yes” CreditOK = 1
Logical Data Types and Their Data Sets
Date Is a numeric data type as mathematical operations can be performed onto the value Allows user to subtract one date from another date
Examples of Data Types
Examples of Data Types (cont.) pp. 47, QUESTIONS, Q5
Functions 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
Examples of Functions
Examples of Functions (cont.)
Examples of Functions (cont.)
Examples of Functions (cont.)
Operators 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)
Mathematical Operators and Their Computer Symbols
Relational Operators and Their Computer Symbols
Logical Operators and Their Computer Symbols
NOT Logical Operators
AND Logical Operators
OR Logical Operators pp. 48, QUESTIONS, Q9
Hierarchy of Operations pp. 48, QUESTIONS, Q10
Expressions and Equations Used to process data (operands) through operators Example: width * 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) area = width * height
Expressions and Equations (cont.)
Setting Up a Numeric Expression Mathematical expression: X (3Y + 4) - Appropriate computer representation: 4Y X + 6 X * ( 3 * Y + 4) – 4 * Y / ( X + 6) pp. 49, PROBLEMS, Q1
Setting Up a Mathematical Equation 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 pp. 49, PROBLEMS, Q2
Setting Up a Relational Expression Given an expression : Appropriate computer representation: Computer can’t understand “is lesser than” X is lesser than Y + 5 X < Y + 5
Evaluating a Mathematical Expression pp. 49, PROBLEMS, Q5
Evaluating a Relational Expression
Evaluating a Logical Expression
Evaluating Equation That Uses Both Relational and Logical Operators pp. 51, PROBLEMS, Q14
Developing a Table of All Possible Resultants of a Logical Expression One unknown - A. Two combinations: A can be either True or False
Developing a Table of All Possible Resultants of a Logical Expression (cont.) Two unknowns - A and B. Four combinations: B can be either True or False for each value of A.
Developing a Table of All Possible Resultants of a Logical Expression (cont.) Three unknowns - A, B, and C. Eight combinations.
Developing a Table of All Possible Resultants of a Logical Expression pp. 50, PROBLEMS, Q13