PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Arithmetic Calculations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Methods Java 5.1 A quick overview of methods
Building Java Programs
Return values.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Chapter 6: User-Defined Functions I
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
Chapter 6: User-Defined Functions I
1 Python Chapter 2 © Samuel Marateck, After you install the compiler, an icon labeled IDLE (Python GUI) will appear on the screen. If you click.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Java Data Types. Primitive Data Types Java has 8 primitive data types: – char: used to store a single character eg. G – boolean: used to store true or.
CSC Programming I Lecture 5 August 30, 2002.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
1 Lecture04: Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
1 Math Expressions and Operators. 2 Some C++ Operators Precedence OperatorDescription Higher ( )Function call +Positive - Negative *Multiplication / Division.
CSC Programming for Science Lecture 7: Math Functions.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
CSC 107 – Programming For Science. The Week’s Goal.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
CSCE 121: Introduction to Program Design and Concepts, Honors J. Michael Moore Spring 2015 Set 14: Plotting Functions and Data 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Computer Graphics Basic Maths for Graphics in C++ CO2409 Computer Graphics Week 4.
Chapter 3: User-Defined Functions I
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
PHY 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Problem of the Day  Why are manhole covers round?
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
CSC Programming for Science Lecture 5: Actual Programming.
CSE 110: Programming Language I Afroza Sultana UB 1230.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Variables, Operators, and Expressions
Computer Graphics Basic Maths for Graphics in C++
Arithmetic Expressions
Week 3 - Wednesday CS 121.
Chapter 6 - Functions modular programming general function format
Chapter 2 Basic Computation
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
TMF1414 Introduction to Programming
Revision Lecture
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Terminal-Based Programs
Presentation transcript:

PHY 107 – Programming For Science

Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging is focus  All material is important & you are responsible for it  PPTX slides posted onto D2L for each lecture  Also post all solutions to activities, labs, & assignments  Grades available on D2L and updated regularly  Since submissions electronic, s sent with grades

The Lecture’s Goal

Functions  C/C++ actually tries being useful on occasion

Functions  C/C++ actually tries being useful on occasion

C/C++ Being Helpful functions  Defines built-in functions for use in any program  Functions in C/C++ work similar to algebraic functions  Consider black-box that takes value & returns another  (Will discuss other types of functions later)  Functions must be declared before using it  Just like variables, computer needs some warning  For built-in functions, use #include statements

Mathematical Functions  Add #include at top of file  Should go with #include and others  Order does not matter, can list however you want  All of these mathematical functions return value  Will NOT change arguments’ value(s), so safe to use

Mathematical Functions  Function result is ignored unless you take action  Use within an expression your program is computing  Result of the function can be assigned to variable  Could be ignored, but why bother calling function?

Mathematical Functions  Function result is ignored unless you take action  Use within an expression your program is computing  Result of the function can be assigned to variable  Could be ignored, but why bother calling function?

Using These Functions  abs( x ) returns absolute value of number  Result’s type matches type of expression x int i1 = abs(-1); double d1 = abs(-56.54); double d2 = abs(i1); int i2 = abs(i1 + 1 * d2); double d3 = d2 * abs(d1); i2 = 46 * abs(i1); d3 = abs(abs(d2));

Using These Functions  abs( x ) returns absolute value of number  Result’s type matches type of expression x int i1 = abs(-1); double d1 = abs(-56.54); double d2 = abs(i1); int i2 = abs(i1 + 1 * d2); double d3 = d2 * abs(d1); i2 = 46 * abs(i1); d3 = abs(abs(d2));

Other Functions  Decimal data only returned by trig. functions sin( x ), cos( x ), tan( x ), asin( x ), atan( x )…  Whether float or double depends on x ’s type  Measure angle in radians for these to work (2π = 360˚)  Exponent functions also return decimal data log10( x ), sqrt( x ), log( x ), exp( x )…  x ’s type also specifies if float or double returned  Decimals needed since results could be decimal  pow( x, y ) computes x y  x ’s (decimal) type determines type of value computed

Errors In Functions  Some values may cause errors in function  Nothing output, but result appears funny if printed  If assigned to variable, variable used without error  Using funny value yields funny value for all equations acos( x ), asin( x ) x must be in range [-1, 1] sqrt( x ) x must be number ≥ 0 exp( x ) e x must be in range of x ’s type pow( x, y ) x y must fit in x ’s type; y ≥ 0

Rounding Functions  Safely convert decimal numbers into integers  floor( x ) returns x to nearest smaller integer ([ x ]) floor(2.01) returns 2.0 floor( ) returns 78.0 floor( ) returns -1.0 floor(floor( )) returns  ceil( x ) takes x & returns nearest larger integer ([ x ]) ceil(2.01) returns 3.0 ceil( ) returns 79.0 ceil( ) returns 0.0 ceil(ceil( )) returns -65.0

What Planet Are [They] From?  Why do floor( x ) & ceil( x ) return decimals

What Planet Are [They] From?  Why do floor( x ) & ceil( x ) return decimals

Data Types  Assignments are legal only if always safe  C/C++ defines ordering of legal assignments long double double float long int short char Legal to assign to higher type

Type of An Expression  Within expression, C/C++ tracks types computed  Uses simple rules and cannot apply common sense  As seen in integer division, this can have big impact  Computers are stupid & cannot think ahead  Type of expression computed step-by-step as it goes  Only examines current operation & ignores future if  Looks at arguments’ types & promotes if needed

Type of An Expression

Computing Expression Type int whole; double stuff; whole = abs((5 * 3 / 2) + (10 / 2.0)); stuff = (3 + 4 * 1.0) / (3 / (2 * 2) + 1.0);

Computing Expression Type int whole; double stuff; whole = abs((5 * 3 / 2) + (10 / 2.0)); stuff = (3 + 4 * 1.0) / (3 / (2 * 2) + 1.0);

Typecasting Requires typecast to work

Typecasting int i1 = (int)(-1.0); char c1 = (char)(abs(48.3)); float f1 = (float)(48.3); i1 = (int)(f1 * 2); c1 = i1 + 3; c1 = (char)(i ); i1 = (char)(i ); f1 = (char)(f ); i1 = (int)(49.0 * 2);

Typecasting int i1 = (int)(-1.0); char c1 = (char)(abs(48.3)); float f1 = (float)(48.3); i1 = (int)(f1 * 2); c1 = i1 + 3; c1 = (char)(i ); i1 = (char)(i ); f1 = (char)(f ); i1 = (int)(49.0 * 2);

Normal Rounding  C/C++ lacks function for typical rounding  floor( x + 0.5) works with numbers > -0.5  ceil( x + 0.5) works with numbers < -0.5  We will revisit this problem later…

Your Turn  Get in groups & work on following activity

For Next Lecture  Week #3 weekly assignment due Tuesday at 5PM  Read web page for Friday  How does the computer store information?  Why do we use numbers to represent characters?  Why is this funny?