Você está na página 1de 10

CSC1101 PLF Problem Solving Concepts for the Computer

Lesson Two: Problem Solving Concepts for the Computer

Objectives

1. Differentiate between the basic data types of variables and


constants, which include character, numeric and logical data types.
2. Set up and evaluate expressions and equations using variables,
constants, operators and the hierarchy of operations.

Introduction

 Problems that can be solved on computers generally consist of only three:


1. Computational, problems involving some kind of mathematical
processing.
2. Logical, problems involving relational or logical processing, the
kinds of processing used in decision making on the computer; and
3. Repetitive, problems involving repeating a set of mathematical
and/or logical instructions.

 The computer uses constants and variables to solve problems. They are
data used in processing.

Variables

♦ A named memory location in the computer that given a value that can be
changed any time during the processing of the program.
♦ Computer sets up a specific memory location to hold the value of each variable
name that found in a program.
♦ Variables can be any data type (few types of data)
♦ For any variables use in a program, should assign a name (called variable name

Prepared By Chong Page 1 of 10


CSC1101 PLF Problem Solving Concepts for the Computer

– user defined) and MUST define a data type (the type of data this variable can
hold).

♦ For example,
• value of age (variable name: age, data type – integer)
• value of cash price (variable name: price, data type – real)
• value of city code (variable name: city_code, data type – string).

Constants

♦ It is a value that is a specific alphabetical and/or numeric value that are never
changes during the processing of all the instructions in a solution.
♦ Constant can be any type of data – numeric, alphabetical or special symbols.
♦ The constant is a given location in memory and a name.
♦ For any constants use in a program, should assign a name (called constant name
– user defined) and MUST define a data type (the type of data this variable can
hold).
♦ During the execution of the program, this constant is given a value and then is
referred by its name and the value is never changed during the execution of the
program.
♦ For example, to calculate payroll for a company, constant can be the name of the
company.

Rules for Naming and Using Variable/Constant name

• Do not use reserved word/keyword as variable/constant name.


• Do not use spaces in a variable/constant name, i.e.HOURS WORKED. (if a
space is needed, use the underscore, i.e. HOURS_WORKED)
• Do not use a dash or any other symbols in a variable/constant name, except
underscore is allowed in a variable/constant name.
• A variable/constant name can come from a combination of letters and numbers,

Prepared By Chong Page 2 of 10


CSC1101 PLF Problem Solving Concepts for the Computer

but letter must come first in the name.

Extra Rules:
• Variable/constant must be named according to what they represent (a meaningful
variable/constant), i.e. HOURS for hours worked.
• Be consistent when using upper- and lowercase characters. In some languages
HOURS is a different variable name than Hours.
• Variable name that used to represent a data item should be used consistently
throughout the program where the data item is used. For example, you may not
use HRS or HOURSWORKED to represent the same data item.

Example: Input an integer number from the keyboard

Good variable name : Number


Poor variable name : A, N,

Data Type Groups

♦ The kind of data of a variable or a constant. The three basic data types are
numerical, character and logical.

Numerical data type – Integer and Real

♦ Includes all types of numbers and only can be used in calculations.


♦ It can be integers or real number.
♦ Integers are whole numbers and they can be represented positive or negative
value. I.e., -5, 89.
♦ Real numbers are floating numbers and can be expressed in scientific notation.
2.3E5 is the same number as 2.3 X 105 = 2300000. E stands for time 10 to the
power of.

Prepared By Chong Page 3 of 10


CSC1101 PLF Problem Solving Concepts for the Computer

Examples:

Variable Name Data Data type


Age 16 Integer
Salary 100.00 Real
Time 12.10 Real

Character data type – Character & String

♦ Sometimes known as alphanumeric data.


♦ Consists of all digit numbers, letter and special characters available to the
computer – A, a, z, 4, %, and so forth – placed within quotation marks.
♦ Upper case letters are different from lower case letter.
♦ Characters cannot be used for calculations even if they consist of only numbers.
♦ When more than one character is put together is known as String, a collections
of characters.
♦ Character and string can be compared and arranged in alphabetical order.

For example,

‘B’ is larger than ‘A’, ‘B’ is placed after ‘A’.


‘C’ is larger than ‘B’, ‘C’ is placed after ‘B’.
‘Banana’ is larger than ‘Apple’, therefore ‘Banana’ is arranged after the
‘Apple’.

Examples:

Variable Name Data Data type


Name Mel String
Gibson
Gender F Character
Account A2563222 String
Number
Password Mom*/9 String

Prepared By Chong Page 4 of 10


CSC1101 PLF Problem Solving Concepts for the Computer

Post code 70300 String

Logical data type

♦ Consists of two pieces of data in the data set; TRUE/FALSE .


♦ Some languages accept yes, T and Y for TRUE, and no, F and N for FALSE.
♦ They are used in making yes-or-no decisions.

Examples:

Variable Name Data Data type


Marital Status Y/N Logical
Credit check Y/N Logical
ok

Rules for data type groups

♦ The data that define the value of a variable or constant will most commonly be
one of three data types.

♦ The programmer designated the data type during the programming process. The
computer then associates the variable name with the designated data type.

