Download presentation
Presentation is loading. Please wait.
Published byGeorgia Blackner Modified over 9 years ago
1
Write a function to calculate the cubic function: y = 4x 3 + 2x 2 –5x – 4 The function should return y for any given value of x. Question One #include double cubic(double x){ double y; y = 4*pow(x,3)+10*pow(x,2)-5*x-4; return y; } … or … double cubic(double x){ return (4*x*x*x + 10*x*x - 5*x - 4); } When we say return we mean return Unless a method is specifically for writing to screen (e.g. dump(), Print()…) only use std::cout inside a method for debugging
2
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 < x < 2.5. Question One (cont) void main(){ for (double x = -2.5; x <= 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 <= 2.5 ; x += 0.05){ double y = cubic(x); if (y < ymin) { ymin = y; xmin = x; } return; } #include void main(){ double xmin = -2.5; double ymin = cubic(xmin); for (double x = -2.5; x <= 2.5 ; x += 0.05){ double y = cubic(x); if (y < ymin) { ymin = y; xmin = x; } std::cout << “The minimum value of y is ” << ymin << “and occurs at x = ” << xmin << std::endl; return; } 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 < x < 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.
3
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; Question Two 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 void main(){ int int1=1; int int2=1; while(){ std::cout << “Please enter two integers” << std::endl; std::cin >> int1 >> int2; } return; } #include void main(){ int int1=1; int int2=1; ofstream output(“myfile.txt”); while(){ std::cout << “Please enter two integers” << std::endl; std::cin >> int1 >> int2; output << int1 << “ “ << int2 << std::endl; } return; } #include void main(){ int int1=1; int int2=1; ofstream output(“myfile.txt”); while(int1!=0 || int2!=0){ std::cout << “Please enter two integers (0,0) exits” << std::endl; std::cin >> int1 >> int2; if (int1==0 && int2==0) break; output << int1 << “ “ << int2 << std::endl; } return; }
4
Question Three Class BroomStick{ private: public: } 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); }
5
Question 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 float s or double s but not really int s
6
Question Three - constructors BroomStick::BroomStick() { x = y = z = vx = vy = 0.0; } BroomStick::BroomStick(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.
7
Question Three - set methods void BroomStick::SetVelocity(double set_vx, double set_vy){ vx = set_vx; vy = set_vy; } void BroomStick::SetHeight(double height){ z = height; } A method that sets a value shouldn’t 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 BroomStick::AddHeight(double height){ z += height; return z; }
8
Question Three - return methods double BroomStick::Height() { return z; } double BroomStick::XPosition(double time) { return (x + vx*time); } XPosition() should not really modify the member variables, otherwise the position returned will not be relative to the original position.
9
Question 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.
10
Question Four - track class 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(); } The Track design should be simple and embody the concept of direction and velocity.
11
Question 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(); }
12
Question Four - jet class #include class Jet { private: std::vector tracks; ThreeVector pos; ThreeVector vel; public: Jet(); Jet(Track track); void AddTrack(Track track); void AddTracks(std::vector tracksin); int NumTracks() void ZeroTracks(); Track GetTrack(int i); ThreeVector Position(); ThreeVector Velocity(); } The Jet design should allow it to contain the Tracks which belong to it, and have suitable member variables and methods for this purpose.
13
Question Four - jet class methods int Jet::NumTracks() { return tracks.size(); } void Jet::AddTrack(Track track) { tracks.pushback(track); } Track Jet::GetTrack(int i) { if ( i >= 0 && i NumTracks()){ return tracks[i]; } else { Track empty; return empty; }
14
Question Four - demonstration void main(){ Jet Jet1, Jet2; for (int i = 0 ; i < 100 ; i++){ Track temp(rand(), rand(), rand(), rand(), rand(), rand()); Jet1.Add(temp); } for (int j = 0 ; j < 20 ; j++){ Track temp(rand(), rand(), rand(), rand(), rand(), rand()); Jet2.Add(temp); } std::cout << “Jet1 has ” << Jet1.NumTracks() << “ tracks” << std::endl; Track test = Jet2.GetTrack(4); std::cout << “Track random has position (” << test.Position().X() << “,” << test.Position().Y() << “,” << test.Position().Z() << “)” << std::endl; return; } 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.