Beginning Problem-Solving Concepts for the Computer

Slides:



Advertisements
Similar presentations
Data types and variables
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Chapter 2 Beginning Problem-Solving Concepts for the Computer
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Objectives You should be able to describe: Data Types
Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Lesson 2 McManus COP  Computational ◦ problems involving some kind of mathematical processing  Logical ◦ Problems involving relational or logical.
Beginning Problem-Solving Concepts for the Computer Lesson 2 McManusCOP10001.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
2440: 211 Interactive Web Programming Expressions & Operators.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Computer Programming 12 Lesson 3 – Computer Programming Concepts By Dan Lunney.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
1 CSC103: Introduction to Computer and Programming Lecture No 6.
Input, Output, and Processing
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Chapter 2 Variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
A variable is a name for a value stored in memory.
Chapter 2 Variables.
Chapter 2 Basic Computation
BASIC ELEMENTS OF A COMPUTER PROGRAM
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
IDENTIFIERS CSC 111.
Data Types, Identifiers, and Expressions
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2 Variables.
Chapter 2: Introduction to C++.
Data Types and Expressions
The Data Element.
Primitive Types and Expressions
The Data Element.
Chapter 2 Variables.
Programming Fundamental-1
Presentation transcript:

Beginning Problem-Solving Concepts for the Computer COP1006 4/24/2017 Beginning Problem-Solving Concepts for the Computer Lesson 2 McManus COP1006 COP1006_02

Fundamental Concepts The building blocks of equations and expressions COP1006 4/24/2017 Fundamental Concepts The building blocks of equations and expressions Constants & Variables Data Types Functions Operators Expressions & Equations McManus COP1006 COP1006_02

Constants A value Can be of any data type Examples COP1006 4/24/2017 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 McManus COP1006 COP1006_02

Constants In some programming languages, constants can be named COP1006 4/24/2017 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 Example SalesTaxRate = 6 Commission_Rate = 6 Note: no spaces within variable names McManus COP1006 COP1006_02

Variables May change during processing COP1006 4/24/2017 Variables May change during processing A name is assigned to each variable used in a solution. The name references the memory area where the value is stored. May be of any data type Examples Age, LastName, Address, PayRate McManus COP1006 COP1006_02

Rules for Naming Variables COP1006 4/24/2017 Rules for Naming Variables Use mnemonic terms, that is, the name means something Use PayRate not Z Must begin with a Letter Do not use special symbols Do not use spaces Either combine terms (PayRate) or include an underscore to separate (Pay_Rate) Be consistent! Some languages are case sensitive PayRate, Pay_Rate, PAYRATE, or PAY_RATE Use appropriate naming convention McManus COP1006 COP1006_02

Data & Information Data Information Raw, organized facts COP1006 4/24/2017 Data & Information Data Raw, organized facts Information Meaningful output McManus COP1006 COP1006_02

Data Types Most common types Most languages include other data types COP1006 4/24/2017 Data Types Most common types Numeric Character Logical Most languages include other data types Date Currency User-defined McManus COP1006 COP1006_02

Data Types & Data Sets Data Type Data Set Examples Integer COP1006 4/24/2017 Data Types & Data Sets Data Type Data Set Examples Integer All whole numbers 3456 -43 Real All real numbers (whole + decimal) 3456.78 0.000123 Character (uses quotation marks) All letters, numbers, and special symbols “A”, “a”, “1”, “5”, “+”, “%” String (uses quotation marks) Combinations of more than one character “X Files” “Torchwood” “123-45-6789” Logical True or False True False McManus COP1006 COP1006_02

Numeric Types Include all types of numbers Integers COP1006 4/24/2017 Numeric Types Include 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 McManus COP1006 COP1006_02

Character Types Alphanumeric data set Consists of COP1006 4/24/2017 Character Types Alphanumeric data set Consists of all single-digit numbers, letters, and special characters available to the computer contained within quotation marks McManus COP1006 COP1006_02

Coding Systems EBCDIC – Extended Binary Coded Decimal Information Code COP1006 4/24/2017 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 C McManus COP1006 COP1006_02

String Data Type Made up of one or more characters COP1006 4/24/2017 String Data Type Made up of one or more characters Contained within quotation marks Cannot be used within calculations McManus COP1006 COP1006_02

Strings - Caution “123” is not the same thing as 123 COP1006 4/24/2017 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 McManus COP1006 COP1006_02

Comparing Characters & Strings COP1006 4/24/2017 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 McManus COP1006 COP1006_02

Concatenation Joins character data or string data together COP1006 4/24/2017 Concatenation Joins character data or string data together Operators: + and & Dependent on programming language & can mix data types + cannot mix data types (also known as Join) 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. McManus COP1006 COP1006_02

Logical (Boolean) Data Type COP1006 4/24/2017 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 McManus COP1006 COP1006_02

Other Pre-defined Data Types COP1006 4/24/2017 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. McManus COP1006 COP1006_02

User-Defined Data Types COP1006 4/24/2017 User-Defined Data Types User-defined Data types Defined by the user as a new data type Examples Records Employee_Record Fname Lname SSN End McManus COP1006 COP1006_02

