BEGINNING PROBLEM-SOLVING CONCEPTS FOR THE COMPUTER. Chapter 2

Similar documents
Problem Solving. Problem Solving Concept for Computer Science

Outline. Data and Operations. Data Types. Integral Types

CHAPTER 3: CORE PROGRAMMING ELEMENTS

BASIC ELEMENTS OF A COMPUTER PROGRAM

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

C++ Programming: From Problem Analysis to Program Design, Third Edition

COMP Primitive and Class Types. Yi Hong May 14, 2015

StudyHub+ 1. StudyHub: AP Java. Semester One Final Review

Chapter 2 Working with Data Types and Operators

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Full file at

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Midterms Save the Dates!

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Full file at

Chapter 2.4: Common facilities of procedural languages

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Lesson 02 Working with Data Types MIT 31043, Visual Programming By: S. Sabraz Nawaz

3. Types of Algorithmic and Program Instructions

CMPT 125: Lecture 3 Data and Expressions

Chapter Two PROGRAMMING WITH NUMBERS AND STRINGS

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Expressions and Variables

Visual C# Instructor s Manual Table of Contents

Lesson 02 Working with Data Types. MIT 31043: VISUAL PROGRAMMING By: S. Sabraz Nawaz Senior Lecturer in MIT

BITG 1233: Introduction to C++

Fundamentals of Programming

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

Chapter 17. Fundamental Concepts Expressed in JavaScript

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Building Java Programs

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

DATA AND ABSTRACTION. Today you will learn : How to work with variables How to break a program down Good program design

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Constants and Variables

Fundamental of Programming (C)

Chapter 2: Using Data

Ex: If you use a program to record sales, you will want to remember data:

Introduction to Engineering gii

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Reserved Words and Identifiers

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

KNOWLEDGE OF NUMBER SENSE, CONCEPTS, AND OPERATIONS

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Operators and Expressions:

ITP 342 Mobile App Dev. Code

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Python Programming Exercises 1

1-3 Variables and Algebraic Expressions. Warm Up Problem of the Day Lesson Presentation Lesson Quizzes

Types and Expressions. Chapter 3

Arithmetic Operations

2.Simplification & Approximation

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Introduction to Computer Use II

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

VARIABLES & ASSIGNMENTS

Topic 4 Expressions and variables

Fundamentals of Programming CS-110. Lecture 2

Chapter 3: Operators, Expressions and Type Conversion

Lecture 3 Tao Wang 1

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Chapter 2. Data Representation in Computer Systems

Programming for Engineers Introduction to C

Scheme of work Elementary 1, the maths pathway

Web Application Development (WAD) V th Sem BBAITM(Unit-1) By: Binit Patel

Data types, variables, constants

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Basic data types. Building blocks of computation

Basics of Java Programming

ANSI C Programming Simple Programs

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Bits, Words, and Integers

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

The C++ Language. Arizona State University 1

Unit 1. Word Definition Picture. The number s distance from 0 on the number line. The symbol that means a number is greater than the second number.

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Multiply Decimals Multiply # s, Ignore Decimals, Count # of Decimals, Place in Product from right counting in to left

UNIT- 3 Introduction to C++

Introduction to Computer Programming

Variable A variable is a value that can change during the execution of a program.

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Department of Computer Science

2.2 Order of Operations

CS313D: ADVANCED PROGRAMMING LANGUAGE

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Introduction to TURING

LCSL Reference Manual

California Standard Study Island Topic Common Core Standard

Objective- Students will be able to use the Order of Operations to evaluate algebraic expressions. Evaluating Algebraic Expressions

L-System Fractal Generator: Language Reference Manual

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Transcription:

1 BEGINNING PROBLEM-SOLVING CONCEPTS FOR THE COMPUTER Chapter 2

2 3 Types of Problems that can be solved on computers : 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.

Fundamental Concepts 3 The building blocks of equations and expressions Constants Variables Operators Functions Expressions equations

Constants 4 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, Sara, +, -, /

5 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

Variables 6 May change during processing In many programming languages, variables are called identifiers. The variable name should be consistent with what the value of the variable represents A name is assigned to each variable used in a solution. The name references the memory area where the value is stored.

Variables 7 The computer uses the name as a reference to help it find that value in its memory. May be of any data type Examples Age, LastName, Address, PayRate

