commonwealth of australia copyright regulation 1969

29
COMMONWEALTH OF AUSTRALIA Copyright Regulation 1969 WARNING This material has been copied and communicated to you by or on behalf of Curtin University of Technology pursuant to Part VB of the Copyright Act 1968 (the Act) The material in this communication may be subject to copyright under the Act. Any further copying or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice

Upload: independent

Post on 04-Feb-2023

0 views

Category:

Documents


0 download

TRANSCRIPT

COMMONWEALTH OF AUSTRALIA

Copyright Regulation 1969

WARNING

This material has been copied and communicated to you by or on behalf of Curtin University of

Technology pursuant to Part VB of the Copyright Act 1968 (the Act)

The material in this communication may be subject to copyright under the Act. Any further copying or communication of this material by you may be the

subject of copyright protection under the Act.

Do not remove this notice

Quiz Two

• When – Mon (23 Sep starting at 12noon) till Sun (29 Sep ending at 11.55pm).

• You have up to 5 attempts• Remember to press “submit” after you have

completed each quiz attempt. Pressing “save” WILL NOT register your quiz mark.

Mid Semester Test

• When – Next Mon (23 Sep) at 11am• Three main questions (40 mins)

Q1 : Fundamentals (14 marks)Q2 : Control (13 marks)Q3 : Repetition (13 marks)

• Past year test papers are available in Blackboard

Engineering Programming 100Lecture 6: Repetition

Dr Tele Tan

This week

• Explore the various methods to repeat sections of code– For loops– Do while loops– While do loops

• Explore the methods using an engineering/mathematical example

Last week example: what if enter a negative time?

• Need an if statement to control whether to compute distanceif (time <= 0.0)

{distance = 0.0;printf("warning, time <= 0.0\n");

}else{

conversion = 3.141592 / 180.0;theta_2 = conversion * theta;distance = 0.5 * time * time * 9.81 * sin (theta_2);

}

• Problem? Need to re-run program to enter correct value

Simple program

• List of statements executed in order:int main(void) 1

{

int x; 2

int y; 3

int z; 4

x = 5; 5

y = 7; 6

z = x + y; 7

printf("z: %d\n",z); 8

} exit

Repeating statements

• Choose how many times to execute a block of code - called iteration

• Examples (from last lecture):– Compute distance a block has slid for a number of values of

friction coefficients and plot graph of results– Convert student marks to grades for a number of students

• Types of looping:– for loop– do while (also known as repeat until) loop– while do loop

Last week example – solution (1)

• Use a while loop to make sure time > 0.0time = -1.0; // force into loop by making -vewhile (time <= 0.0){

printf("enter a time: ");scanf("%f", &time);if(time <= 0.0)

printf("error, time must be +ve and greater than zero\n");}conversion = 3.141592 / 180.0;theta_2 = conversion * theta;distance = 0.5 * time * time * 9.81 * sin (theta_2);

• Use of relation (i.e. time <= 0.0) and logical (i.e. &&, || or !) operators as in last week

• The loop exits only if time is > 0.0

while_example.c

Last week example – solution (2)

• Use a do loop to make sure time > 0.0

do{

printf("enter a time: ");scanf("%f", &time);if(time <= 0.0)

printf("error, time must be +ve and greater than zero\n");} while (time <= 0.0);conversion = 3.141592 / 180.0;theta_2 = conversion * theta;distance = 0.5 * time * time * 9.81 * sin (theta_2);

• The loop exits only if time is > 0.0

do_example.c

Example - flow of liquid in a pipe

• An engineer is responsible for recommending a certain size of pipe for a new plant. The internal radius of the pipe can vary between 10 and 50 cm in steps of 5 cm. The size of the pipe required is dependent on the volume of liquid flowing out of the pipe in m3 per second.

Flow of liquid

• We need to design a program that prints out a table of values showing the radius of the pipe and the flow volume:-----------------------------------

Volume of liquid flowing from pipes

Flow is constant at 2.0m/s