Rules for Data Types Programmer designates the data type COP1006 4/24/2017 Rules for Data Types Programmer designates the data type Data types cannot be mixed Called data typing Not allowed: X = 3.1459 * 5 (real * integer) All data to be used in calculations must be declared as a numeric data type Zip should be a string because won’t be used in calculation Each data type uses a data set or domain Integers – typically -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” McManus COP1006 COP1006_02

COP1006 4/24/2017 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 McManus COP1006 COP1006_02

COP1006 4/24/2017 Functions Benefits Reduces the amount of code that needs to be written, thus reducing errors of repetition Shortens the development time Improves readability McManus COP1006 COP1006_02

COP1006 4/24/2017 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 McManus COP1006 COP1006_02

Function Types Mathematical String Conversion Statistical Utility COP1006 4/24/2017 Function Types Mathematical String Conversion Statistical Utility Specific to each programming language…a partial list follows McManus COP1006 COP1006_02

Mathematical Functions COP1006 4/24/2017 Mathematical Functions McManus COP1006 COP1006_02

COP1006 4/24/2017 String Functions McManus COP1006 COP1006_02

COP1006 4/24/2017 Conversion Functions McManus COP1006 COP1006_02

Statistical Functions COP1006 4/24/2017 Statistical Functions McManus COP1006 COP1006_02

COP1006 4/24/2017 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. McManus COP1006 COP1006_02

Operators The data connectors within expressions and equations. COP1006 4/24/2017 Operators The data connectors within expressions and equations. Operands & Resultants Operand are the data Resultant is the answer that results when the operation is completed McManus COP1006 COP1006_02

Two Types of Operations COP1006 4/24/2017 Two Types of Operations Unary Negation Binary Addition, Subtraction, Multiplication, Floating Point Division and Integer Division McManus COP1006 COP1006_02

Operator Types Mathematical Relational Logical +, -, *, / \, Mod COP1006 4/24/2017 Operator Types Mathematical +, -, *, / \, Mod ^, and functions Relational <, >, <=, >=, =, <> Logical And, Or, Not, XOR, EQV McManus COP1006 COP1006_02

Two Special Math Operators COP1006 4/24/2017 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. 2  10 10 McManus COP1006 COP1006_02

Two Special Math Operators COP1006 4/24/2017 Two Special Math Operators Modulo Division (Mod) or Modulus The remainder part of the answer is returned. 3  19 15 4 McManus COP1006 COP1006_02

The Boolean Data Type Returns either the keywords True or False or COP1006 4/24/2017 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 McManus COP1006 COP1006_02

Relational Operators Perform comparisons COP1006 4/24/2017 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 McManus COP1006 COP1006_02

Logical (Boolean) Operators COP1006 4/24/2017 Logical (Boolean) Operators And, Or, XOR, Not, 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! McManus COP1006 COP1006_02

COP1006 4/24/2017 The NOT Operator Also called the “logical complement” or “logical negation” Significance: Whatever the value of A is, NOT A will be the opposite. McManus COP1006 COP1006_02

COP1006 4/24/2017 The AND Operator Significance: The only time the result is True is if both A and B are True. McManus COP1006 COP1006_02

COP1006 4/24/2017 The OR Operator Significance: The only time the result is False is if both A and B are False. McManus COP1006 COP1006_02

The XOR (Exclusive Or) Operator COP1006 4/24/2017 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. McManus COP1006 COP1006_02

The EQV (Equivalent) Operator COP1006 4/24/2017 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. McManus COP1006 COP1006_02

Hierarchy of Operations COP1006 4/24/2017 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. McManus COP1006 COP1006_02

Hierarchy of Operations COP1006 4/24/2017 Hierarchy of Operations Note 1: Whenever you want to clarify an equation, use the parentheses! Note 2: Use the one on the next slide for your homework, but be aware that each language will have its own hierarchy, as well as different languages have different Hierarchies McManus COP1006 COP1006_02

The Order of Precedence COP1006 4/24/2017 The Order of Precedence *this differs from the one in the textbook—use this one… McManus COP1006 COP1006_02

Example Evaluate the following: COP1006 4/24/2017 Example Evaluate the following: A = True, B = False, C = True, D = False A or B or C and D and A and B and (not C) or (not D) To do this, we create what’s called an Evaluation Tree… McManus COP1006 COP1006_02

COP1006 4/24/2017 The Evaluation Tree A or B or C and D and A and B and (not C) or (not D) False True McManus COP1006 COP1006_02

Expressions & Equations COP1006 4/24/2017 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 McManus COP1006 COP1006_02

COP1006 4/24/2017 Equations Same as an expression, except the equation stores the resultant (answer) in a memory location “takes on the value of” not equals Examples SalesTax = Price * SalesTaxRate OvertimePay = (Hours – 40) * Wage * 1.5 Area = Length * Width SalesTax is the memory location where the answer will be stored Often called Assignment Statements McManus COP1006 COP1006_02

More about Equations Destructive Read-In Non-Destructive Read-In COP1006 4/24/2017 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 McManus COP1006 COP1006_02

COP1006 4/24/2017 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 = (X – Y) / ( (X + Y)^2 / (X – Y )^2 ) straight line form McManus COP1006 COP1006_02

3 Types of Problems Computational Logical Repetitive COP1006 4/24/2017 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. McManus COP1006 COP1006_02

Planning Your Solution... COP1006 4/24/2017 Next? Planning Your Solution... McManus COP1006 COP1006_02