Software Engineering. Dr Ian Reid B4, 4 lectures, Hilary Term. http://www.robots.ox.ac.uk/~ian/Teaching/SoftEng. Software Engineering vs structured programming. Not really a course about software engineering… Software engineering Mostly about concepts, Structured programming
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Software Engineering
http://www.robots.ox.ac.uk/~ian/Teaching/SoftEng
Software Engineering vs structured programming
Learning Outcomes
Texts
The Role of Computing in Engineering
some examples…
Example: mobile phone
Example: Sizewell B
Example: A380
Example: NPfIT
Software engineering versus programming
Software vs “other” engineering
Abstraction: freebody diagram
In
Out
+

Modularity: Opamp buffer
Software vs “other” engineering
Intrinsic difficulties with software
When software projects go wrong
When software projects go wrong
NHS National programme for IT: NPfIT
Software lifecycle
Requirements
Validation/Verification
Extreme programming (XP)
Top down design
Modular design
Algorithms
Data structures
Programs
Structured programming
Simple design tools
Controller
state
thrust
Simulator
state
Display
Data flows
Simple design tools
Basic coding techniques
Implementation in Matlab and C
N= 10;
tot = 0;
totsq = 0;
for i=1:N
tot = tot+i;
totsq = totsq+i^2;
end
tot
totsq
int i;
int tot = 0;
int totsq = 0;
for (i=1; i<N; i++) {
tot += i;
totsq += i*i;
}
cout << tot << endl;
cout << totsq << endl;
Notes on coding style
Matlab vs C
Procedural programming
Organisation of Matlab programs
FUNC
foo
Matlab file organisation
FUNC.m
foo.m
bar.m
bar
Organisation of C programs
Source code
Source code
…..
.c .cc
.c .cc
compilation
compilation
Object file
Object file
…..
.o
.o
linking
executable
Functions
Function definition
% compute factorial
function z = fact(n)
% function body
z = 1;
for i=1:n
z = z*i;
end
// compute factorial
int fact(int n)
{
int i, val = 1;
for (i=1; i<=n; i++) {
val *= i;
}
return val;
}
Function call
fact(10)
a = 6;
z = fact(a);
[V,D] = eig(A);
Function prototype
myexp.h file
float myexp(float x);
myexp.c file
float myexp(float x)
{
const float precision = 1.0e6;
float term=1.0, res=0.0;
int i=0;
while (fabs(term)>precision) {
res += term;
i++;
term = pow(x,i)/fact(i);
}
return res;
}
Scope: local variables
Scope: global variables
Encapsulation
Output values
Input parameters
Hidden input
Output values
Input parameters
Hidden output
Sideeffects
Lowlevel implementation of function call
Memory
CODE
machine code
DATA
global variables
local variable m
…
local variable 1
return location
parameter x
…
Activation
record
parameter 1
return value n
STACK
…
return value 1
Pass by value/reference
int i=5, j=10;
swap(i,j);
cout << i << “ “ << j << endl;
Pass by value
Pass by reference
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
return;
}
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
return;
}
class Complex {
public:
double re, im;
};
int i;
Complex z;
Class definition
Create a variable (an instance) of this type
Controller
state
thrust
Simulator
state
Display
class State {
double pos, vel, mass;
bool landed;
};
State s;
State s;
s.pos = 1.0;
s.vel = 20.0;
s.mass = 1000.0;
s.landed = false;
s.pos = s.pos + s.vel*deltat;
Thrust = ComputeThrust(s);
In Matlab introduce structure fields without declaration
s.pos = 1.0;
s.vel = 20.0;
…
Thrust = ComputeThrust(s);
Image ReadImage(const string filename, bool& flag);
bool ReadImage(const string filename, Image& im);
function [Image, errflag] = ReadImage(filename)
int a[10];
res = a[0] + a[1] + a[2];
Complex z[20];
State s[100];
for (t=1; t<100; t++) {
s[t].pos = s[t1].pos + s[t1].vel + 0.5*g;
s[t].vel = s[t1].vel + g – GetThrust(s[t1], burnrate)/s[t1].mass;
s[t].mass = s[t1].mass – burnrate*escapevel;
}
double d[10][5];
has elements:
d[0][0] d[0][1] … d[0][4]
.
.
.
d[9][0] d[9][1] … d[9][4]
class Complex {
public:
double re, im;
double Mag() {
return sqrt(re*re + im*im);
}
double Phase() {
return atan2(im, re);
}
};
Complex z;
cout << “Magnitude=“ << z.Mag() << endl;
{ re = x; im = y; }
class Complex {
public:
Complex(double x, double y) { re=x; im=y; }
double Re() { return re; }
double Im() { return im; }
double Mag() { return sqrt(re*re + im*im);}
double Phase() { return atan2(im, re);
}
private:
double re, im;
};
Complex z(10.0,8.0);
cout << “Magnitude=“ << z.Mag() << endl;
cout << “Real part=“ << z.Re() << endl;
class Complex {
public:
Complex(double x, double y) {
r = sqrt(x*x + y*y);
theta = atan2(y,x);
}
double Re() { return r*cos(theta); }
double Im() { return r*sin(theta); }
double Mag() { return r;}
double Phase() { return theta; }
}
private:
double r, theta;
};
Complex z(10.0,8.0);
cout << “Magnitude=“ << z.Mag() << endl;
cout << “Real part=“ << z.Re() << endl;
Complex.h
class Complex {
public:
Complex(double x, double y);
double Re();
double Im();
double Mag();
double Phase();
private:
double re, im;
};
Complex.cpp
#include “Complex.h”
Complex::Complex(double x, double y) {
re = x; im = y;
}
double Complex::Re() { return re; }
double Complex::Im() { return im; }
double Complex::Mag() {
return sqrt(re*re+im*im);
}
double Complex::Phase() { return atan2(im,re); }
class A : public B {
…
};
class Window
Data: width, height
posx, posy
Methods: raise(), hide()
select(), iconify()
class TextWindow
class GraphicsWindow
Data: cursor_x, cursor_y
Data: background_colour
Methods: redraw(), clear()
backspace(), delete()
Methods: redraw(), clear()
fill()
class InteractiveGraphicsWindow
Data:
Methods: MouseClick(), MouseDrag()
class Window {
…
virtual void redraw();
};
virtual void func() = 0;
then A has no implementation of func()
for (int i=0; i<N; i++) {
obj[i]>Draw();
}
class BoundedArray {
public:
float GetElement(int i) {
if (i<0  i>=10) {
cerr << “Access out of bounds\n”;
return 0.0;
} else {
return a[i];
}
}
private:
float a[10];
};
template <class Type>
class BoundedArray {
public:
Type GetElement(int i) {
if (i<0  i>=10) {
cerr << “Access out of bounds\n”;
return Type(0);
} else {
return a[i];
}
}
private:
Type a[10];
};
BoundedArray<int> x;
BoundedArray<Complex> z;
#include<vector>
int main() {
std::vector<int> v;
for (int i=0; i<20; i++) v.push_back(i);
for (int i=0; i<v.size(); i++)
std::cout << v[i] << std::endl;
}
int size;
std::vector<Complex> z;
std::cin >> size;
z.resize(size);
z[5] = Complex(2.0,3.0);
int width, height;
std::vector< std::vector<int> > x;
x.resisze(height);
for (int i=0; i<height; i++)
x[i].resize(width);
x[2][3] = 10;
…
std::vector<int> v;
std::vector<int>::iterator i;
for (it=v.begin(); it!=v.end(); it++) …
class Cell {
public:
Cell();
bool Visited();
void MarkVisited();
bool BottomWall();
bool RightWall();
void BreakBottom();
void BreakRight();
private:
bool bottomwall;
bool rightwall;
bool visited;
};
class Maze {
public:
Maze(int width, int height);
void Compute(int x, int y);
void Print();
private:
int Rand(int n);
int H, W;
std::vector< std::vector<Cell> > cells;
};
int main(int argc, char* argv[])
{
int width, height;
cerr << "Enter maze width: ";
cin >> width;
cerr << "Enter maze height: ";
cin >> height;
Maze m(width, height);
m.Compute(height1,0);
m.Print();
return 0;
}