-----------------------------------

Radius in cm Volume in cubic m/s

10.0 0.063

15.0 0.141

20.0 0.251

25.0 0.393

30.0 0.565

35.0 0.770

40.0 1.005

45.0 1.272

50.0 1.571

-----------------------------------

• If need a volume rate of 0.5m3/sec then choose radius = 30.0cm.

Using a “while do” loopint i = 0;

float radius, volume_flow;

float initial_radius;

float increase_radius;

float flow;

float conversion = 0.01;

initial_radius = 10.0;

increase_radius = 5.0;

flow = 2.0;

while ( i < 9 )

{

radius = initial_radius + increase_radius * i;

volume_flow = PI * radius * conversion * radius * conversion * flow;

printf("%.1f %.3f\n", radius, volume_flow);

i = i + 1;

}

• Executes for i = 0, 1, 2, 3, 4, 5, 6, 7, 8 (9 iterations)

Pseudo code:

while condition truebeginexecute commandsend

Code6_1.c

“while do” flowchart

testcondition

statement

true

false

Using “do while” loopint i = 0;

float radius, volume_flow;

float initial_radius;

float increase_radius;

float flow;

float conversion = 0.01;

initial_radius = 10.0;

increase_radius = 5.0;

flow = 2.0;

do

{

radius = initial_radius + increase_radius * i;

volume_flow = PI * radius * conversion * radius * conversion * flow;

printf("%.1f %.3f\n", radius, volume_flow);

i = i + 1;

} while ( i < 9 );

• Executes for i = 0, 1, 2, 3, 4, 5, 6, 7, 8 (9 iterations)

Pseudo code:

dobeginexecute commandsend

while condition true

Code6_2.c

“do while” flowchart

testcondition

statement

true

false

Using a “for” loopint i = 0;float radius, volume_flow;float initial_radius;float increase_radius;float flow;float conversion = 0.01;

initial_radius = 10.0;increase_radius = 5.0;flow = 2.0;

for ( i=0; i<9; i++){

radius = initial_radius + increase_radius * i;volume_flow = PI * radius * conversion * radius * conversion *

flow;printf("%.1f %.3f\n", radius, volume_flow);

}

• Executes for i = 0, 1, 2, 3, 4, 5, 6, 7, 8 (9 iterations)

Code6_3.c

The “for” loop

• Pseudo code:Do for i from start to finish, incrementing by inc

beginstatements

end

• Variables: start = 3, finish = 8, inc = 1Values of i:• 3, 4, 5, 6, 7, 8

• Variables: start = 3, finish = 12, inc = 3Values of i:• 3, 6, 9, 12

• Variables: start = 12, finish =-6, inc = -3Values of i:• 12, 9, 6, 3, 0, -3, -6

for ( i = 3; i <= 8; i++)

for ( i = 3; i <= 12; i+=3)

for ( i = 12; i >= -6; i-=3)

The “for” loop

• Variables: start = 3, finish = 7, inc = 1Values of i:• 3, 4, 5, 6, 7

• Variables: start = 3, finish = 9, inc = 3Values of i:• 3, 6, 9

• Variables: start = 12, finish =-3, inc = -3Values of i:• 12, 9, 6, 3, 0, -3

The “for” loop

• Variables: start = 3, finish = 7, inc = 1• Values of i:

• 3, 4, 5, 6, 7

• Variables: start = 3, finish = 9, inc = 3• Values of i:

• 3, 6, 9

• Variables: start = 12, finish =-3, inc = -3• Values of i:

• 12, 9, 6, 3, 0, -3

for ( i = 3; i < 8; i++) orfor ( i = 3; i <= 7; i++)

for ( i = 3; i < 12; i+=3) orfor ( i = 3; i <= 9; i=i+3)

for ( i = 12; i > -6; i-=3) orfor ( i = 12; i >= -3; i=i-3)

Using a “for” loop - alternativefloat radius, volume_flow;

float initial_radius;

float increase_radius;

