Professional Documents
Culture Documents
WARNING
#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;
//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)
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;
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;
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;
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;
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