♦ Data type cannot be mixed. For an example string data cannot be placed in a
variable memory location that has been designated as numerical or vice versa.

♦ Each of the data type uses what is called data set.

o The numerical data uses the set of all base 10 numbers, the plus (+) sign, and
the negative sign (-);
o The character data type uses the set of all characters and;
o The logical data type uses the set of data consisting of the words TRUE and
FALSE. The use of any data outside the data set results in an error.

Prepared By Chong Page 5 of 10


CSC1101 PLF Problem Solving Concepts for the Computer

♦ Any numeric item that must be used in calculations resulting in a numeric result
must be designated as numerical data type. All other numbers should be
designated as character or character-string data type.

Operators

• Operators are used to process data and to tell the computer how to process data.
• Operators are the data connectors within expression and equations.
• Three types of operators are used in calculation and problem solving include:

1. Mathematical operators, (Refer to text book, page no.22, Table 2.5)

+ Addition MOD Modulus division


- Subtraction \ Integer division
* Multiplication ^ Power
/ Division

• Use to form an arithmetic expression, e.g. 4+9

2. Relational, (Refer to text book, page no.22, Table 2.5).


=, >, <, >=, <=, <>
• Use to form a condition, e.g. (8 <= 10)

3. Logical, (Refer to text book, page no.22 & 23, Table 2.5 & 2.6)
AND, OR, and NOT.
• Use to join more than TWO conditions together, e.g. (8<=10) AND
(4 >10)

• The operand and resultant are two concepts related to the operators.
• Operand is the data that the operator connects and process.
• Resultant is the answer that results after the operation is completed.

For example, 5 + 7 = 12

5 and 7 are the operand, + is the operator, 12 is the resultant.

Prepared By Chong Page 6 of 10


CSC1101 PLF Problem Solving Concepts for the Computer

• These mathematical, relational and logical operators have a hierarchy or


precedence, an order in which their operations take place.

(Refer to text book, page no.24, Table 2.7 Hierarchy of Operations)

• To reorder the normal processing sequence, the programmer uses parenthesis.

Expression and Equations

• An expression process data, the operand, through the use of operators.


• Example : to find the production of two numbers
A*B
• An equation stores the resultant of an expression in a memory location in the
computer through the equal(=) sign.
• Example : The resultant of the expression A * B would be stored in a memory
location called C
C = A * B { Known as assignment statement}

Setting up a mathematical equation

• Mathematical equation has to be the form of an assignment statement.

Examples:

Mathematical equation Assignment statement


A+B=C C=A+ B
Y + 3 = X(Z+5) Y = X * (Z + 5) - 3

Evaluating mathematical expression

A = 10 B =1
Operation Resultant
A+B 11

Prepared By Chong Page 7 of 10


CSC1101 PLF Problem Solving Concepts for the Computer

Setting up a relational expression

• A relational expression is used to make decisions.

In computer form
X is less than Y + 5 X<Y+5

Evaluating relational expression

X=5 Y=4

Operation Resultant
Y + 5  (4+5) 9
X < Y  (5<9) TRUE

Setting up a logical expression


• Based on more than one TRUE or FALSE

Example 1: Student must have student card(SC)or Identification card (IC) in order
to sit for and exam
SC OR IC

Example 2: Student must have both student card(SC)and Identification card (IC) in
order to sit for and exam
SC AND IC

Evaluating a logical expression

Values : A = TRUE B = FALSE C = FALSE

Given : A OR B AND C OR B

Prepared By Chong Page 8 of 10


CSC1101 PLF Problem Solving Concepts for the Computer

TABLE OF LOGICAL OPERATORS RESULTANT – TRUTH TABLE

AND OR NOT
C1 C2 R C1 C2 R C R
T T T T T T T F
T F F T F T F T
F T F F T T
F F F F F F
Operation Resultant

1. B AND C (TRUE AND FALSE) FALSE


2. A OR resultant 1 (TRUE OR FALSE) TRUE
3. Resultant 2 OR B (TRUE OR FALSE) TRUE

Prepared By Chong Page 9 of 10


CSC1101 PLF Problem Solving Concepts for the Computer

Questions: Discussion in the Class.

Q1. Fill in the following table with the variable name and data type.

Data item Variable Name Data Type


a. Name of vendor company
b. Inventory item name
c. Inventory number
d. Quantity
e. Price
f. Address of company
g. Date last ordered
h. Reorder quantity
i. Obsolete item (yes/no)

Q2. Name the data type for each of the following constant.
1. 5.38
2. “87645”
3. TRUE
4. “A”
5. “New York”
6. 2.45E6
7. 458963.62

Q3. Evaluate the following equations, given the values A = 12, B = 3, C = 6, D = 2.

1. F = A + B/C – D ^ 2
2. F = (A + B)/C – D ^ 2
3. F = A + B/(C – D ^ 2)
4. F = (A + B) MOD C
5. F = (A + B)\D ^ 2

Q4.
Set up a logical expression for the following conditions. A company gives a bonus
at the end of each fiscal year. For an employee to get a bonus, the following must be
true.
a. The employee has been working at the company for more than six
months with no negative reports.

b. The employee has earned more than $5,000 during the fiscal year.

Prepared By Chong Page 10 of 10

Você também pode gostar