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.

Slides:



Advertisements
Similar presentations
Complete Structure class Date {class Date { private :private : // private data and functions// private data and functions public :public : // public data.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Engineering Problem Solving With C++ An Object Based Approach Additional Topics Chapter 10 Programming with Classes.
Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 4 Functions for All Subtasks Slides by David B. Teague, Western Carolina University.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Functions Prototypes, parameter passing, return values, activation frams.
1 Data Structures - CSCI 102 CS102 C++ Operator Overloading Prof Tejada.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Lab 8 User Defined Function.
Lecture Notes 3 Loops (Repetition)
Esempio Polimorfismo1 // Definition of abstract base class Shape #ifndef SHAPE_H #define SHAPE_H class Shape { public: virtual double area() const { return.
1 Objectives Understand streamed input and output.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
Computer Science 1620 Loops.
File I/O. COMP104 Lecture 20 / Slide 2 Using Input/Output Files (Review) * A computer file n is stored secondary storage devices (hard drive, CD) n can.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
1 Lecture 14:User-Definded function I Introduction to Computer Science Spring 2006.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Thursday, December 21, 2006 The Briggs/Chase Law of Program Development: To determine how long it will take to write and debug a program, take your best.
Basic Elements of C++ Chapter 2.
CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Review. Problem 1 What is the output of the following piece of code void increment(int x) { x++; } int main() { int y = 10; increment(y); cout
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
For loop. Exercise 1 Write a program to have the user input three (3) numbers: (f)rom, (t)o, and (i)ncrement. Count from f to t in increments of i, inclusive.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Computer Programming -1-
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Introduction to Programming
Chapter 8: More on the Repetition Structure
#define #include<iostream> using namespace std; #define GO
while Repetition Structure
C++ Programming: CS150 For.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Functions and Procedures
While Loop Design ENGI 1020 Fall 2018.
Starting Out with C++: From Control Structures through Objects
Anatomy of a Function Part 1
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Some Common Issues: Iteration
Unit 3 Review (Calculator)
Anatomy of a Function Part 1
CS31 Discussion 1D Winter19: week 4
Calculate 9 x 81 = x 3 3 x 3 x 3 x 3 3 x 3 x 3 x 3 x 3 x 3 x =
Introduction to Algorithms and Programming COMP151
Presentation transcript:

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

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.

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; }

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); }

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

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.

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; }

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.

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.

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.

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(); }

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.

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; }

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.