22 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

Slides:



Advertisements
Similar presentations
Ch 4 Sec 2: Slide #1 Columbus State Community College Chapter 4 Section 2 Writing Fractions in Lowest Terms.
Advertisements

Types and Arithmetic Operators
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1.1 Numbers Classifications of Numbers Natural numbers {1,2,3,…}
division algorithm Before we study divisibility, we must remember the division algorithm. r dividend = (divisor ⋅ quotient) + remainder.
Operators and Expressions
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
Munster Programming Training
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 3.  Traditionally, programming languages have assigned different types of data for different types of numbers.  In many languages, there may.
Python Types Python values are of various “types” Ints, Floats, Strings, Characters, and more Two representations of numbers 1 vs 1.0.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Divisibility Rules and Finding Factors
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
THE REAL NUMBERS College Algebra. Sets Set notation Union of sets Intersection of sets Subsets Combinations of three or more sets Applications.
Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Doing math In java.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
5 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
4 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introductory Algebra Glossary The Language of Math.
Introduction to Programming
Introduction to Programming
Introduction to Computer Systems
Introduction to Programming
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Chapter 7: Expressions and Assignment Statements
Lecture 4: Expressions and Variables
Introduction to Programming
Chapter 7: Expressions and Assignment Statements
Introduction to Programming
Introduction to Programming
Arithmetic Operator Operation Example + addition x + y
Introduction to Programming
Data Representation in Computer Systems
Place Value, Names for Numbers, and Reading Tables
Numbers.
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Chapter 3 Operators and Expressions
Topics Designing a Program Input, Processing, and Output
Lecture 4: Expressions and Variables
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Introduction to Programming
Introduction to Programming
Introduction to Programming
Class code for pythonroom.com cchsp2cs
Presentation transcript:

22 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 2016 Week 3:Pseudocode and Arithmetic

Overview Review of week 2: number types and creating variables Pseudocode Arithmetic See Python for Everyone, Ch January 2015Birkbeck College, U. London2

Recall Number Types  Type int: whole numbers, positive, zero and negative, e.g. 1, 0, -1.  Type float: decimal fractions, e.g. 1.28, 3.0,  Warning: a number literal such as 1.3 cannot be represented exactly by a number of type float. There are numbers of type float very near to 1.3. This is sufficient for most applications. 22 January 2016PFE Section 2.13

Recall Creation and Initialisation of Variables cansPerPack = 6 # create the variable cansPerPack and initialise it with # the value 6 of type int cansPerPack = 8 # overwrite the previous value 6 with the new value 8 cansPerPack = 8.5 # The values of cansPerPack can be switched from type # int to type float and conversely 22 January 2016PFE Section 2.14

Problem Statement You have the choice of buying two cars. One is more fuel efficient than the other, but also more expensive. You know the price and fuel efficiency (in miles per gallon, mpg) of both cars. You plan to keep the car for 10 years. Assume a price of £4 per gallon of petrol and usage of 15,000 miles per year. You will pay cash for the car and not worry about financing costs. Which car is the better deal? 22 January 2016PFE Section 1.75

Problem Break Down  First stage Find the total cost for each car Choose the car that has the lowest total cost  Second stage : total cost of a car = purchase price + operating cost  Third stage: operating cost = 10*annual fuel cost  Fourth stage: annual fuel cost = price per gallon*annual fuel consumed  Fifth Stage: annual fuel consumed = annual miles driven/fuel efficiency 22 January 2016PFE Section 1.76

Description of Each Step  The descriptions are in pseudocode. The steps are ordered such that each step can be carried out using the results of previous steps  for each car, compute the total cost as follows annual fuel consumed = annual miles driven/fuel efficiency annual fuel cost = price per gallon x annual fuel consumed operating cost = 10 x annual fuel cost total cost = purchase price + operating cost if total cost 1 < total cost 2 choose car 1 else choose car 2 22 January 2016PFE Section 1.77

Example  R1.15. You want to decide whether you should drive your car to work or take the train. You know the distance from your home to your place of work, and the fuel efficiency of your car (in miles per gallon). You also know the price of a return train ticket. The cost of petrol is £4 per gallon and car maintenance is 20p per mile. Write an algorithm to decide which commute is cheaper. 22 January 2016PFE Ch. 1, Review questions8