Difference between name of a variable and value of a variable 8 The rule for naming variable differ from language to language. there will be no character- length limitations for variable. The computer uses the variable name to find the location; it uses the value found at the memory location to do the processing.

Rules for Naming Variables 9 1- name variable according to what it represents Hours for hours worked. x H for hours worked (correct but not recommended because it is not clear enough). PayRate for rate of pay. x P for rate of pay (correct but not recommended because it is not clear enough). Create as short a name as possible, but one that clearly represents the variable.

2- Do not use spaces HoursWorked for hours worked (correct). x Hours Worked Incorrect because the space. StudentName for student name (correct). x Student Name Incorrect because the space.

3 - Start a variable name with a letter. Hours for hours worked (correct). x 2hours Incorrect because starting by number 2. Student for student name (correct). x 8Student Incorrect because starting by number 8

4- Do not use a dash ( or any other symbol that is used as mathematical operator). For example: StudentName for student name (correct). x Student-Name Incorrect because using a dash symbol.

13 4 - Be consistent when using upper-and lower case characters. HOURS is different variable name than Hours For example: Age and AGE are two variables (not the same variable).

5 - Exact variable name must be used in all places where the data item is used. For example: If the data item hours worked has the variable name of Hours, Hours must be used consistently. Hours hours worked x Hrs or HoursWorked (Incorrect, it is a new variable).

6 - Use the naming convention specified by the company where you work. The naming convention is uppercase for the first character in each of the words in the name, with no spaces between words in the name. Variable Names PayRate Rate HoursWorked Amount Tempreature StudentName Constant Name PI KCONATANT MULTIPLIER

Data type 16 Data unorganized facts. They go into the computer as input. Information The output of processing of input. Is printed in the form of reports.

17 Processing Data How a computer Balances a Checkbook Data Input Data Processed into Information Output Report Checks Deposits Bank Charge Calculates the Balance Balance Sheet

18 Data Types Data can come with different type :(Most common types) Numeric Character Logical Computer should know the data type of each variable or constant. Most languages include other data types Date User-defined

Numeric Types 19 Include all types of numbers. The only data type that can be used in numeric calculation. Integers 3, 5, -5, 0, -1 and 32,767 Real numbers (floating point numbers) Are whole numbers plus decimal parts (3.1459, -5745) Scientific Notation 2.3E5 or 5.4E-3, where E stands for the Power of 10 Can be Positive or Negative. Numeric data are used for value ( salary, rate of pay)

Data Types & Data Sets 20 Each data type has a data set A data set is the set of value that are allowed in a particular data type.

Data Types & Data Sets Data Type Data Set Examples Numeric :Integer All whole numbers 3456-43 Numeric :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 Mulder Scully 123-45-6789 Logical True or False True False 21

Character Types 22 Alphanumeric data set Consists of all single-digit numbers, letters, and special characters available to the computer contained within quotation marks An upper case letter is considered a different character from a lower case letter.

Table of ASCII Characters 23 Char Dec Char Dec A 65 a 97 B 66 b 98 C 67 c 99 D 68 d 100 E 69 e 101........................

24 String Data Type Made up of one or more characters. Contained within quotation marks Cannot be used within calculations Items that would not have mathematical computations performed on them should be designed string data type. some languages do not differentiate between characters and strings.

Strings - Caution 25 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

Comparing Characters & Strings 26 The computer s particular coding scheme (ASCII, Unicode, or EBCDIC) converts the character to its associated ordinal number The computer gives each character a number. The number representation is necessary because the computer work only with numbers. example: A 65 a 97

Comparing Characters & Strings 27 Note: Uppercase letters have a smaller ordinal values than lowercase letters A = 65 whereas a = 97 (ASCII 8-bit) Once converted, each character, one at a time, can be compared. Example: Banana is larger than Apple because B has larger number representing it than A COP1006 McManus

Comparing Characters & Strings 28 Concatenation could be used to join a first name with a last name or to join pieces of data together. Characters and String data can be compared and arranged in alphabetical order (as ascending order).

Concatenation 29 Joins character data or string data together with the + operator. Example : James + + T. + + Kirk = James T. Kirk 100 + 3 = 1003 not 103

