Vous êtes sur la page 1sur 33

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


Online Quiz One is This Week
First in a series of three online quizzes.
When Mon (24 Aug starting at 12pm) till
Sun (30 Aug ending at 11.59pm).
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.
EP100 Clinics
When
Wed, 9am 1pm
Fri, 1pm 5pm
Where
204:305
Last Weeks Lecture
Equate (i.e. ==) boolean operation
/* File: realequate.c
** Description: Test equate operation on double data types
** Programmer: Tele Tan
** Date: 15th August 2009
*/

#include <stdio.h>

int main(void)
{
double a, b;
int z;
a = 0.45;
b = 0.2+0.05+0.05+0.05+0.05+0.05;

z = a==b; // Boolean operation; FALSE=0; TRUE=1;


printf("value of z is %d\n",z);

//printf("a = %.20lf\n",a);
//printf("b = %.20lf\n",b);

return 0;
}
code4_1.c
Engineering Programming 100
Lecture 4: Design

Dr Tele Tan
Outcomes
After this lecture you should be able to ...
Understand need to specify program behaviour
Map behaviour to a program
Identifying variables needed
Identify steps needed
Understand the need to check behaviour, debug and test
Problem solving
State the program behaviour:
State what you want the program to do as precisely as possible
Traditional computer dislikes ambiguity
Identify the required variables, data, information
(should you use char, int, float or others.)
Identify the operations needed
Construct the algorithm:
Use pseudo code
Implement method as a C program
Assessment:
Test implementation and assess results
Program development environment
A set of programs to enable you to:
Create a place to keep your program (make a directory)
mkdir (in Linux)
Create and modify the program (edit)
vi, gvim, gedit etc. (in Linux)
Run the compiler (preprocessor, compile, link)
gcc, g++ (in Linux)
Execute the program (load, execute)
a.out etc. (in Linux)

Edit, compile, test, debug cycle


Case study - speeder.c
Sarah OSprinter is a member of the local
track and field club. Although she runs the
100 metres, 200 metres and 400 metres
races, her best event is the 100 metres race,
for which her best time is 11.2 seconds. To
impress her friends, Sarah would like to be
able to tell them how fast she runs. She
wants a program that will compute her
average speed in kilometres per hour.

From: Adams, Nyhoff and Nyhoff, Java, An introduction to


Computing
Case study - speeder.c
Sarah OSprinter is a member of the local
track and field club. Although she runs the
100 metres, 200 metres and 400 metres
races, her best event is the 100 metres race,
for which her best time is 11.2 seconds. To
impress her friends, Sarah would like to be
able to tell them how fast she runs. She
wants a program that will compute her
average speed in kilometres per hour.

From: Adams, Nyhoff and Nyhoff, Java, An introduction to


Computing
Restate the problem
Given a distance of 100 metres travelled in a
time of 11.2 seconds, compute the average
speed in kilometres per hour.
Problem definition
Must define and understand the problem
requiring a solution
Start with a top level general English
description
State what type of data is required to solve
the problem
State what results will be calculated
State the actions needed to be taken to
generate the results
Program behaviour
The program should display on the screen a
prompt for a distance in metres. The user
should enter this distance at the keyboard,
from which the program should read it. The
program should then display on the screen a
prompt for a time in seconds. The user
should enter this time at the keyboard, from
which the program should read it. The
program should then compute the speed in
kilometers per hour and display it on the
screen.
Program behaviour
The program should display on the screen a
prompt for a distance in metres. The user
should enter this distance at the keyboard,
from which the program should read it. The
program should then display on the screen a
prompt for a time in seconds. The user
should enter this time at the keyboard, from
which the program should read it. The
program should then compute the speed in
kilometers per hour and display it on the
screen.
Stages of the algorithm
1. Prompt for the distance in metres
2. Read distance
3. Prompt for the time in seconds
4. Read time
5. Compute speed in kilometres per hour
6. Display speed
Computation
Average speed is given by:
speed = distance / time
metres_per_second = metres / seconds
However need result in kilometres_per_hour
Know
1000 metres in a kilometre
kilometres_per_second = metres_per_second / 1000
3600 seconds in an hour
kilometres_per_hour = kilometres_per_second * 3600
Or
1000 metres in a kilometre
kilometres = metres / 1000
3600 seconds in an hour
hours = seconds / 3600
Revised stages of the algorithm
1. Prompt for the distance in metres
2. Read distance
3. Prompt for the time in seconds
4. Read time
5. Convert distance to kilometres
6. Convert time to hours
7. Compute speed in kilometres per hour
8. Display speed
Revised variable names
1. Prompt for the distance in metres (metres)
2. Read distance
3. Prompt for the time in seconds (seconds)
4. Read time
5. Convert distance to kilometres (kms)
6. Convert time to hours (hours)
7. Compute speed in kilometres per hour
(kms_per_hour)
8. Display speed
Possible pseudo code version
start

