500 likes | 716 Views
Beginning Problem-Solving Concepts for the Computer. Lesson 2. 3 Types of Problems. Computational problems involving some kind of mathematical processing Logical Problems involving relational or logical processing Repetitive
E N D
Beginning Problem-Solving Concepts for the Computer Lesson 2 COP1000
3 Types of Problems • Computational • problems involving some kind of mathematical processing • Logical • Problems involving relational or logical processing • Repetitive • Problems involving repeating a set of mathematical and/or logical instructions. COP1000
Fundamental Concepts • The building blocks of equations and expressions • Constants • Variables • Operators • Functions (predefined—more about user-defined functions later) COP1000
Constants • A value • a specific alphabetical and/or numeric value • Does not change during the processing of all the instructions in a solution • Can be of any data type • Numeric, alphabetical or special symbols • Examples • 3, 5, 10, “Mulder”, “Scully”, “+”, “-”, “/” • Note: “” indicates datum rather than variable name COP1000
Constants • In some programming languages, constants can be named • Provides protection to the constant value and other areas of the program. • Cannot be changed once given initial value • Note: no spaces allowed within names • Examples • SALES_TAX_RATE = 6 • COMMISSION_RATE = 6 Constant name are usually in ALL CAPS COP1000
Variables • The name references the memory storage location where the value is stored • May change during processing • May be of any data type • A name is assigned to each variable used in a solution. • Examples • Age, LastName, Address, PayRate COP1000
Rules for Naming Variables • Use mnemonic terms--the name is meaningful • Use PayRate not Z • Do not use spaces • Do not use special symbols, except underscore • Examples • PayRate, Pay_Rate, PAYRATE, or PAY_RATE • Use appropriate naming convention • Be consistent! • Some languages are case sensitive • Either combine terms (PayRate) or include an underscore to separate (Pay_Rate) COP1000
Data & Information • Data • Raw, organized facts • Information • Meaningful output COP1000
Data Types • Most common types • Numeric • Character • Logical • Most languages include other data types • Date • Currency • User-defined • Strings COP1000
Rules for Data Types • Programmer designates the data type • Data types cannot be mixed • Called data typing • All data to be used in calculations must be declared as a numeric data type • Each data type uses a data set or domain • Integers - -32768 -> 0 -> 32767 but is programming language dependent • Characters – all alphanumeric characters included in the computer’s coding scheme • Logical – the words “true” and “false” COP1000
Data Types & Data Sets COP1000
Numeric Types • Includes all types of numbers • Integers • 3, 5, -5, 0, -1 and 32,767 • Real numbers (floating point numbers) • 3.1459, -5745 • Scientific Notation • 2.3E5 or 5.4E-3, where E stands for the Power of 10 • Can be Positive or Negative COP1000
Character Types • Alphanumeric data set • Consists of • all single-digit numbers, • letters, and • special characters available to the computer • contained within quotation marks COP1000
Coding Systems • EBCDIC • Extended Binary Coded Decimal Information Code • Used on IBM mainframes • ASCII • American Standard Code for Information Interchange • 256 characters (1 byte) • First 128 – standard • Second 128 – specific to computer • Unicode (2 bytes) • Replacing ASCII as standard on PC’s See Appendix B or online COP1000
String Data Type • Made up of one or more characters. • Contained within quotation marks • Cannot be used within calculations • There is some debate about using strings… • The new consensus is that if the value will be used in a mathematical calculation, make it a number, otherwise make it a string. • The alternative view (older view) is to look at the amount of storage that is needed for the value. A number generally takes up less memory space than a string. COP1000
Strings - Caution • “123” is not the same thing as 123 • The first one is a string • The second one is a number • Becomes important when deciding when a piece of data is a string or a number • Ex. Social Security Numbers • The first one contains 11 characters and is left aligned whereas the second contains 9 numbers and is right aligned COP1000
Comparing Characters & Strings • The computer’s particular coding scheme (ASCII, Unicode, or EBCDIC) converts the character to its associated ordinal number • Note: Uppercase letters have a smaller ordinal values than lowercase letters • “A” = 193 whereas “a” = 129 (Unicode 16-bit) • “A” = 65 whereas “a” = 97 (ASCII 8-bit) • Once converted, each character, one at a time, can be compared COP1000
Concatenation • Joins character or string data together • Operators: + and & • Dependent on programming language • & can mix data types • + cannot mix data types (also known as Join in databbases) • Examples • FName & “ ” & MI & “ ” & LName • “James” + “ ” + “T.” + “ ” + “Kirk” = “James T. Kirk” • “James” & “ ” & “T.” & “ ” & “Kirk” = “James T. Kirk” • Note the space at the end of the T. COP1000
Logical (Boolean) Data Type • Consists just two pieces of data • True and False • Different programming languages also allow • Yes and No • 1 (True) and 0 (False) • On and Off COP1000
Other Pre-defined Data Types • Dependent on programming language, numerous other pre-defined data types are available. • Examples • Dates 01/01/1900 January 1, 1900 • Times 03:35:05 PM 15:35:05 • Currency 3000.00 • Note: This is how the data will be stored, not how it is formatted. COP1000
User-Defined Data Types • User-defined Data types • Defined by the user as a new data type • Example • Record Employee_Record FnameLname SSN BirthDateHireDate TerminationDatePayRate End COP1000
Functions • Small sets of instructions that perform specific tasks and return values. • Two types: • Pre-defined • Built into a computer language or application • User-defined • More about these later COP1000
Functions • Benefits • Reduces the amount of code that needs to be written, thus reducing errors of repetition • Shortens the development time • Improves readability COP1000
Function Parameters • Data usually placed within parentheses that the function uses without altering the data • In Sqrt(n), the n represents the parameter, in this case a number • In Value(s), the s represents a string COP1000
Function Types • Mathematical • String • Conversion • Statistical • Utility • Specific to each programming language…a partial list follows COP1000
Mathematical Functions Random is a random number selected between 0 and 1 COP1000
String Functions COP1000
Conversion Functions COP1000
Statistical Functions COP1000
Utility Functions Error is a special function that appears in most languages and upon execution return control to the program when (not if) system errors occur. COP1000
Operands, Resultants & Operators • Operands • The data that the operator connects and processes • Resultant • The answer that results when the operation is executed • Operators • The data connectors within expressions and equations. • Two types of operations • Unary • Negation • Binary • Addition, Subtraction, Multiplication, Floating Point Division and Integer Division COP1000
Operator Types • Mathematical • +, -, *, / • \, Mod {Integer Division & Modulo} • ^, {Exponentiation} and • functions • Relational • <, >, <=, >=, =, <> • Logical • And, Or, Not, XOR, EQV COP1000
Two Special Math Operators • Integer Division (\) • Regular Floating Point Division, except only the whole number part of the answer is returned. • If given a Floating Point number, the number is rounded—first—before division occurs. • Modulo Division (Mod) or Modulus • The remainder part of the answer is returned. COP1000
The Boolean Data Type • Returns either the keywords True or False or • Returns a non-zero or zero value. • -9, -1, 1, 2 are equivalent to True • 0 is equivalent to False • Is stored in two bytes COP1000
Relational Operators • Perform comparisons variable - relational operator - variable • Ex. If Y > Z then ... = equal <> not equal < less than > greater than <= less than or equal to >= greater than or equal to COP1000
Logical (Boolean) Operators • And, Or, Not, XOR, EQV • Boolean Expressions are used • To create more complicated Boolean Expressions. • If a = b AND c < d Then … • They are defined by using Truth Tables… • Know the significance of each! Hint!These are on the Test! COP1000
The AND Operator • Significance: The only time the result is True is if both A and B are True. COP1000
The OR Operator • Significance: The only time the result is False is if both A and B are False. COP1000
The NOT Operator • Also called the “logical complement” or “logical negation” • Significance: Whatever the value of A is, NOT A will be the opposite. COP1000
The XOR (Exclusive Or) Operator • Significance: The only time the result is True is if A and B are different and False if both A and B are the same. COP1000
The EQV (Equivalent) Operator • Significance: The only time the result is True is if A and B are the same and False if both A and B are the different. COP1000
Hierarchy of Operations • Data and Operators are combined to form expressions and equations • To evaluate these in the proper order, a hierarchy of operations, or Order of Precedence, is used. • Note: Different programming languages use different Order of Precedence… • Note: Whenever you want to clarify an equation, use the parentheses! COP1000
The Order of Precedence COP1000
Example • Evaluate the following: • A = True, B = False, C = True, D = False • To do this, we create what’s called an Evaluation Tree… A or B or C and D and A and B and (not C) or (not D) COP1000
The Evaluation Tree A or B or C and D and A and B and (not C) or (not D) False True False False False False True True True 1 2 3 4 5 6 7 8 COP1000
Expressions & Equations • Expressions • Process the data and the operands, through the use of the operators • Does not store the resultant (answer) • Examples • Price * SalesTaxRate • (Hours – 40) * Wage * 1.5 • Length * Width COP1000
Equations • Same as an expression, except the equation stores the resultant (answer) in a memory location (must be on the left side of Assignment Operator) • “takes on the value of” not “equals” • Examples • SalesTaxis the memory location where the answer will be stored, the resultant • Often called Assignment Statements SalesTax = Price * SalesTaxRate OvertimePay = (Hours – 40) * Wage * 1.5 Area = Length * Width COP1000
More about Equations • Destructive Read-In • The value stored in the variable on the left-hand side of the equals sign is replaced by the result of the expression on the right-hand side. • Non-Destructive Read-In • Values used on the right-hand side are protected from being changed. SalesTax = =Price ** SalesTaxRate D R-I ND R-I ND R-I Ahh..but what about this one? Count = Count + 1 COP1000
Straight-Line Form • Algebraic Expressions cannot be represented in the computer. • They must be converted to a form the computer will recognize. • Thus, Straight Line Form = ( 2 *(X – Y ) )/ ( ( ( 2 * X + Y )^2 )/ (( X – Y )^2 )) straightline form COP1000
Next? Programming Concepts... COP1000