Title: Question One
1Question One
Write a function to calculate the cubic
function y 4x3 2x2 5x 4 The function
should return y for any given value of x.
include ltcmathgt double cubic(double x)
double y y 4pow(x,3)10pow(x,2)-5x-4
return y
or double cubic(double x) return
(4xxx 10xx - 5x - 4)
- When we say return we mean return
- Unless a method is specifically for writing to
screen (e.g. dump(), Print()) only use stdcout
inside a method for debugging
2Question One (cont)
Write a main program which repeatedly calls this
function in a loop where x is incremented in
steps of 0.05 over the range 2.5 lt x lt 2.5.
Determine the minimum value of y returned within
the range and print it, and the value of x at
which it occurs, to the screen.
Write a main program which repeatedly calls this
function in a loop where x is incremented in
steps of 0.05 over the range 2.5 lt x lt 2.5.
void main() for (double x -2.5 x lt 2.5
x 0.05) double y cubic(x)
return
void main() double xmin -2.5 double
ymin cubic(xmin) for (double x -2.5 x lt
2.5 x 0.05) double y cubic(x)
if (y lt ymin) ymin y xmin
x return
include ltiostreamgt void main() double xmin
-2.5 double ymin cubic(xmin) for
(double x -2.5 x lt 2.5 x 0.05)
double y cubic(x) if (y lt ymin)
ymin y xmin x
stdcout ltlt The minimum value of y is ltlt
ymin ltlt and occurs at x ltlt xmin
ltlt stdendl return
3Question Two
- Write a piece of code which performs the
following functions - requests the user to supply a pair of integer
numbers from the keyboard within a loop
- Write a piece of code which performs the
following functions - writes each pair of numbers to an output file on
a single line (i.e. one line in the file per pair
of numbers)
- Write a piece of code which performs the
following functions - continues in the request loop until the user
enters the values 0 0 at which point the loop
should be broken and the program should
terminate.
include ltiostreamgt void main() int int11
int int21 while() stdcout ltlt
Please enter two integers ltlt
stdendl stdcin gtgt int1 gtgt int2
return
include ltiostreamgt include ltfstreamgt void
main() int int11 int int21 ofstream
output(myfile.txt) while() stdcout
ltlt Please enter two integers ltlt
stdendl stdcin gtgt int1 gtgt int2
output ltlt int1 ltlt ltlt int2 ltlt
stdendl return
include ltiostreamgt include ltfstreamgt void
main() int int11 int int21 ofstream
output(myfile.txt) while(int1!0
int2!0) stdcout ltlt Please enter two
integers (0,0) exits ltlt
stdendl stdcin gtgt int1 gtgt int2
if (int10 int20) break output ltlt
int1 ltlt ltlt int2 ltlt stdendl
return
4Question Three
Write a class called BroomStick.
- Write a class called BroomStick. The class should
include - Suitable member variables to represent position
and velocity
- Write a class called BroomStick. The class should
include - An appropriate way of initialising the
broomstick position and velocity upon creation
- Write a class called BroomStick. The class should
include - Methods to set the height and velocity in the
horizontal plane of the broomstick
- Write a class called BroomStick. The class should
include - Methods to return the current height and x
coordinate at time t of the broomstick
Class BroomStick private double x, y, z
double vx, vy public
Class BroomStick private double x, y, z
double vx, vy public BroomStick()
BroomStick(double x0, double y0, double z0,
double vx0, double vy0)
Class BroomStick private double x, y, z
double vx, vy public BroomStick()
BroomStick(double x0, double y0, double z0,
double vx0, double vy0) void
SetVelocity(double set_vx, double set_vy)
void SetHeight(double height)
Class BroomStick private double x, y, z
double vx, vy public BroomStick()
BroomStick(double x0, double y0, double z0,
double vx0, double vy0) void
SetVelocity(double set_vx, double set_vy)
void SetHeight(double height) double
Height() double XPosition(double time)
Class BroomStick private public
5Question Three (cont)
Class BroomStick private double x, y, z
double vx, vy public BroomStick()
BroomStick(double x0, double y0, double z0,
double vx0, double vy0) void
SetVelocity(double set_vx, double set_vy)
void SetHeight(double height) double
Height() double XPosition(double time)
- Data members should always be private, if you
want access to them provide a method to do this - Positions velocities can be floats or doubles
but not really ints
6Question Three - constructors
BroomStickBroomStick() x y z vx vy
0.0 BroomStickBroomStick(double x0,
double y0, double z0, double vx0, double vy0)
SetPosition(x0, y0) SetHeight(z0)
SetVelocity(vx0, vy0)
- You could set the member variables directly of
course, but since we have methods that do this
for us it is best to use these.
7Question Three - set methods
void BroomStickSetVelocity(double set_vx,
double set_vy) vx set_vx vy
set_vy void BroomStickSetHeight(double
height) z height
- A method that sets a value shouldnt assume that
the user knows (or cares) what the current value
is. - A method to change the height by a certain value
could be useful but is not what was asked for
double BroomStickAddHeight(double height) z
height return z
8Question Three - return methods
double BroomStickHeight() return
z double BroomStickXPosition(double
time) return (x vxtime)
- XPosition() should not really modify the member
variables, otherwise the position returned will
not be relative to the original position.
9Question Four
Write classes to represent tracks and
jets. - tracks these are single high
energy particles travelling out from the centre
of the detector and are shown in yellow
- jets collections of tracks which
clearly come from the same point. There are three
distinct jets in this event
Physicists analyse these events using object
oriented code.. In a real analysis we first find
all of the tracks, then we run algorithms to find
those tracks which are close together because
they come from a Jet. Assume that this step has
been done, i.e. we now know all of the tracks
which belong to each Jet.
10Question Four - track class
The Track design should be simple and embody the
concept of direction and velocity.
class Track private double x, y, z
double vx, vy, vz public Track()
Track(double x0, double x0, double x0,
double vx0, double vy0, double vz0) double
X() double Y() double Z() double
XVelocity() double YVelocity() double
ZVelocity()
11Question Four - track class (Mk II)
class Track private ThreeVector pos
ThreeVector vel public Track()
Track(ThreeVector pos0, ThreeVector vel0)
Track(double x0, double x0, double x0,
double vx0, double vy0, double vz0)
ThreeVector Position() ThreeVector
Velcoity()
12Question Four - jet class
The Jet design should allow it to contain the
Tracks which belong to it, and have suitable
member variables and methods for this purpose.
include ltvectorgt class Jet private
stdvectorltTrackgt tracks ThreeVector pos
ThreeVector vel public Jet() Jet(Track
track) void AddTrack(Track track) void
AddTracks(stdvectorltTrackgt tracksin) int
NumTracks() void ZeroTracks() Track
GetTrack(int i) ThreeVector Position()
ThreeVector Velocity()
13Question Four - jet class methods
int JetNumTracks() return
tracks.size() void JetAddTrack(Track
track) tracks.pushback(track) Track
JetGetTrack(int i) if ( i gt 0 i lt
this-gtNumTracks()) return tracksi
else Track empty return empty
14Question Four - demonstration
Demonstrate the creation of Tracks and empty
Jets, and then show how you would call methods
of Jet objects to add Track objects into it.
void main() Jet Jet1, Jet2 for (int i 0
i lt 100 i) Track temp(rand(),
rand(), rand(), rand(), rand(), rand())
Jet1.Add(temp) for (int j 0 j lt 20
j) Track temp(rand(), rand(), rand(),
rand(), rand(), rand()) Jet2.Add(temp)
stdcout ltlt Jet1 has ltlt Jet1.NumTracks()
ltlt tracks ltlt stdendl
Track test Jet2.GetTrack(4) stdcout ltlt
Track random has position ( ltlt
test.Position().X() ltlt , ltlt
test.Position().Y() ltlt , ltlt
test.Position().Z() ltlt ) ltlt stdendl
return