output To compute the speed for a distance travelled


output newline

output Enter distance in metres


input metres
output Enter time in seconds
input seconds

kms <- metres / 1000.0


hours <- seconds / 3600.0
kms_per_hour < kms /hours

output Average speed in kilometres per hour:


output kms_per_hour

end
Code writing
Create a program stub:
Opening documentation
Empty main
Compile program stub
Check expected output
Add a step of the algorithm to the stub,
recompile and check
Code writing - first program
/* File: Speeder.c
** Description: Computes the speed given the distance and time
** Programmer: Tele Tan
** Date: 15th August 2009
*/

#include <stdio.h>

int main(void)
{
return 0;
}

code4_2_1.c
Code writing - add variables
/* File: Speeder.c
** Description: Computes the speed given the distance and time
** Programmer: Tele Tan
** Date: 15th August 2009
*/

#include <stdio.h>

int main(void)
{
float metres;
float seconds;
float kms;
float hours;
float kms_per_hour;

return 0;
}

code4_2_2.c
Code writing - add program description
/* File: Speeder.c
** Description: Computes the speed given the distance and time
** Programmer: Tele Tan
** Date: 15th August 2009
*/

#include <stdio.h>

int main(void)
{
float metres;
float seconds;
float kms;
float hours;
float kms_per_hour;

printf(To compute the speed for a distance travelled\n);

return 0;
}

code4_2_3.c
Code writing - add prompts for input and
read
/* File: Speeder.c
** Description: Computes the speed given the distance and time
** Programmer: Tele Tan
** Date: 15th August 2009
*/

#include <stdio.h>

int main(void)
{
float metres;
float seconds;
float kms;
float hours;
float kms_per_hour;

printf(To compute the speed for a distance travelled\n);


printf(Enter distance in metres);
scanf(%f, &metres);
printf(Enter time in seconds);
scanf(%f, &seconds);

return 0;
}

code4_2_4.c
Code writing - add computation
/* File: Speeder.c
** Description: Computes the speed given the distance and time
** Programmer: Tele Tan
** Date: 15th August 2009
*/

#include <stdio.h>

int main(void)
{
float metres;
float seconds;
float kms;
float hours;
float kms_per_hour;

printf(To compute the speed for a distance travelled\n);


printf(Enter distance in metres);
scanf(%f, &metres);
printf(Enter time in seconds);
scanf(%f, &seconds);

kms = metres / 1000.0;


hours = seconds / 3600.0;
kms_per_hour = kms /hours;

return 0;
}

code4_2_5.c
Code writing - add output
/* File: Speeder.c
** Description: Computes the speed given the distance and time
** Programmer: Tele Tan
** Date: 15th August 2009
*/

#include <stdio.h>

int main(void)
{
float metres;
float seconds;
float kms;
float hours;
float kms_per_hour;

printf(To compute the speed for a distance travelled\n);


printf(Enter distance in metres);
scanf(%f, &metres);
printf(Enter time in seconds);
scanf(%f, &seconds);

kms = metres / 1000.0;


hours = seconds / 3600.0;
kms_per_hour = kms /hours;

printf(Average speed in kilometres per hour: %f\n, kms_per_hour);

return 0;
}

code4_2_6.c
Algorithm Design
Algorithm design is an art that takes LOTS of practice
Made up of actions and their order
Actions are steps to be performed
Order is the sequence in which the steps must be
performed in order to achieve the required results
Complex problems will need several stages of
refinement
Desk-check for correctness
Description independent of any programming
language
Order is important
Consider the following morning ritual of
getting to your first lecture:
1. Get out of bed
2. Take off pajamas
3. Take a shower
4. Get dressed
5. Eat breakfast
6. Lock up flat/house
7. Car/bus/bike/walk to Curtin
8. Attend lecture

Consider 1,2,3,5,6,7,4,8
Implementation
Problem
and Testing

Code

Compile
SYNTAX
ERROR!

Computer
program
Implementation
Problem
and Testing

Code

Compile

Test LOGIC
ERROR!

Computer
program
Implementation and Testing
Errors can and will occur
Grammar/syntax (syntax error)
Algorithmic (logic error)
Process of finding errors is called debugging
Syntax errors detected by the compiler
Runtime/logic errors detected by testing
Compare results against hand worked examples
Use a variety of inputs
Desk check the algorithm
Today we discussed
Program design methods
Implementing the design
Used a case study - speeder.c
Whats next?
Practical
Design and writing of some simple programs
Lecture:
Control Flow

Vous aimerez peut-être aussi