CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Programming with MATLAB
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
General Computer Science for Engineers CISC 106 Lecture 02 James Atlas Computer and Information Sciences 6/10/2009.
CIS 101: Computer Programming and Problem Solving Lecture 6 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
1 ICS102: Introduction To Computing King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Lecture 7 Sept 17 Goals: Complete Chapter 4 Chapters 5 and 6.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
CIS 101: Computer Programming and Problem Solving Lecture 5 Usman Roshan Department of Computer Science NJIT.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
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.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Chapter 1: Getting Started with MATLAB MATLAB for Scientist and Engineers Using Symbolic Toolbox.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Functions CS 103 March 3, Review A function is a set of code we can execute on command to perform a specific task A function is a set of code we.
Functions CS 103. Review A function is a set of code we can execute on command to perform a specific task When we call a function, we can pass arguments.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Artificial Intelligence Lecture No. 26 Dr. Asad Ali Safi ​ Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology.
Pascal Programming Today Chapter 11 1 Chapter 11.
XP New Perspectives on XML, 2 nd Edition Tutorial 7 1 TUTORIAL 7 CREATING A COMPUTATIONAL STYLESHEET.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 2 in MATLAB Topics Covered: 1.Functions in Script Files Inline Functions.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
D Copyright © 2009, Oracle. All rights reserved. Using SQL*Plus.
Physics 114: Lecture 1 Overview of Class Intro to MATLAB
Test 2 Review Outline.
EEE 161 Applied Electromagnetics
SQL and SQL*Plus Interaction
Introduction to Matlab
1-1 Logic and Syntax A computer program is a solution to a problem.
Introduction to Programming for Mechanical Engineers
Basic operations in Matlab
Functions (subprograms)
Programming Right from the Start with Visual Basic .NET 1/e
Introduction to Programming
funCTIONs and Data Import/Export
Islamic University of Gaza
Using SQL*Plus.
Topics Introduction to File Input and Output
Introduction to Programming
Presentation transcript:

CIS 101: Computer Programming and Problem Solving Lecture 4 Usman Roshan Department of Computer Science NJIT

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

Scripts Used for writing programs Script variables are global. They can be accessed and modified from anywhere

Creating scripts

Editor for scripts

Running script

Output of script

Script for mean of three numbers

Output of mean script

fprintf command A powerful command General syntax fprintf(‘text as string %-5.2f additional text’, variable name) Formatting elements

Updated mean script

Output of updated mean script

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

Mean script output to file

Output file of mean script

Function files Function File Input data Output

Function syntax function [output arguments] = function_name(input arguments) Example functions: function [A] = RectArea(x, y) function [V, S] = SphereVolArea(r)

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)

Memory space Global memory space accessible by all scripts and functions Local space for function A Local space for function B

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: –FunctionFilename –function [A] = RectArea(x,y)RecArea.m –function[V, S] = SphereVolArea(r) SphereVolArea.m

Function for computing area of sphere

Area of sphere It won’t run if you click on the run button

Area of sphere The function is called from the command line…

Area of sphere Or the function can be called from a script.

Area of sphere Function output from script

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.

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

Using inline to define area of sphere