Logical (Boolean) Data Type 30 Consists of two value in the data set : True and False Some languages accept yes, T, and Y for true. no, F, and N c for False. Example : Check someone s credit record ; true means credit is ok. False means it s not ok. For example, the variable Warm might be declared as a logical data type to describe if temperature values are warm or not.

Other Data Types 31 date data type and user defined data type Date data type is a number for the date that is the number of days from a certain date. Is a numeric data type because you can perform mathematical calculation on any date.

32 User-Defined Data Types User-defined Data types Defined by the user as a new data type Example( kind of cars ) Car_Type End GMC Ford Toyota

Rules for Data Types 33 Programmer chooses the data type during the programming process. The data that define the value of a variable or a constant will be one of three data type : numeric, character or logical. Data types can not be mixed, Once a constant or a variable has been given a data type it cannot change. All data to be used in calculations must be declared as a numeric data type.

Rules for Data Types 34 Each data type uses a data set or domain Numeric uses the set of all base 10 numbers, the plus sign(+), and the negative sign(-). Characters all characters included in the computer. Logical the words true and false

Examples of Data Types Data The price of item 4.25 Account number 1234567 Room numbers 112 Name of company Apple Macintosh Check status Pass/Fail True or False Date 12/3/2011 Data Type Numeric Real String Numeric Integer String Logical Date or String

36 How the Computer Stores Data Data Storage Each variable name is given a memory location Each memory location can hold one and only one value at a time. This memories are temporary storage, when a user enters a new value the old one is destroyed Example: COP1006 McManus

Functions 37 Small sets of instructions that perform specific tasks and return values. FunctionName(data) Are used as parts of instructions in a solution. Two types: Pre-defined each language has a set of functions within it. User-defined Programmers write their own functions.

Functions 38 The name of function may vary from language to language Benefits Reduces the amount of code that needs to be written, thus reducing errors of repetition.

Function Parameters 39 the data which is listed as part of the function and are called parameters. In Sqrt(n), the n represents the parameter, in this case a number In Value(s), the s represents a string Not all function need parameters In Random, no data are needed Parameter can be a constant, a variable, or an expression

Function Types 40 1. Mathematical 2. String Used in science and business, Calculate such things as square root, absolute value Manipulate string variable 3. Conversion Convert data from one data type to another 4. Statistical 5. Utility Important in business programming

Mathematical Functions 41 Function Form Example Result Square Root Sqrt(n) Sqrt(4) 2 Absolute Abs(n) Abs(-3), Abs(3) 3 Rounding Round(n,n1) Round (3.7259,2) 3.73 Integer Integer(n) Integer(5.7269) 5 Random Random Random 0.239768 Sign of a number Sign(n) Sign(7.39) 1

String Functions 42 Function Form Example Result Middle String Mid(s, n1, n2) Mid(S,3,2) where S = "Thomas" "om" Left String Left(s, n) Left(S,3) where S = "Thomas" "Tho" Right String Right(s, n) Right(S, 3) where S = "Thomas" "mas" Length(S) where S Length String Length(s) = "Thomas" 6

Conversion Functions 43 Function Form Example Result Valued Conversion Value(s) Value("-12.34") -12.34 String Conversion String(n) String(-12.34) "-12.34" For example, since character string can not be used in calculations, one of these functions would convert a string value to a numeric value

Statistical Functions 44 Function Form Example Result Average Average(list) Average(5, 3, 8, 6) 5.5 Maximum Max(list) Max(5, 3, 8, 6) 8 Minimum Min(list) Min(5, 3, 8, 6) 3 Summation Sum(list) Sum (5, 3, 8, 6) 22

Utility Functions 45 Functio Definition Exampl Result n e Date Time Error Returns the current date from the system. The date may be in various forms: mm/dd/yy, day only, month only, year only, or Julian calendar Returns the current time from the system. The time may be in various forms: hh:mm:ss, seconds from midnight, or minutes from midnight Returns control to the program when a systems error occurs. Date Time 09/15/08 9:22:34

Operators 46 The data connectors within expressions and equations. thy tell the computer how to process the data. They also tell the computer what type of the processing( mathematical, logical, or..) needs to done.

The operand are the data that the operator connect and processes ( constants or variable). The data type of the operands of an operator must be the same. The resultant is the answer that results when the operation is completed. The data type of operands and the resultant depends on the operator. Ex : 5 + 7

