Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHYS707: Special Topics C++ Lectures Lecture 2. Summary of Today’s lecture: 1.More data types 2.Flow control (if-else, while, do-while, for) 3.Brief introduction.

Similar presentations


Presentation on theme: "PHYS707: Special Topics C++ Lectures Lecture 2. Summary of Today’s lecture: 1.More data types 2.Flow control (if-else, while, do-while, for) 3.Brief introduction."— Presentation transcript:

1 PHYS707: Special Topics C++ Lectures Lecture 2

2 Summary of Today’s lecture: 1.More data types 2.Flow control (if-else, while, do-while, for) 3.Brief introduction to classes: The complex class 4.Functions 1.Built-in 2.Do it yourself

3 More Data Types Boolean (bool): Boolean logic; can have one of 2 values, true or false. Example: bool b1, b2; b1= true; b2=false; cout << “b1 is “ << b1 << “\nb2 is “ << b2 << endl; Output: b1 is 1 b2 is 0 Character (char): a single symbol Example: char c1, c2; c1= ‘C’; c2=“+”; cout << c1 << c2 << c2 << endl; Output: C++

4 Control the Flow! You've already used some flow control. Here are the most important ones: // The “for loop”: for (i = imin; i <= imax; i++) { [stuff goes here] } // this ends the “loop” // the “while loop”: imax = 6; i = 0; while (i < imax) { [stuff goes here] i++; } // the case construct (great for menus) switch(case_number) { case 0: [stuff goes here] break; case 1: [stuff goes here] break; case 3: [stuff goes here] break; default: [stuff goes here] }

5 If…else if construct if(a > b) { [stuff goes here] } else if((c==d)|| (i == j)) { [other stuff goes here] } Some logic operators: >greater than <less than ==is equal to !NOT, eg: !=NOT equal to !>NOT greater than, etc >=greater than or equal to <=less than or equal to ||logical OR &&logical AND

6 Complex Class A “class” is an abstract data type that includes members. Members can include variables (data members) and functions (member functions). You can easily create your own classes, or use classes that others have already created: Example, complex: // program to demonstrate use of classes // use the complex class!! // written 07/23/2012 by B. DePaola #include using namespace std; typedef complex dcmplx; int main() { dcmplx a(5.0,6.0),b,c; const double PI=3.14159; // input value from keyboard: (x,y) cout << "enter b: "; cin >> b; cout << endl; cout << "a= " << a << endl; cout << "b= " << b << endl; cout << "a + b = " << a+b << endl; cout << "a * b = " << a*b << endl; cout << "a/b = " << a/b << endl; cout << "|a| = " << abs(a) << endl; cout << "cmplx conj of a = " << conj(a) << endl; cout << "norm of a = " << norm(a) << endl; cout << "exp(a) = " << exp(a) << endl; cout << "Re(a) = " << a.real() << endl; cout << "Im(a) = " << a.imag() << endl; cout << "a^3 = " << pow(a,3) << endl; cout << "c= " << c << endl; c=polar(3.0,PI/4.0); cout << "3*e^(PI/4) = " << c << endl; cout << "|c| = " << abs(c) << endl; cout << “phase of c = “ << arg(c) << endl; return 0; }

7 Output: a= (5,6) b= (1,2) a + b = (6,8) a * b = (-7,16) a/b = (3.4,-0.8) |a| = 7.81025 cmplx conj of a = (5,-6) norm of a = 61 exp(a) = (142.502,-41.4689) Re(a) = 5 Im(a) = 6 a^3 = (-415,234) c= (0,0) 3*e^(PI/4) = (2.12132,2.12132) |c| = 3 phase of c = 0.785397 Homework: Revisit the quadratic equation problem, BUT now allow for complex roots!

8 Functions Built-in: May have to #include a library (like etc). Otherwise completely straightforward. Examples: sin(x), fabs(x), sqrt(x), etc.

9 User-Defined Functions Example – main program: #include using namespace std; double quadeq(double a, double b, double c); // function declaration // computes degenerate soln of quadratic equation, given the coefficients int main() { double aa, bb, cc; [input coeff part…] cout << quadeq(aa,bb,cc); return 0; }

10 double quadeq(double a, double b, double c) // note: no semicolon!! { double arg_of_sqrt, x; const double eps = 1.0e-6; arg_of_sqrt = b*b – 4.0*a*c; if (fabs(arg_of_sqrt) > eps) { cout 1 solution\n"; return 0; } else { return –b/(2.0*a); }

11 Key Points Arguments passed by position (mess up the order => mess up the calculation!) Can also pass by reference (next lecture!) Functions can return: int floating point string or char boolean nothing at all! (Huh???) Use lots of functions! "Block Structure" Scope & local variables Debug functions separately

12 MORE Homework! 1.Write and implement a function that returns the product of three floating point numbers 2.Write and implement a function that returns the mean of two floating point numbers 3.Write and implement a function that passes three boolean arguments, and returns the logical AND of the third argument with the logical OR of the first two arguments: x(a,b,c) = (a OR b) and c


Download ppt "PHYS707: Special Topics C++ Lectures Lecture 2. Summary of Today’s lecture: 1.More data types 2.Flow control (if-else, while, do-while, for) 3.Brief introduction."

Similar presentations


Ads by Google