CS005 Introduction to Programming

Slides:



Advertisements
Similar presentations
Method Parameters and Overloading. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
Advertisements

Assignment Array Sorter NSF WorkshopPam Lawhead. Overview The purpose of this assignment is to give the student experience using Java arrays, light sensors.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.
Box and Whisker Plots. Introduction: Five-number Summary Minimum Value (smallest number) Lower Quartile (LQ) Median (middle number) Upper Quartile (UP)
Method Parameters and Overloading Version 1.0. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
Constraints Feasible region Bounded/ unbound Vertices
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Precedence Operators Error Types
Week 2 - Wednesday CS 121.
Topic: Functions – Part 1
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
NEW UNIT: Samples & Populations
Functions f(x)=2x-7 g(x)=x+12 Bob Greer.
CS005 Introduction to Programming
CS005 Introduction to Programming
Arrays and Array Visualization
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Method Parameters and Overloading
Functions CIS 40 – Introduction to Programming in Python
Gateway Quiz #1 Results:
CLOSE Please YOUR LAPTOPS, and get out your note-taking materials.
Stack Data Structure, Reverse Polish Notation, Homework 7
CS005 Introduction to Programming
NEW UNIT: Samples & Populations
Introduction to Object-Oriented Programming
Calculus - Mr Santowski
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
CS005 Introduction to Programming
Objective: Be able to add and subtract directed numbers.
Box and Whisker Plots Algebra 2.
Python Mr. Husch.
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Fundamentals of Programming
Value returning Functions
Rules of Integers.
Paper SOLO/PAIR Preparation
CS005 Introduction to Programming: Matlab Eamonn Keogh
Alice Variables Pepper.
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Box and Whisker Plots.
Warm-up (10 min.) I. Factor the following expressions completely over the real numbers. 3x3 – 15x2 + 18x x4 + x2 – 20 II. Solve algebraically and graphically.
2.4 Solving Multi-Step Equations
Functions In Matlab.
CS005 Introduction to Programming: Matlab
Warm up ADD 1.) ) 3.) 4.) 7 + (-3) 5.) 0 + (-4) 1.) 8 2.) -11
Eamonn Keogh CS005 Introduction to Programming: Matlab Eamonn Keogh
Hardness Of Approximation
Sketch the graph of each function. 1. −
LMC Little Man Computer What do you know about LMC?
A#2 / 0-4 Homework Worksheet
Introduction to Programming Concepts (VRH )
X y y = x2 - 3x Solutions of y = x2 - 3x y x –1 5 –2 –3 6 y = x2-3x.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Chapter 1: Boundary Value Testing
Selection Statements Chapter 3.
14.2 Measures of Central Tendency
Objective: Be able to add and subtract directed numbers.
CPS125.
Lecture 20 – Practice Exercises 4
Lecture 20 – Practice Exercises 4
Sorting and selection Prof. Noah Snavely CS1114
Python Creating a calculator.
Presentation transcript:

CS005 Introduction to Programming Matlab Eamonn Keogh eamonn@cs.ucr.edu

More on Designing Functions Let us get more practice using and writing functions We have seen max is a built-in function that can find the largest of two numbers. Does it work for three numbers? No. Let us write our own function that can..

In the black box view of max it is always expecting two input arguments, and to return one number. So we need to write a function that is expects three input arguments, and to return one number. Actually, MyMax is not a good name, it is not descriptive enough. Let is use MaxOfThreeNumbers 1 7 max 10 1 7 2 MyMax 7

The name of the function is MaxOfThreeNumbers We need names for the three input arguments Let us use Number1, Number2, Number3 We need a name for the returned variable, let us call it ReturnedNum 1 7 2 MaxOfThreeNumbers 7

7 1 2 MaxOfThreeNumbers ReturnedNum 7 Above is how we want to be able to use the function (be we have not written it yet) Here: ‘1’ gets bound to Number1 ‘7’ gets bound to Number2 ‘2’ gets bound to Number3 Some calculations happen … And the answer (in this case 7) gets assigned to ReturnedNum 1 7 2 MaxOfThreeNumbers Number1, Number2, Number3 ReturnedNum 7

MaxOfThreeNumbers 2 HisAge 1 ReturnedNum 19 Here: ‘1’ gets bound to Number1 HisAge gets bound to Number2 ‘3’ gets bound to Number3 Some calculations happen … And the answer (in this case 19) gets assigned to ReturnedNum MaxOfThreeNumbers 2 19 HisAge 1 Number1, Number2, Number3 ReturnedNum

We begin by opening an editable file, just like we did for scripts…

MaxOfThreeNumbers Number1, Number2, Number3 ReturnedNum See how the first line of our function maps onto our black box view of a function.

Stub Testing Here is a clever idea. Before we write the real code, we put in a temporary “stub” that just returns say 999. We can now test the function.

The real code I Here is one solution

The real code II Here is another solution

Testing the Code This version of the code has a bug! Can you see it?

The buggy code passes this test And it passes this test. But it fails this test. We must test our code!

Matlab did try to warn us that the code was buggy However, there will always be bugs that Matlab cannot catch.

HomeWork This will not be collected or graded, but you must do it, before Friday’s lab. Write a function that finds the min of three numbers. Write a function that finds the max of four numbers.

Programming Requires Cleverness We have seen how to get the maximum of three numbers, i.e. the 7 from {1, 7, 2} We have seen how to get the minimum of three numbers, i.e. the 1 from {1, 7, 2} How can we get the middle value, i.e. the 2 from {1, 7, 2}?

Programming Requires Cleverness Let us add the three numbers 1 + 7 + 2 = 10 Now subtract the max number, which was 7 10 – 7 = 3 Now subtract the min number, which was 1 3- 1 = 2 And 2 was the middle number..

Programming Requires Cleverness Let us add the three numbers 1 + 7 + 2 = 10 Now subtract the max number, which was 7 10 – 7 = 3 Now subtract the min number, which was 1 3- 1 = 2 And 2 was the middle number..

Programming Requires Cleverness