CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer...
-
date post
21-Dec-2015 -
Category
Documents
-
view
214 -
download
1
Transcript of CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer...
![Page 1: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/1.jpg)
CIS 101: Computer Programming and Problem Solving
Lecture 4Usman Roshan
Department of Computer Science
NJIT
![Page 2: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/2.jpg)
Overview
• So far we have seen how to define basic mathematical expressions and how to manipulate arrays and matrices
• Now we will look at script files and functions. They are useful for storing sets of commands and defining them apriori. Also an important step before we get into programming with control and flow operators
• Today: chapters 4 and 6
![Page 3: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/3.jpg)
Scripts
• Used for writing programs
• Script variables are global. They can be accessed and modified from anywhere
![Page 4: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/4.jpg)
Creating scripts
![Page 5: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/5.jpg)
Editor for scripts
![Page 6: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/6.jpg)
Running script
![Page 7: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/7.jpg)
Output of script
![Page 8: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/8.jpg)
Script for mean of three numbers
![Page 9: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/9.jpg)
Output of mean script
![Page 10: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/10.jpg)
fprintf command
• A powerful command
• General syntax
fprintf(‘text as string %-5.2f additional text’, variable name)
Formatting elements
![Page 11: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/11.jpg)
Updated mean script
![Page 12: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/12.jpg)
Output of updated mean script
![Page 13: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/13.jpg)
Writing to a file
• fid = fopen(‘file_name’, ‘permission’)
• File permissions– r = open for reading– w = open for writing (if file exists then content
is deleted)– a = open for appending
![Page 14: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/14.jpg)
Mean script output to file
![Page 15: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/15.jpg)
Mean script output to file
![Page 16: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/16.jpg)
Output file of mean script
![Page 17: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/17.jpg)
Function files
Function FileInput data Output
![Page 18: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/18.jpg)
Function syntax
function [output arguments] = function_name(input arguments)
Example functions:
function [A] = RectArea(x, y)
function [V, S] = SphereVolArea(r)
![Page 19: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/19.jpg)
Local and global variables
• So far ALL variables we have worked with have been global. This means they can be accessed and modified by any part of the program
• In functions variables are local by default (and global if they are defined as such)
![Page 20: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/20.jpg)
Memory space
Global memoryspace accessibleby all scripts andfunctions
Local spacefor function A
Local spacefor function B
![Page 21: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/21.jpg)
Defining and calling a function
• A function file must be saved before it can be used.
• The name of the file must be the same as the function name
• Examples:– Function Filename– function [A] = RectArea(x,y) RecArea.m– function[V, S] = SphereVolArea(r) SphereVolArea.m
![Page 22: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/22.jpg)
Function for computing area of sphere
![Page 23: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/23.jpg)
Area of sphere
It won’t run if you click on the run button
![Page 24: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/24.jpg)
Area of sphere
The function is called from the command line…
![Page 25: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/25.jpg)
Area of sphere
Or the function can be called from a script.
![Page 26: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/26.jpg)
Area of sphere
Function output from script
![Page 27: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/27.jpg)
Comparison between functions and scripts
• Both scripts and funtion files are saved with .m extension
• The first line in a function file is the definition line• Function variables are local whereas script ones
are global• Function files can accept data through input
arguments and can return data similarly• When a function file is saved, the name of the
file should be the same as the function’s.
![Page 28: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/28.jpg)
Inline functions
name = inline(‘math expression typed as a string’)
name = inline(‘math expression’, arg1, …, argn)
For example,
double = inline(‘2*x’)
defines a function that doubles the input. So
double(10) = 20
![Page 29: CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.](https://reader036.fdocuments.us/reader036/viewer/2022081519/56649d615503460f94a43c00/html5/thumbnails/29.jpg)
Using inline to define area of sphere