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

The Adobe Flash plugin is needed to view this content

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

Review of Last Class

- To get input from screen use the command?

Review of Last Class

- To get input from screen use the command?
- input x input(Enter x )

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?

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))

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?

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

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?

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

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 ?

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

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

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

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

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

Now Add a Feature to Program

- Sum digits of number input from screen
- For simplicity, input only integers

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

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

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

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 )

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)

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

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))

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)

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

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?

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?

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?

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

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

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

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)

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)