ENGR 1202: Introduction to Engineering Principles and Practice II 5' More Examples of Looping and Br - PowerPoint PPT Presentation

Loading...

PPT – ENGR 1202: Introduction to Engineering Principles and Practice II 5' More Examples of Looping and Br PowerPoint presentation | free to view - id: 25159a-NjI4M



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

ENGR 1202: Introduction to Engineering Principles and Practice II 5' More Examples of Looping and Br

Description:

5. More Examples of Looping and Branching. Jim Bowen. Dept. of Civil Engineering. UNC Charlotte. September 4, 2001. Review of Last Class ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 34
Provided by: jwhi2
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: ENGR 1202: Introduction to Engineering Principles and Practice II 5' More Examples of Looping and Br


1
ENGR 1202 Introduction to Engineering
Principles and Practice II 5. More Examples of
Looping and Branching
  • Jim Bowen
  • Dept. of Civil Engineering
  • UNC Charlotte
  • September 4, 2001

2
Review of Last Class
  • To get input from screen use the command?

3
Review of Last Class
  • To get input from screen use the command?
  • input x input(Enter x )

4
Review of Last Class
  • To get input from screen use the command?
  • input x input(Enter x )
  • To display output to screen use the command?

5
Review of Last Class
  • To get input from screen use the command?
  • input x input(Enter x )
  • To display output to screen use the command?
  • disp disp(x ,num2str(x))

6
Review of Last Class
  • To get input from screen use the command?
  • input x input(Enter x )
  • To display output to screen use the command?
  • disp disp(x ,num2str(x))
  • To add a variable controlled loop use?

7
Review of Last Class
  • To get input from screen use the command?
  • input x input(Enter x )
  • To display output to screen use the command?
  • disp disp(x ,num2str(x))
  • To add a variable controlled loop use?
  • a for loop for i110 … end

8
Review of Last Class
  • To get input from screen use the command?
  • input x input(Enter x )
  • To display output to screen use the command?
  • disp disp(x ,num2str(x))
  • To add a variable controlled loop use?
  • a for loop for i110 … end
  • A loop controlled by a logical expression?

9
Review of Last Class
  • To get input from screen use the command?
  • input x input(Enter x )
  • To display output to screen use the command?
  • disp disp(x ,num2str(x))
  • To add a variable controlled loop use?
  • a for loop for i110 … end
  • A loop controlled by a logical expression?
  • a while loop while ilt10, ii1, end

10
Review of Last Class
  • To get input from screen use the command?
  • input x input(Enter x )
  • To display output to screen use the command?
  • disp disp(x ,num2str(x))
  • To add a variable controlled loop use?
  • a for loop for i110 … end
  • A loop controlled by a logical expression?
  • a while loop while ilt10, ii1, end
  • To add branching, use an ?

11
Review of Last Class
  • To get input from screen use the command?
  • input x input(Enter x )
  • To display output to screen use the command?
  • disp disp(x ,num2str(x))
  • To add a variable controlled loop use?
  • a for loop for i110 … end
  • A loop controlled by a logical expression?
  • a while loop while ilt10, ii1, end
  • To add branching, use an ? if-else-end

12
In-class script 5.1
  • Ask for 2 numbers from the screen
  • Display to screen the following
  • How many of the numbers are even
  • Solution on web ics5_1.m, ics5_1a.m

13
In-class script 5.2 (ics5_2.m)
  • Write a script that does this
  • inputs integer from screen
  • Stops if the integer is lt 0
  • Displays number to screen if gt 0
  • Goes back to 1. if gt 0

14
In-class script 5.2 (ics5_2.m)
  • Write a script that does this
  • inputs integer from screen
  • Stops if the integer is lt 0
  • Displays number to screen if gt 0
  • Goes back to 1. if gt 0
  • Hints
  • While x gt 0 … end use while for outer
    loop

  • after initializing x to gt 0
  • if x gt0 use if inside loop
  • disp(x) displays x to screen

15
In-class script 5.2 (ics5_2.m)
  • Write a script that does this
  • inputs integer from screen
  • Stops if the integer is lt 0
  • Displays number to screen if gt 0
  • Goes back to 1. if gt 0

Script Outline x1 start
loop while x gt 0 input x if x is
positive display to screen end end
  • Hints
  • While x gt 0 … end use while for outer
    loop

  • after initializing x to gt 0
  • if x gt0 use if inside loop
  • disp(x) displays x to screen

16
Now Add a Feature to Program
  • Sum digits of number input from screen
  • For simplicity, input only integers

17
Now Add a Feature to Program
  • Sum digits of number input from screen
  • For simplicity, input only integers
  • HINT Use the int2str and fix functions