Operators, Variables and Literals  Operators act on one or more numbers to produce a new number, e.g. Addition + Subtraction – Multiplication * Division /  Variables p, q, cansPerPack, …  Number literals 4, 5, -64.8, , … 22 January 2016PFE Section 2.29

Expressions  An expression is a combination of operators, variables, literals and parentheses  Examples (3+4)/2 p+p*p (p+p)*p January 2016PFE Section 2.210

Arithmetic Operators in Python  Addition: p = 3+4 # assign value of type int q = # assign value of type float  Subtraction: p = 3-4 q =  Multiplication p = 4*5 # other versions, e.g. 4x5, not permitted q = 4.0*5 # assign value of type float 22 January 2016PFE Section 2.211

Power Operation 22 January 2016PFE Section 2.212

Division and Remainder Operators  Division p = 6/4 # assign value 1.5 of type float q = 3.1/7 # assign value of type float  Floor division p = 6//4 # round down to 1 q = (-6)//4 # round down to -2 # avoid using // with arguments of type float  Remainder p = 5%4 # remainder 1 on dividing 5 by 4 q = (-5)%4 # remainder 3 (-5 = 4q+r with q=(-5)//4) 22 January 2016PFE Section 2.213

Table for Floor Division and Remainder 22 January 2016PFE Section Expression where n=1729 Value Comment n%10 9 for any positive integer n, n%10 is the last digit of n n// This is n without the last digit n% The last two digits of n n%2 1 -n// Check the web for the significance of 1729

Decimal Digits  The operators // and % can be used to extract the digits of a decimal integer  Examples 385%10 = 5 385//10 = 38 (385//10)%10 = 8  To extract from an integer n the ith digit from the right, use (n//(10**(i-1)))%10 22 January 2016PFE Section

More About Expressions  Literals and names of variables are expressions  If e, f are expressions then (e)+(f), (e)-(f), (e)*(f), (e)/(f), (e)**(f) are expressions  Examples: 4, 5, p are three expressions, therefore (p)+(4) is an expression, therefore (5)*((p)+(4)) is an expression, and so on 22 January 2016PFE Section

Precedence The number of brackets needed in an expression is reduced by specifying a precedence for the operators Exponentiation ** takes precedence over multiplication *, real division /, remainder %, which in turn take precedence over addition + and subtraction - 22 January 2016PFE Section 2.217

Examples of Precedence p = 4*3+1 # value 13 p = 2**3*3 # value 24 p = 3.0*5.5/6.0 # value 2.75 # A precedence rule is not required for * and real # division / 22 January 2016PFE Section See PFE Appendix B. If in any doubt use brackets

Built in Function abs The function abs takes a number as an argument and returns the absolute value of that number, e.g. distance1 = abs(-5) # the argument of abs is -5, the value 5 is returned distance2 = abs(5) # the argument of abs is 5, the value 5 is returned 22 January 2016PFE Section

Additional Built in Functions  round(x): return the value of x rounded to a whole number p = round(1.5) # assign the value 2 q = round(-1.5) # assign the value -2  round(x, n): return the value of x rounded to n decimal places p = round(1.572, 2) # assign the value 1.57  max(x, y, z, …): return the largest value of the arguments  min(x, y, z, …): return the smallest value of the arguments 22 January 2016PFE Section

Associativity  All operators have left to right associativity except exponentiation which has right to left associativity p=3-4-7 # value (3-4)-7 = -8 p=1/2/4 # value (1/2)/4 = p=2**2**3 # value 2**(2**3) = 256 p=4+5+6 # rule for associativity not required 22 January 2016PFE Section See PFE Appendix B. If in any doubt use brackets. Never write anything as horrible as 1/2/4

Question R2.5  What are the values of the following expressions? In each line assume x = 2.5 y = -1.5 m = 18 n = 4 22 January 2016PFE Section 2 Review Questions22 1.x*y-(x+n)*y 2.m//n+m%n 3.5*x-n/5 4.1-(1-(1-(1-(1-n)))) 5.sqrt(sqrt(n)) # sqrt is the square root function

Question R January 2016PFE Section 2 Review Questions23