View
18
Download
1
Category
Tags:
Preview:
DESCRIPTION
Citation preview
Beginning Problem-Solving Concepts for the Computer Page 1 of 17
Data Structures and Algorithms
Constants andVariables
Constants@ a value, that is, a specific alphabetical and/or
numeric value, that never changes during theprocessing of all the instructions in a solution
@ can be any type of data - numeric, alphabetical,or special symbols
Variable
@ may change during processing
@ the computer sets up a specific memory locationto hold the value of each variable name found in aprogram
@ can be any data type, just as constant
Beginning Problem-Solving Concepts for the Computer Page 2 of 17
Data Structures and Algorithms
Constants andVariables
Rules for Naming and Using Variables
@ Name a variable according to what it reperesents,that is, HOURS for hours worked, PAYRATE for rateof pay, and so on.
@ Do not use spaces in a variable name. If a space isneeded, use the underline character.
@ Do not use a dash (or any symbol that is used as amathematical operator) in a variable name. Thecomputer will recognize these symbols asmathematical operators, turn your variable intotwo or more variables, and treat your variable asa mathematical expression.
Beginning Problem-Solving Concepts for the Computer Page 3 of 17
Data Structures and Algorithms
@ After you have introduced a variable name thatrepresents a specific data item, this exact variablename must be used in all places where the dataitem is used.
@ Be consistent when using upper- and lowercasecharacters.
Rules for Naming and Using Variables
Constants andVariables
Beginning Problem-Solving Concepts for the Computer Page 4 of 17
Data Structures and Algorithms
Data Types
Data@ unorganized facts
Information
@ what is returned to the user as output, orprocessed data
DATAData
Processed intoInformation
REPORT
ChecksDepositsBk. Chgs.
Calaculatesthe balance Balance Sheet
Input Output
Beginning Problem-Solving Concepts for the Computer Page 5 of 17
Data Structures and Algorithms
Data Types
Data Type Data Set ExamplesNumeric: Integer All whole numbers 3570 , -54
Numeric: RealAll real numbers (whole +
decimal) 3792.91, 474526.23CHARACTER (surrounded by
quotation marks)All letters, numbers, and
special symbols"A","a", "M", "1",
"88","&", "#"
STRING (surrounded by quotation marks)
Combinations of more than one character "waahhh", "876860"
LOGICAL TRUE FALSE TRUE FALSE
Beginning Problem-Solving Concepts for the Computer Page 6 of 17
Data Structures and Algorithms
Data Types
Data Data Type
The price of an item: 12.50, 34.00 Numeric: real
An account number: "A8693" Character StringA quantity: 10,345 Numeric: integer
The name of a school: "STI" Character String
A credit check: TRUE, FALSE Logical
A date: 06/21/03 or "01/11/81 Date or Character String
Beginning Problem-Solving Concepts for the Computer Page 7 of 17
Data Structures and Algorithms
Data Types
Rules for Data Types
@ The data that define the value of a variable or aconstant will most commonly be one of three datatypes: numeric, character (including characterstring), or logical.
@ The programmer designates the data type duringthe programming process. The computer thenassociates the variable name with the designateddata type.
@ Data types cannot be mixed.
Beginning Problem-Solving Concepts for the Computer Page 8 of 17
Data Structures and Algorithms
@ Each of the data types uses what is called a dataset. The numeric data uses the set of all base 10numbers, the plus sign (+), and the negative sign(-); the character type uses the set of dataconsisting of the words TRUE and FALSE. The useof any data outside the data set results in an error.
@ Any numeric item that must be used in calculationsresulting in a numeric results must be designatedas numeric data type. All other numbers should bedesignated as character or character-string datatypes, even if data are all numbers.
Rules for Data Types
Data Types
Beginning Problem-Solving Concepts for the Computer Page 9 of 17
Data Structures and Algorithms
Functions
@ small sets of instructions that performspecific tasks and return values
@ usually built into a computer language orapplication
@ used as parts of istructions in a solution
Classes of Functions:
a Mathematicala Stringa Conversiona Statisticala Utility
Beginning Problem-Solving Concepts for the Computer Page 10 of 17
Data Structures and Algorithms
Functions
Mathematical
Example ResultSQRT(4) 2ABS (-3) 3
ROUND (5.678) 6INTEGER (5.789) 5
StringExample Result
MID(S, 3, 2) where S = "THOMAS" "OM"
LEFT (S,3) where S = "THOMAS" "THO"
RIGHT (S, 3) where S= "THOMAS" "MAS"
LENGTH(S) where S = "THOMAS" 6
Beginning Problem-Solving Concepts for the Computer Page 11 of 17
Data Structures and Algorithms
Functions
ConversionExample Result
VALUE ("57") +57STRING (+57) "57"
Statistical Example ResultAVERAGE (1,2,3,4) 2.5
MAX (1,2,3,4) 4MIN (1,2,3,4) 1SUM (1,2,3,4) 10
Utility Example ResultDATE 6/22/03TIME 2:55:03
Beginning Problem-Solving Concepts for the Computer Page 12 of 17
Data Structures and Algorithms
Operators
@ the data connectors within expressions andequations
@ tell the computer how to process the data
@ tell the computer what type of processing(mathematical,logical or whatever) needs to bedone
Types of Operator@ Mathematical - include addition, subtraction,
multiplication, division, integer division, modulodivision, powers, and functions
@ Relational - = , <, >, >=, <=, <>
@ Logical - AND, NOT, and OR
Beginning Problem-Solving Concepts for the Computer Page 13 of 17
Data Structures and Algorithms
Operators
Operation Resultant3.0 + 5.2 8.27.5 - 4.0 3.58.0 * 5.0 409.0/4.0 2
9\4 29 MOD 4 1
3 ^ 2 9
Mathematical
Relational
Operation Resultant3 = 2 FALSE5>1 TRUE
5 = 1 FALSE7 <= 8 TRUE9>=12 FALSE10 < 2 TRUE8<>8 FALSE
Beginning Problem-Solving Concepts for the Computer Page 14 of 17
Data Structures and Algorithms
Operators
A Not AT FF T
A B A AND BT T TT F FF T FF F F
A B A OR BT T TT F TF T TF F F
Logical
Beginning Problem-Solving Concepts for the Computer Page 15 of 17
Data Structures and Algorithms
Operators
Hierarchy of Operations( ) Reorders the hierarchy; all operations arecompleted within the parentheses using the samehierarchy.
1. Functions
Mathematical:2. Power3. \, MOD4. *, /5. +, -
Relational:6. =, <, >, <=, >=, <>
Logical:7. NOT8. AND9. OR
Beginning Problem-Solving Concepts for the Computer Page 16 of 17
Data Structures and Algorithms
Expressions andEquations
Expression@ processes data, the operands, through the use of
operators
@ example: LENGTH * WIDTH
Equation@ stores the resultant of an expression in a memory
location in the computer through the equal (=) sign
@ example: AREA = LENGTH * WIDTH
@ often called assignment statements because thevariable on the left-hand side of the equal sign isassigned the value of the expression on the right-hand side
@ the equal sign does not mean equals; instead itmeans replaced by or is assigned the value of
Beg
inni
ng P
robl
em-S
olvi
ng C
once
pts
for
the
Com
pute
r
P
age
17 o
f 17
Dat
a S
tru
ctu
res
and
Alg
ori
thm
s
Ex
pre
ss
ion
s a
nd
Eq
ua
tio
ns
Recommended