18
Now Add a Feature to Program
  • Sum digits of number input from screen
  • For simplicity, input only integers
  • HINT Use the int2str and fix functions
  • int2str - converts integer to string

19
Now Add a Feature to Program
  • Sum digits of number input from screen
  • For simplicity, input only integers
  • HINT Use the int2str and fix functions
  • int2str - converts integer to string
  • int2str(689) same as
    689

20
Now Add a Feature to Program
  • Sum digits of number input from screen
  • For simplicity, input only integers
  • HINT Use the int2str and fix functions
  • int2str - converts integer to string
  • int2str(689) same as
    689
  • fix - converts to integer (uses ASCII codes for
    strings, 1 49, 2 50, 3 51 …)

21
Now Add a Feature to Program
  • Sum digits of number input from screen
  • For simplicity, input only integers
  • HINT Use the int2str and fix functions
  • int2str - converts integer to string
  • int2str(689) same as
    689
  • fix - converts to integer (uses ASCII codes for
    strings, 1 49, 2 50, 3 51 …)
  • fix(2) - 48 same as 2
    (integer)

22
In-class script 5.3 (ics5_3.m)
  • Add this to the previous script (use x as
    variable)
  • Convert x to integer
  • Then convert x to string (character vector)
  • Then convert x to integer vector
  • Find answer as sum of vector x
  • Print result to screen

23
In-class script 5.3 (ics5_3.m)
  • Add this to the previous script (use x as
    variable)
  • Convert x to integer
  • Then convert x to string (character vector)
  • Then convert x to integer vector
  • Find answer as sum of vector x
  • Print result to screen

ex5_1.m x fix(x) convert
to integer x int2str(x) convert
to string x fix(x) - 48 convert
digits to integers answer (sum(x)) disp(' Sum
of digits is ',int2str(answer))
24
File Input and Output
  • Three things still missing from our programmers
    toolbox
  • File input and output (this week)
  • Error trapping (both weeks)
  • Graphics (next week)

25
Example Problem - Assignment 3
  • Write a script that does the following
  • It opens a file for reading called
    'assignment3.npt'
  • It reads all the real numbers out of the file and
    creates a vector using these numbers
  • It computes several statistics using the numbers
    that have been read in
  • It assigns variables to the calculated statistics
  • It closes the file

26
Example Problem - Assignment 3
  • Write a script that does the following
  • It opens a file for reading called
    'assignment3.npt'
  • It reads all the real numbers out of the file and
    creates a vector using these numbers
  • It computes several statistics using the numbers
    that have been read in
  • It assigns variables to the calculated statistics
  • It closes the file

How do we do items 1., 2, and 5?
27
Example Problem - Assignment 3
  • Write a script that does the following
  • It opens a file for reading called
    'assignment3.npt'
  • It reads all the real numbers out of the file and
    creates a vector using these numbers
  • It computes several statistics using the numbers
    that have been read in
  • It assigns variables to the calculated statistics
  • It closes the file

How do we do items 1., 2, and 5?
28
Example Problem - Assignment 3
  • Write a script that does the following
  • It opens a file for reading called
    'assignment3.npt'
  • It reads all the real numbers out of the file and
    creates a vector using these numbers
  • It computes several statistics using the numbers
    that have been read in
  • It assigns variables to the calculated statistics
  • It closes the file

How do we do items 1., 2, and 5?
29
But first, how about items 3 4 ?
  • It computes several statistics using the numbers
    that have been read in
  • It assigns variables to the calculated statistics

30
But first, how about items 3 4 ?
  • It computes several statistics using the numbers
    that have been read in
  • It assigns variables to the calculated statistics
  • Example data_in 3 5 89 2 3 9 3.2 -12
    111

31
But first, how about items 3 4 ?
  • It computes several statistics using the numbers
    that have been read in
  • It assigns variables to the calculated statistics
  • Example data_in 3 5 89 2 3 9 3.2 -12
    111
  • Vector Functions (S. 7.2, p. 14) ex5_2.m
  • Xmean(data_in) - mean value
  • Xmax (data_in) - maximum value
  • Xstd(data_in) - standard deviation
  • Xsum(data_in) - sum of elements in vector
  • Xprod(data_in) - product of elements

32
Input and Output Functions (p.113-116)
  • Three Steps to File I/O
  • Open file (fopen)
  • Read from or write to file (fread, fwrite, fgetl,
    fgets, fscanf, fprintf, sscanf, sprintf)
  • Close file (fclose)

33
An example of file input (ex5_3.m)
  • ex5_3.m - file input
  • reads a header line and then
  • assignment and grades one each per line
  • fin fopen('input.dat')
  • header fgetl(fin)
  • numbers fscanf(fin,'f f')
  • fclose(fin)
  • disp('finished reading')

Now assign numbers (ex5_4.m)
About PowerShow.com