Unformatted text preview: Engineering 101 Engineering 101
Lecture 4 9/13/07
Input / Output Quote of the Day Quote of the Day
It does not matter how slowly you go, so long as you do not stop.
Confucius Some thoughts on Project 1 Some thoughts on Project 1 Run lots of trials using the sample program Use a variety of values for each input variable. Some results may appear correct, but the code is not. The efficiency input will be a percentage between 1 and 100 You must convert it into a ratio Remember to only output a number with no other text Remember where to submit it correctly and also the proper name to call it C++ Expressions C++ Expressions Expressions consist of operators, literals and identifiers. Literals are direct representations of data such as 2.4, 5 or 1.0e5 (which is the way a computer writes 1.0×105). Identifiers are names given to data objects in declarations. In the previous program x and y were identifiers. Operators transform data. These include +(add), * (multiply), =(assign), plus many others. Operators Operators Operators are the most interesting of the three since they transform data.
Lite r als operator
s ifier t den I new data Operators that operate on one operand (data object) are unary, those that operate on two operands are called binary. Mathematical Operators Mathematical Operators
Operator Meaning
sign change multiplication real division integer division remainder addition subtraction Arity
unary binary binary binary binary binary binary Placement
prefix infix infix infix infix infix infix Precedence
15 13 13 13 13 12 12 – * / / % +
– The Minus Operators – The Minus Operators – Two applications of “–” The difference is determined by whether the “–” is in front of a single literal or identifier or between two. int x, y, z; x = 5; y = – x; // here the “–” is a sign change z = x – 3; // here the “–” is a subtraction The Division Operators / The Division Operators / There are two “/” operators Integer division occurs if both operands are integers In this case the result is only the whole number part of the result. So the result is also an integer.
2/2 evaluates to 1; 5/2 evaluates to 2; 4/2 evaluates to 2 2/5 evaluates to 0 The Division Operators / The Division Operators / There are two “/” operators Real division occurs if any of the operands are not integers. Any of the operands that are not integers are converted to doubles before dividing
2/2 evaluates to 1; evaluates to 2 2.0/2.0 evaluates to 1.0; 5/2 evaluates to 2; 5.0/2.0 evaluates to 2.5; 4/2 4.0/2 evaluates to 2.0 2/5 evaluates to 0 2/5.0 evaluates to 0.4 The Division Operators / The Division Operators / There are two “/” operators Which division is performed is determined entirely by context.
int x; x=5; cout << x/2; // this line will print “2” cout << x/2.0; // this line will print “2.5” The Remainder Operator % The Remainder Operator % The remainder operator “%” returns the remainder part in a division. “%” takes two integers as operands and returns an integer It is like the remainder algorithm we discussed in class.
2/2 2%2 5/2 5%2 evaluates to 1; evaluates to 0; evaluates to 2; evaluates to 1; 4/2 evaluates to 2 4%2 evaluates to 0 2/5 evaluates to 0 2%5 evaluates to 2 The order of operations is important. First sign changes Then multiplication, division and remainder Then addition and subtraction. 4 + 1 / 5 evaluates to 4 Parenthesis should be used to specify the order of operations and to avoid confusion (4 + 1) / 5 evaluates to 1 Mathematical Operator Mathematical Operator Precedence Evaluate: A 3 B 3.25 C 10 D 10.75 10.0 + 3.0 / 4.0 Evaluate: A 3 B 3.25 C 10 D 10.75 10.0 + 3.0 / 4.0 Evaluate: A 3 B 3.25 C 10 D 10.75 (10.0 + 3.0) / 4.0 Evaluate: A 3 B 3.25 C 10 D 10.75 (10.0 + 3.0) / 4.0 Evaluate: A 3 10 + 3 / 4 B 3.25 C 10 D 10.75 Evaluate: A 3 10 + 3 / 4 B 3.25 C 10 D 10.75 Evaluate: A 3 B 3.25 C 10 D 10.75 (10 + 3) / 4 Evaluate: A 3 B 3.25 C 10 D 10.75 (10 + 3) / 4 Evaluate: A 0 B 1 C 2 D 3 (7%2+1)/2 Evaluate: A 0 B 1 C 2 D 3 (7%2+1)/2 Evaluate: A 0 B 1 C 2 D 3 7%(2+1)/2 Evaluate: A 0 B 1 C 2 D 3 7%(2+1)/2 Other Kinds of Operators Other Kinds of Operators
Operator Meaning
insertion extraction assignment Arity
binary binary binary Placement
infix infix infix Precedence
11 11 2 << >> = Not all operators perform mathematical operations >> and << send output to and get input from external streams (keyboards, screens). = sets the value of an identifier. The Insertion Operator << The Insertion Operator << Expects an ostream (output stream) on the left and an expression on the right. Sends the expression to the ostream and returns the ostream. The ostream could be cout which is standard output (usually the screen) or alternately it could be a file (on a disk). << has lower precedence than any of the math operators. A series of << operations are evaluated left to right. The Insertion Operator << The Insertion Operator << In addition to numbers the << operator can send text strings. A string literal is enclosed in “quotes”. << can also send an endofline command, endl, so that the output skips to the next line. cout << “User: Oh. ”; cout << “Hello world!” << endl; cout << “Computer: all your base are belong to us…” << endl; outputs the following: User: Oh. Hello world! Computer: all your base are belong to us… Trace the execution and Trace the execution and determine what prints out
cout << 3.0+2.5 << 2 << endl;
1 5.5 2 2 5.5 2 3 5.52 4 7.5 Trace the execution and Trace the execution and determine what prints out
cout << 3.0+2.5 << 2 << endl;
1 5.5 2 2 5.5 2 3 5.52 4 7.5 The Insertion Operator << The Insertion Operator << So for example:
cout << 3.0+2.5 << 2 << endl; 5.52 ^ ^^ ^ evaluates to cout << 5.5 << 2 << endl; evaluates to cout << 2 << endl; (and outputs 5.5) evaluates to cout << endl; (and outputs 2) evaluates to cout; (skips another line) and ends What Prints Out? What Prints Out?
cout << “The first answer is ” << 3 + 17 / 3 << endl << “And the second answer is ” << 14 % 3 + 5 * 8 << endl; 1 2 3 4 5 6 The first answer is 8 And the second answer is 44 The first answer is 8 And the second answer is 42 The first answer is 6 And the second answer is 42 The first answer is 6 And the second answer is 44 The first answer is 8 And the second answer is 42 The first answer is 6 And the second answer is 44 What Prints Out? What Prints Out?
cout << “The first answer is ” << 3 + 17 / 3 << endl << “And the second answer is ” << 14 % 3 + 5 * 8 << endl; 1 2 3 4 5 6 The first answer is 8 And the second answer is 44 The first answer is 8 And the second answer is 42 The first answer is 6 And the second answer is 42 The first answer is 6 And the second answer is 44 The first answer is 8 And the second answer is 42 The first answer is 6 And the second answer is 44 cout << “The first answer is ” << 3 + 17 / 3 << endl << “And the second answer is ” << 14 % 3 + 5 * 8 << endl;
 Trace the execution and Trace the execution and determine what prints out cout << “The first answer is ” << 3 + 5 << endl << “And the second answer is ” << 2 + 40 << endl;
 Trace the execution and Trace the execution and determine what prints out cout << “The first answer is ” << 8 << endl << “And the second answer is ” << 42 << endl;
 Trace the execution and Trace the execution and determine what prints out cout << “The first answer is ” << 8 << endl << “And the second answer is ” << 42 << endl;