Operator Types 48 1. Mathematical The data types of operands and resultants of mathematical operators are always numeric +, -, *, / \, Mod ( used in business ) ^ functions

Two Special Math Operators 49 Integer Division (\) 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.

Mathematical Operators Operator Compute r Symbol Operation Resultant Addition + 3.0 + 5.2 8.2 Subtraction - 7.5 4.0 3.5 Multiplication * 8.0 * 5.0 40.0 Division / 9.0/4.0 2.25 Integer division \ 9\4 2 Modulo division MOD 9 MOD 4 1 Power ^ 3^2 9

51 Relational Operators Relational operators Perform comparisons the use of relational operators is the only way for the computer to make decisions. The resultant of relational operation is logical data type. The operand s can be numeric or character

Relational operatores Operator Computer Symbol Operation Resultant Equal to = 5 = 7 False Less than < 5 < 7 True Greater than > 5 > 7 False Less than or equal to < = 5 < = 7 True Greater than or equal to > = 5 > = 7 False Not < > 5 < > 7 True

Logical Operators 53 AND, OR, NOT used to connect relational expressions. a = b AND c < d Perform operations on logical data. They are defined by using Truth Tables

Logical opeartores Operator Compute r Symbol Operation Resultant Not NOT NOT True False And AND True AND True True Or OR True OR False True

The AND Operator 55 Significance: The only time the result is True is if both A and B are True. A B A AND B True True True True False False False True False False False False

The OR Operator 56 Significance: The only time the result is False is if both A and B are False. A B A OR B True True True True False True False True True False False False

57 The NOT Operator Also called the logical complement or logical negation Significance: Whatever the value of A is, NOT A will be the opposite. A True False NOT A False True

58 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. The processing of the operands always starts with functions then the innermost parentheses and works outward, and processes from left to right Note: Whenever you want to clarify an equation, use the parentheses!

The Order of Precedence 59 Order of Operations Operand Data Type Resultant Data Type ( ) reorders the hierarchy; all operations are completed within the parentheses using the same hierarchy Power ^ \, MOD *, / +, _ =, <, >, < =, > =, < > NOT AND OR Numeric Numeric Numeric Numeric Numeric or string or character Logical Logical Logical Numeric Numeric Numeric Numeric Logical Logical Logical Logical

Example 60 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

61 A or B or C and D and A and B and (not C) or (not D) True False False True False False False True True

62 Expressions & Equations Expressions Process the data and the operands, through the use of the operators Does not store the resultant (answer) Examples : to find the number of square feet in a room : Length * Width

Equations 63 Same as an expression, except the equation stores the resultant (answer) in a memory location takes on the value of not equals Examples Area = Length * Width Often called Assignment Statements

Expressions A+B A and B are numeric. The resultant is numeric and is not stored A < B A and B are numeric, character, or string. The resultant is logical and is not stored. A OR B A and B are logical. The resultant is logical and is not stored Equations C= A + B C,A and B are numeric. The resultant is stored in C. C = A < B A and B are numeric, character, or string. The resultant is stored in C ; C is logical. C = A OR B C,A and B are logical. The resultant is stored in C.

Example : evaluating a mathematical expression. 5 * ( X + Y ) 4 * Y / ( Z + 6 ) uses the following values to evaluate the expression : X = 2 Y = 3 Z = 6 Operation resultant 1. X + Y 5 2. Z + 6 12 3. 5 * resultant of 1 25 4. 4 * Y 12 5. Resultant of 4 / resultant of 2 1 6. Resultant of 3 resultant of 5 24

Example : evaluating a relational expression. A 2 B uses the following values to evaluate the expression : A = 6 b= 8 Operation resultant 1. A 2 4 2. Resultant of 1 B false

Example : evaluating a logical expression. A AND B OR C AND A uses the following values to evaluate the expression : A = true B = false C = true Operation resultant 1. A AND B false 2. C AND A true 3. Resultant of 1 OR resultant of 2 true

Straight-Line Form 68 Algebraic Expressions cannot be represented in the computer. They must be converted to a form the computer will recognize. Thus, Straight Line Form Y ( X ( X ( X Y ) = (X Y) / ( (X + Y)^2 / (X Y )^2 ) Y ) Y ) 2 2 straight line form

Attachment: ASCII Coding 69 COP1006 McManus