Chapter 2 Beginning Problem-Solving Concepts for the Computer

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Data Types and Expressions
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
Copyright © 2012 Pearson Education, Inc. Chapter 2 Beginning Problem-Solving Concepts for the Computer Problem Solving and Programming Concepts 9 th Edition.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Chapter 3 Planning Your Solution
String Escape Sequences
Basic Elements of C++ Chapter 2.
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:
Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs.
Copyright © 2012 Pearson Education, Inc. Chapter 2 Beginning Problem-Solving Concepts for the Computer Problem Solving and Programming Concepts 9 th Edition.
1 PHP Variables. 2 What is a variable? A variable is something that can be changed – a thing that can take on any value from a possible set of values.
2440: 211 Interactive Web Programming Expressions & Operators.
Computer Programming 12 Lesson 3 – Computer Programming Concepts By Dan Lunney.
1 CSC103: Introduction to Computer and Programming Lecture No 6.
CPS120: Introduction to Computer Science
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
An Object-Oriented Approach to Programming Logic and Design Chapter 1 An Overview of Computers and Logic.
Introduction to Programming with RAPTOR
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Beginning Problem-Solving Concepts for the Computer
Introduction to Computer Programming
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
ICT Introduction to Programming Chapter 4 – Control Structures I.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators.
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
ICS102 Lecture 8 : Boolean Expressions King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
BASIC ELEMENTS OF A COMPUTER PROGRAM
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Algebra.
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
The Selection Structure
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
Data Types, Identifiers, and Expressions
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Boolean Expressions to Make Comparisons
The Data Element.
The Data Element.
DATA TYPES There are four basic data types associated with variables:
Variables and Constants
Presentation transcript:

Chapter 2 Beginning Problem-Solving Concepts for the Computer CMPF144 Introduction to Problem Solving and Basic Computer

Overview Constants and Variables Data Types Functions Operators Expressions and Equations

Objectives Differentiate between variables and constants. Differentiate between character, numeric, and logical data types. Identify operators, operands and resultants. Identify and use functions Identify and use operators according to placement in hierarchy chart. Set up & evaluate expressions and equations using variables, constants, operators, and the hierarchy of operations.

Constant A value (content in memory cell, can be alphabetic and/or numeric) that will never change during the execution of computer program is referred to by its name (location of memory cell) Name is given in CAPITAL LETTER to differentiate from variable Example: PI = 3.142

Variable A value (can be any data type)that might change during the execution of computer program is referred to by its name also known as identifier there are rules for naming and using variables Example: number_of_student = ?? total_amount =?? depends on program execution

Rules for Naming and Using Variables Name a variable according to what it represents. Do not use spaces. Start a variable name with a letter. Do not use a dash or any other symbol that is used as a mathematical operator. Consistent usage of variable name. Consistent use of upper, lowercase characters in variable names Use naming convention specified by your company

Incorrect Variable Names Data Item Incorrect Variable Name Problem Corrected Variable Name Hours worked Hours Worked Space between words HoursWorked Name of client CN Does not define data item ClientName Rate of pay Rate-Pay Uses a mathematical operator PayRate Quantity per customer Quantity/Customer QuantityPerCustomer 6% sales tax 6%_sales_tax Starts with a number SixPercentSalesTax @ SalesTax Client address Client_address_for_client_of_XYZ_corporation_in_California Too long ClientAddress Variable name introduced as Hours Hrs Inconsistent name Hours Hours_worked pp 51, WHAT’s WRONG WITH THIS? Q1

Data Type a classification of the type of data that a variable or constant can hold in computer programming Each data type consists of a set of permitted values, which is known as data set Common categories of data type: Numeric Character String Logical Date

Numeric Include all types of numbers Can be used for numeric calculations 2 + 1 = ?? Subtypes include: Integer Whole number Can be positive of negative Example: 5297, -376 Real number Also known as floating point numbers Can be represented in scientific notation Example: 0.0054, 2.3E5 (2.3 x 105)

Numeric Data Types and Their Data Sets

Character / String Consists of all single-digit numbers, letters and special characters available to the computer Placed within quotation marks Example: “a”, “2”, “=“ Cannot be used for calculation even though some are numbers “2” + “1” = ?? Uppercase letter is different from lower case letter “A” ≠ “a” more than one character are combined together to form a string

Character / String (cont.) Can be compared and sorted in alphabetical order (computer gives each character an ASCII number) Banana > Apple Joan > James A < a Can be joined together via concatenation (by using + operator) “Hello” + “World” = “HelloWorld” “4” + “4” = ??

Character / String Data Types and Their Data Sets

Logical Only consists of two value – True or False Used to make yes-or-no decisions Example: result = True badInput = “yes” CreditOK = 1

Logical Data Types and Their Data Sets

Date Is a numeric data type as mathematical operations can be performed onto the value Allows user to subtract one date from another date

Examples of Data Types

Examples of Data Types (cont.) pp. 47, QUESTIONS, Q5

Functions Small sets of instructions that perform specific tasks and return values. Requires parameter General syntax : FunctionName (parameter) Example: Sqrt(N) – What is this?? (╥﹏╥) Syntax might vary for different programming language Can be used repeatedly in a program to shorten the problem-solving time and increase program’s readability

Examples of Functions

Examples of Functions (cont.)

Examples of Functions (cont.)

Examples of Functions (cont.)

Operators Tells the computer HOW to process data (example: add, subtract…) Informs the computer WHAT type of processing (example: mathematical, logical…) Two main concepts: OPERAND and RESULTANT Types of operators: Mathematical (standard mathematical calculation) Relational (to program decision) Logical (to connect relational expression and to perform operations on logical data)

Mathematical Operators and Their Computer Symbols

Relational Operators and Their Computer Symbols

Logical Operators and Their Computer Symbols

NOT Logical Operators

AND Logical Operators

OR Logical Operators pp. 48, QUESTIONS, Q9

Hierarchy of Operations pp. 48, QUESTIONS, Q10

Expressions and Equations Used to process data (operands) through operators Example: width * height Equation: Also known as assignment statements Stores the resultant of an expression in a memory location of a computer through equal “=” symbol (assignment operator) area = width * height

Expressions and Equations (cont.)

Setting Up a Numeric Expression Mathematical expression: X (3Y + 4) - Appropriate computer representation: 4Y X + 6 X * ( 3 * Y + 4) – 4 * Y / ( X + 6) pp. 49, PROBLEMS, Q1

Setting Up a Mathematical Equation Example: Appropriate computer representation: Only one variable on the left and an expression on the right of the “=” sign Y + 3 = X ( Z + 5) Y = X * ( Z + 5) - 3 pp. 49, PROBLEMS, Q2

Setting Up a Relational Expression Given an expression : Appropriate computer representation: Computer can’t understand “is lesser than” X is lesser than Y + 5 X < Y + 5

Evaluating a Mathematical Expression pp. 49, PROBLEMS, Q5

Evaluating a Relational Expression

Evaluating a Logical Expression

Evaluating Equation That Uses Both Relational and Logical Operators pp. 51, PROBLEMS, Q14

Developing a Table of All Possible Resultants of a Logical Expression One unknown - A. Two combinations: A can be either True or False

Developing a Table of All Possible Resultants of a Logical Expression (cont.) Two unknowns - A and B. Four combinations: B can be either True or False for each value of A.

Developing a Table of All Possible Resultants of a Logical Expression (cont.) Three unknowns - A, B, and C. Eight combinations.

Developing a Table of All Possible Resultants of a Logical Expression pp. 50, PROBLEMS, Q13