float conversion = 0.01;

initial_radius = 10.0;

increase_radius = 5.0;

for (radius=initial_radius; radius<=50.0; radius+=increase_radius)

{

volume_flow = PI * radius * conversion * radius * conversion * flow;

printf("%.1f %.3f\n", radius, volume_flow);

}

• Executes for radius = 10, 15, 20, 25, 30, 35, 40, 45, 50 (9 iterations)

Which looping method to use?

• Common features:– Loop variable– Initialisation– Termination test– Updating expression

• Counting (for) or general (while or do while)• If a fixed number of iterations,

– Use a for loop: for(...; ...; ...)– Don’t change the loop variable in the loop!

• Execute the loop at least once:– Use a do while loop: do{...}(while...)– Post tests the loop variable

• Allow no execution of the loop:– Use a while do loop: – Pre tests the loop variable

The Bisection method• Find the root of an equation given the

function

• E.g.: Find x given F(x) = x2-5 = 0

• Iterate through values of x to find solution

• Bisection method:1 Choose a=a1 and b=b12 Find midpoint: (a + b)/23 If F(a) * F(midpoint) +ve4 make a = midpoint5 else6 make b = midpoint7 Repeat from 1 until a=b

From en.wikipedia.org/wiki/Bisection_method

Pseudo code versionwhile fabs(x_right - x_left) > epsilon

x_midpoint = (x_right + x_left) / 2if ( ( f(x_left) * f(x_midpoint) ) > 0) then

x_left = x_midpointelse

x_right = x_midpointend if

end while

• epsilon stops the iteration• epsilon - Why not just: (x_left - x_right) == 0 ?

– Rounding errors - may never equal 0

• Computer representation of real numbers is an approximation

fabs() – absolute value function

C version (not complete)double x_midpoint, x_left, x_right;double f_midpoint, f_left, f_right;double epsilon;

x_left = 0.0;x_right = 100.0;epsilon = 0.0000000001;

while (fabs(x_right - x_left) > epsilon) /* while (x_right - x_left) > epsilon */

{x_midpoint = (x_right + x_left) / 2.0; /* x_midpoint = (x_right+x_left) / 2 */f_left = x_left * x_left - 5.0; /* Calculate f(x_left) */

f_midpoint = x_midpoint * x_midpoint - 5.0; /* Calculate f(x_midpoint) */if ((f_left * f_midpoint) > 0) /* if ((f(x_left)*f(x_midpoint)) > 0) then */

{ x_left = x_midpoint;

} else /* else ((f(x_left)*f(x_midpoint)) <= 0)*/

{x_right = x_midpoint;

} }

Code6_4.c

SolutionSqrt(5) = 2.2360679774

Example of approximations/* simple program demonstrating rounding errors */

#include <stdio.h>

int main(void)

{

float f = 0.37;

float g = 2.01 - 2.0;

printf("%.10f\n", f);

printf("%.10f\n", g);

printf("%.16f\n", (2.01 - 2.0) );

return 0;

}

• Outputs (note can change depending on the computer architecture and compiler:

0.3700000048

0.0099999998

0.0099999999999998

Solutions to approximation errors

• Comparing whole numbers:– convert type

if ( (int) amount == 0 ) /* e.g. amount is float */

• removes decimal part • e.g. 2.4 => 2• e.g. 13.6 => 13

– Use the round function in the maths libraryif ( (int) round(amount) == 0)

• rounds to nearest integer• 2.4 => 2• 2.6 => 3• 2.5 => 3

• Use a tolerance (precision) - must be valid for the application

• Two or three decimal places may be okay for money but not for submarine propeller blades!

Today we discussed…

• Looping in a program• Explored different ways to do it• Constructed code using some of the methods

Mid Semester Test

• When – Next Mon (23 Sep) at 11am• Three main questions (40 mins)

Q1 : Fundamentals (14 marks)Q2 : Control (13 marks)Q3 : Repetition (13 marks)

• Past year test papers are available in Blackboard