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.

Slides:



Advertisements
Similar presentations
The Important Thing About By. The Important Thing About ******** The important thing about ***** is *****. It is true s/he can *****, *****, and *****.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Logic Gates.
PHYS707: Special Topics C++ Lectures Lecture 3. Summary of Today’s lecture: 1.Functions (call-by-referencing) 2.Arrays 3.Pointers 4.More Arrays! 5.More.
1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc.
The "if structure" is used to execute statement(s) only if the given condition is satisfied.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Problem: Given an array of positive integers, find a pair of integers in the array that adds up to 10 (or indicate none exist). Your code should be as.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Section 4.2: Functions that Test Conditions (continued)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
Information Technology Systems EN230-1 Justin Champion C208 –
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
1 CSE 20: Lecture 7 Boolean Algebra CK Cheng 4/21/2011.
XOR and XNOR Logic Gates. XOR Function Output Y is TRUE if input A OR input B are TRUE Exclusively, else it is FALSE. Logic Symbol  Description  Truth.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Lecture 7 Topics –Boolean Algebra 1. Logic and Bits Operation Computers represent information by bit A bit has two possible values, namely zero and one.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
Conditions, Logical Expressions, and Selection Control Structures ROBERT REAVES.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
CEC 220 Digital Circuit Design Boolean Algebra I Wed, Sept 2 CEC 220 Digital Circuit Design Slide 1 of 13.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
A: A: double “4” A: “34” 4.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Conditional statements and boolean expressions Arithmetic, relational and logical operators.
CPS120 Introduction to Computer Science
Section 3 Review Mr. Crone.
Topic: Conditional Statements – Part 1
Sequence, Selection, Iteration The IF Statement
Bool operators and, or, not
Topic: Conditional Statements – Part 2
A mechanism for deciding whether an action should be taken
Logic Gates.
Logic Gates Benchmark Companies Inc PO Box Aurora CO
Topics The if Statement The if-else Statement Comparing Strings
4-1 LOGIC OPERATIONS In Chapter 3 we discussed the fact that data inside a computer is stored as patterns of bits. Logic operations refer to those operations.
JavaScript conditional
Topics The if Statement The if-else Statement Comparing Strings
Conditional Construct
Control Structures: Selection Statement
Chapter 4: Decision Structures and Boolean Logic
Logic Gates.
Repetition Control Structure
Chapter 8: More on the Repetition Structure
Flow Control Statements
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Decision I (if Statement)
Formal Methods in Software Engineering
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Boolean Variables & Values
Understanding Conditions
JavaScript conditional
DeMorgan's & related Laws
Nate Brunelle Today: Conditional Decision Statements
ECS15 boolean.
Decisions, decisions, decisions
Control Structures: Selection Statement
ECE Digital Electronics
Chapter 4: Decision Structures and Boolean Logic
Life is Full of Alternatives Part 3
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Conditionals.
Presentation transcript:

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 to classes: The complex class 4.Functions 1.Built-in 2.Do it yourself

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++

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

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

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

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

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

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

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

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

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