The first answer is  Trace the execution and Trace the execution and determine what prints out cout << 8 << endl << “And the second answer is ” << 42 << endl;
The first answer is 8 Trace the execution and Trace the execution and determine what prints out cout << endl << “And the second answer is ” << 42 << endl;
The first answer is 8  Trace the execution and Trace the execution and determine what prints out cout << “And the second answer is ” << 42 << endl; Trace the execution and Trace the execution and determine what prints out The first answer is 8 And the second answer is  cout << 42 << endl; Trace the execution and Trace the execution and determine what prints out The first answer is 8 And the second answer is 42 cout << endl; Trace the execution and Trace the execution and determine what prints out The first answer is 8 And the second answer is 42  cout; Trace the execution and Trace the execution and determine what prints out The first answer is 8 And the second answer is 42  Trace the execution and Trace the execution and determine what prints out The first answer is 8 And the second answer is 42  News Flash! Turn water into warmth (thanks to Taylor for this)! http://www.youtube.com/watch?v=Tf4gOS8aoFk http://www.youtube.com/watch?v=6Rb_rDkwGnU News Flash! News Flash! Proponents "This is the biggest discovery in 100 years in water research“ “The steam engine wasn't invented because thermodynamics existed. The steam engine was invented and then thermodynamics came along.” Professor Emeritus, Rustum Roy, at the Penn State University Materials Lab PSU research associate,Tania Slawecki Critics "There is no breakthrough, because there are more efficient ways of breaking water down to hydrogen and oxygen.“ "You can make steam or you can break it down. One way or another you have to add energy and what's going to come out is less than what you put in." University of Akron Professor Emeritus, Rudy Scavuzzo, Ph.D. Steven Scavuzzo, a technical consultant for Babcock & Wilcox The Extraction Operator >> The Extraction Operator >> Expects an istream (input stream) on the left and an identifier on the right. Obtains data from the istream and places the value into the identifier. The istream could be cin which is standard input (usually the keyboard) or alternately it could be a file (on a disk). The Assignment Operator = The Assignment Operator = Expects an identifier on the left and an expression on the right. identifier = expression; As a result the expression is evaluated and the value is placed in the memory location associated with the identifier. Trace the program Trace the program
#include <iostream> using namespace std; int main() { int x, y; double z; z=5.0; x=3; y=7; z = z + x; x = x + y / x; z = z / x; y = y % x; cout << “The answer is x=“ << x << “ and y=“ << y << “ and z=”<< z << endl; return 0; } Trace the program Trace the program
#include <iostream> using namespace std; int main() { int x, y; double z; z=5.0; x=3; y=7; z = z + x; x = x + y / x; z = z / x; y = y % x; cout << “The answer is x=“ << x << “ and y=“ << y << “ and z=”<< z << endl; return 0; } x y z Trace the program Trace the program
#include <iostream> using namespace std; int main() { int x, y; double z; z=5.0; x=3; y=7; z = z + x; x = x + y / x; z = z / x; y = y % x; x x y y z z 5.0 3 7 cout << “The answer is x=“ << x << “ and y=“ << y << “ and z=”<< z << endl; return 0; } Trace the program Trace the program
#include <iostream> using namespace std; int main() { int x, y; double z; z=5.0; x=3; y=7; z = z + x; x = x + y / x; z = z / x; y = y % x; x x 3 3 y y 7 7 z 5.0 z 8.0 cout << “The answer is x=“ << x << “ and y=“ << y << “ and z=”<< z << endl; return 0; } Trace the program Trace the program
#include <iostream> using namespace std; int main() { int x, y; double z; z=5.0; x=3; y=7; z = z + x; x = x + y / x; z = z / x; y = y % x; x x 3 5 y y 7 7 z 8.0 z 8.0 cout << “The answer is x=“ << x << “ and y=“ << y << “ and z=”<< z << endl; return 0; } Trace the program Trace the program
#include <iostream> using namespace std; int main() { int x, y; double z; z=5.0; x=3; y=7; z = z + x; x = x + y / x; z = z / x; y = y % x; x x 5 5 y y 7 7 z 8.0 z 1.6 cout << “The answer is x=“ << x << “ and y=“ << y << “ and z=”<< z << endl; return 0; } Trace the program Trace the program
#include <iostream> using namespace std; int main() { int x, y; double z; z=5.0; x=3; y=7; z = z + x; x = x + y / x; z = z / x; y = y % x; x x 5 5 y y 7 2 z 1.6 z 1.6 cout << “The answer is x=“ << x << “ and y=“ << y << “ and z=”<< z << endl; return 0; } Trace the program Trace the program
#include <iostream> using namespace std; int main() { int x, y; double z; z=5.0; x=3; y=7; z = z + x; x = x + y / x; z = z / x; y = y % x; x 5 y 2 z 1.6 The answer is x=5 and y=2 and z=1.6 cout << “The answer is x=“ << x << “ and y=“ << y << “ and z=”<< z << endl; return 0; } Trace the program Trace the program
#include <iostream> using namespace std; int main() { int x, y; double z; z=5.0; x=3; y=7; z = z + x; x = x + y / x; z = z / x; y = y % x; The answer is x=5 and y=2 and z=1.6 cout << “The answer is x=“ << x << “ and y=“ << y << “ and z=”<< z << endl; return 0; } Next Lecture Next Lecture Functions and Procedures ...
View
Full Document
 Fall '07
 JeffRingenberg
 ASCII, standard output, Extraction Operator

Click to edit the document details