Lecture notes 2
Engineering 101 with Ringenberg at University of Michigan - Ann Arbor
About this note
By: Manas Tetarbe
Textbook:
Introduction to Engineering Programming: Solving Problems with Algorithms
MATLAB: An Introduction with Applications
Created: 2008-04-04
File Size: 16 page(s)
Views: 11
Textbook:
Introduction to Engineering Programming: Solving Problems with Algorithms
MATLAB: An Introduction with ApplicationsCreated: 2008-04-04
File Size: 16 page(s)
Views: 11
About StudyBlue
STUDYBLUE makes things that make you better at school.
Things like online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free. Join us.
“I have been getting MUCH better grades on all my tests for school. Flash cards, notes, and quizzes are great on here. Thanks!”
Kathy
Kathy
Sign up (free) to study this.
1 Engineering 101 C++ Basics and Input/Output 1/9/08 Quote of the Day - Confucius The people may be made to follow a path of action, but they may not be made to understand it. Announcements ? Exit strategy! ? 2 exits at the front of the class ? 2 exits at the sides of the class ? 2 exits at the rear of the aircraft class ? Exams 1-3 are now 6-8pm, rooms posted on CTools ? Bond Consulting Group (student-run consulting group) ? Today (1/9/2008) ? 5:15pm - 6:30pm ? Room W0768 at the Business School (Wyly Hall) ? Students (all majors accepted) develop skills for analyzing business models, pricing, marketing, and operations From Algorithms To Programs ? So far we have described a few algorithms in pseudo-code. ? Pseudo-code is fine for communicating algorithms between people but it is not precise enough to be used by a computer. Our Algorithms Must Be Written in a Programming Language ? A programming language has precise syntax (grammar) and semantics (meaning). ? An algorithm in a programming language is called a program. ? To be used by the machine it must be translated into a native language specific to the computer?s CPU (central processing unit) called machine language. ? This translation is done by a compiler. ? Writing directly in machine language is taxing for humans because it is so rudimentary. Reading A C++ Program #include using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } 2 #include using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program ? Much of this program is scaffolding. ? Scaffolding is only important insomuch as it holds the program together and provides context for the C++ compiler. ? We will consider this part of the program soon. ? The remaining part of the code consists of only 4 lines. ? The code reads in a number and then writes the result of multiplying the number by 7. #include using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program #include using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program: Declaration ? The first line of the body declares two containers to hold numbers. ? The identifiers of these containers will be x and y. ? It is important to declare every identifier before it is used. #include using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program: Declaration ? Simple declarations are of the form: simple_type identifiers; ? In this case the identifiers x and y are declared to be of the type double. ? For now we will consider two simple types: double and int. #include using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } ? A double is a number with a fractional part that can take on a very large range of magnitudes. ? An int is an integer number between about -2 billion to 2 billion. ? Always choose the one closest to your meaning. Reading A C++ Program: Declaration #include using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } ? The next 3 lines of the program are expressions. ? Expressions change and exchange data. ? The basic form of an expression is: expression; Reading A C++ Program: Expressions 3 #include using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } ? The first expression, cin >> x; will obtain input from the caller. ? The second expression, y = x * 7; multiples x by 7 and assigns the result to y. ? The third expression, cout << y; will output y to the caller. Reading A C++ Program: Expressions #include using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } ; ? Denote the end of a declaration or expression. ? Critical for determining order of execution. ? The most common errors are omitted semicolons. Reading A C++ Program: Semicolons #include using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program: Color-Coded! ? Scaffolding ? Identifiers ? Simple type Declaration Expressions #include using namespace std; // multiply a number by 7 int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program: Scaffolding ? #include is a preprocessor directive. ? It tells the compiler to look for a header file important for accessing a library of algorithms. ? In this case we want to use the iostream library that describes how to get input from the keyboard and print output to the screen. #include using namespace std; // multiply a number by 7 int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program: Scaffolding ? Some of the identifiers in iostream have complicated names like std::cin to denote the ?standard input stream? and std::cout for the ?standard output stream?. ? using namespace std is a directive that tells the compiler to assume the prefix std:: so we can just type cin or cout. #include using namespace std; // multiply a number by 7 int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program: Scaffolding ? The next line in the program is a comment. ? Comments are denoted by //. ? Anything after the // is ignored and not considered by the compiler. ? Comments are important so that other human beings can understand your code. 4 #include using namespace std; // multiply a number by 7 int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program: Scaffolding ? int main() is a C++ construct that denotes the start of a function. ? main is a special function. It is the function called by the OS if the program is executed. ? The empty parentheses denote that main takes no input from the OS. ? ?int? denotes that main will return an integer. This is a vestigial feature. #include using namespace std; // multiply a number by 7 int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program: Scaffolding ? Braces { } denote a compound statement. ? The braces around the central program denote that it is all part of the main function. ? return 0; ends the main function and returns an integer 0 to the OS. #include include using namespace std { double x, z; cout << ?Please enter a number:? << endl; cin >> x; cout << ?Please enter a second number:? << endl; cin >> y; z = x*x; z = z + y*y cout << ?The answer is:? << sqrt(z) < include using namespace std { double x, z; cout << ?Please enter a number:? << endl; cin >> x; cout << ?Please enter a second number:? << endl; cin >> y; z = x*x; z = z + y*y cout << ?The answer is:? << sqrt(z) < include using namespace std { double x, z; cout << ?Please enter a number:? << endl; cin >> x; cout << ?Please enter a second number:? << endl; cin >> y; z = x*x; z = z + y*y cout << ?The answer is:? << sqrt(z) < #include using namespace std; int main( ) { double x, y, z; cout << ?Please enter a number:? << endl; cin >> x; cout << ?Please enter a second number:? << endl; cin >> y; z = x*x; z = z + y*y; cout << ?The answer is:? << sqrt(z) < #include using namespace std int main( ) { double a, b; cout << ?Enter your weight in pounds: ? << endl cin >> a; cout << ?Enter your height in inches: ? << endl; cin >> b; c = 7.03 * a / pow(b, 2); cout << ?Your BMI is: ? << c << endl; return 0 } There are between 1 and 5 errors in the program. How many do you see? #include #include using namespace std; int main( ) { double a, b, c; cout << ?Enter your weight in pounds: ? << endl; cin >> a; cout << ?Enter your height in inches: ? << endl; cin >> b; c = 7.03 * a / pow(b, 2); cout << ?Your BMI is: ? << c << endl; return 0; } 4 1 2 3 scaffolding declarations expressions errors 4 Recap: Reading A C++ Program #include using namespace std; // multiply a number by 7 int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } ? Scaffolding provides contextual information that holds the program together. ? Declarations provide information about the identifiers used by the program. ? Expressions change and exchange data. From Program to Executable ? To make use of our program we must be able to call it. ? The primary caller for our programs is the operating system (OS). The Operating System (OS) is the Primary Caller ? Our OS will be Linux ? The OS allows input via the keyboard and output via the screen. Input and output can also take place to files on disks, speakers, microphones, etc. ? The OS can only directly call machine language executables. Caller (Linux OS) My Program Data Data Data input output Interpreted vs. Compiled Programming Languages ?Call the associate with an interpreter on the line. ?Write down what you need to say. Have it translated, and fax it to your colleague. Let?s say you are working on Project 1 and need to ask the other engineers a question. The problem is they are from may different countries and may actually speak another language? 6 Interpreted vs. Compiled Programming Languages ?Interpreted languages are flexible but slow. Each line gets translated independently but you always have to pay the price of having an interpreter around. ?Compiled languages are less flexible but more efficient. The entire program gets translated at once so when it is executing the CPU only deals with native code. Interpreted vs. Compiled Programming Languages ?Interpreted languages: BASIC, MATLAB ?Compiled languages: C, C++, Fortran Compiling a C++ Program ? In order to utilize our program, we must first translate it into machine language so that the computer can understand it. ? Only once it is translated can it be called by the operating system. ? This is done by running a compiler. On our computer system the C++ compiler is called g++. Creating and Compiling a C++ Program You (the user) The computer OS text editor g++ compiler STEP 1: You use OS to start a machine code executable called a text editor. machine code files text files Project1.cpp Creating and Compiling a C++ Program You (the user) The computer OS text editor g++ compiler STEP 2: Using that program you create a text file called Project1.cpp machine code files text files Project1.cpp Creating and Compiling a C++ Program You (the user) The computer OS text editor g++ compiler STEP 3: You then use the OS to start the g++ compiler, another machine code executable machine code files text files 7 Project1.cpp Creating and Compiling a C++ Program You (the user) The computer OS text editor g++ compiler STEP 4: g++ then translates Project1.cpp to a machine language executable called Project1 machine code files text files Project1 Project1.cpp Creating and Compiling a C++ Program You (the user) The computer OS text editor g++ compiler STEP 5: Using the OS you can then run the new executable that g++ created, it may create files machine code files text files Project1 fakefile.dat Creating and Compiling a C++ Program ? The command to compile a C++ program is: g++ mycode.cpp ?o myexec ? Here mycode.cpp is the name of the text file containing your program and myexec is the resulting machine language executable. ? You can then execute the new program by typing: myexec Recap ? All algorithms consist of sequence, iteration and selection. ? In order to make an algorithm into a program it must be written in a programming language. ? C++ programs are composed of declarations and expressions plus scaffolding. ? A C++ program must be compiled before it can be executed. ? Wild Speculation for Windows Mobile 7! ? I wanna be an iPhone please? ? http://www.engadget.com/2008/01/06/is-this-windows-mobile-7/ News Flash! News Flash ? Gesture Control 8 News Flash ? Camera Gesture Recognition News Flash! ? Touch-based Photo Editing News Flash! ? Media Player C++ Expressions ? Expressions consist of operators, literals and identifiers. ? Literals are direct representations of data such as 2.4, 5 or 1.0e-5 (which is the way a computer writes 1.0×10 -5 ). ? 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 are the most interesting of the three since they transform data. ? Operators that operate on one operand (data object) are unary, those that operate on two operands are called binary. operator L it e r a ls Id e n ti fi e rs new data Mathematical Operators 12infixbinarysubtraction ? 12infixbinaryaddition + 13infixbinaryremainder % 13infixbinaryinteger division / 13infixbinaryreal division / 13infixbinarymultiplication * 15prefixunarysign change ? PrecedencePlacementArityMeaningOperator 9 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 / ? 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; 4/2 evaluates to 2 5/2 evaluates to 2; 2/5 evaluates to 0 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; 4/2 evaluates to 2 2.0/2.0 evaluates to 1.0; 4.0/2 evaluates to 2.0 5/2 evaluates to 2; 2/5 evaluates to 0 5.0/2.0 evaluates to 2.5; 2/5.0 evaluates to 0.4 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 ?%? 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 evaluates to 1; 4/2 evaluates to 2 2%2 evaluates to 0; 4%2 evaluates to 0 5/2 evaluates to 2; 2/5 evaluates to 0 5%2 evaluates to 1; 2%5 evaluates to 2 Mathematical Operator Precedence ? 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 10 Evaluate: 10.0 + 3.0 / 4.0 A- 3 B- 3.25 C- 10 D- 10.75 Evaluate: 10.0 + 3.0 / 4.0 A- 3 B- 3.25 C- 10 D- 10.75 Evaluate: 10 + 3 / 4 A- 3 B- 3.25 C- 10 D- 10.75 Evaluate: 10 + 3 / 4 A- 3 B- 3.25 C- 10 D- 10.75 Evaluate: (10 + 3) / 4 A- 3 B- 3.25 C- 10 D- 10.75 Evaluate: (10 + 3) / 4 A- 3 B- 3.25 C- 10 D- 10.75 11 Evaluate: (7%2+1)/2 A- 0 B- 1 C- 2 D- 3 Evaluate: (7%2+1)/2 A- 0 B- 1 C- 2 D- 3 Other Kinds of Operators ? Not all operators perform mathematical operations ? >> and << send output to and get input from external streams (keyboards, screens). ? = sets the value of an identifier. 2infixbinaryassignment = 11infixbinaryextraction >> 11infixbinaryinsertion << PrecedencePlacementArityMeaningOperator 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 << ? In addition to numbers the << operator can send text strings. ? A string literal is enclosed in ?quotes?. ? << can also send an end-of-line 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 determine what prints out cout << 3.0+2.5 << 2 << endl; 7.54- 5.523- 5.5 2 2- 5.5 21- 12 Trace the execution and determine what prints out cout << 3.0+2.5 << 2 << endl; 7.54- 5.523- 5.5 2 2- 5.5 21- The Insertion Operator << ? So for example: cout << 3.0+2.5 << 2 << << 3. 5 << 2 < endl; evaluates to cout; (skips another line) and ends evaluates to cout << endl; (and outputs 2) evaluates to cout << 2 << endl; (and outputs 5.5) evaluates to cout << 5.5 << 2 << endl; ^ 5.52 ^ ^ ^ What Prints Out? cout << ?The first answer is ? << 3 + 17 / 3 << endl << ?And the second answer is ? << 14 % 3 + 5 * 8 << endl; The first answer is 6 And the second answer is 44 6 The first answer is 8 And the second answer is 425 The first answer is 6 And the second answer is 44 4 The first answer is 6 And the second answer is 423 The first answer is 8 And the second answer is 42 2 The first answer is 8 And the second answer is 441 What Prints Out? cout << ?The first answer is ? << 3 + 17 / 3 << endl << ?And the second answer is ? << 14 % 3 + 5 * 8 << endl; The first answer is 6 And the second answer is 44 6 The first answer is 8 And the second answer is 425 The first answer is 6 And the second answer is 44 4 The first answer is 6 And the second answer is 423 The first answer is 8 And the second answer is 42 2 The first answer is 8 And the second answer is 441 Trace the execution and determine what prints out cout << ?The first answer is ? << 3 + 17 / 3 << endl << ?And the second answer is ? << 14 % 3 + 5 * 8 << endl; | Trace the execution and determine what prints out cout << ?The first answer is ? << 3 + 5 << endl << ?And the second answer is ? << 2 + 40 << endl; | 13 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 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 determine what prints out cout << 8 << endl << ?And the second answer is ? << 42 << endl; The first answer is 8| 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 determine what prints out cout << ?And the second answer is ? << 42 << endl; The first answer is 8 And the second answer is | Trace the execution and determine what prints out cout << 42 << endl; The first answer is 8 And the second answer is 42| 14 Trace the execution and determine what prints out cout << endl; The first answer is 8 And the second answer is 42 | Trace the execution and determine what prints out cout; The first answer is 8 And the second answer is 42 | Trace the execution and determine what prints out The first answer is 8 And the second answer is 42 | 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 = ? 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 #include 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; } 15 Trace the program #include 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; } xyz Trace the program #include 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; } xyz xyz 375.0 Trace the program #include 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; } xyz 375.0 xyz 378.0 Trace the program #include 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; } xyz 378.0 xyz 578.0 Trace the program #include 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; } xyz 578.0 xyz 571.6 Trace the program #include 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; } xyz 571.6 xyz 521.6 16 Trace the program #include 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; } xyz 521.6 The answer is x=5 and y=2 and z=1.6 Trace the program #include 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; } The answer is x=5 and y=2 and z=1.6 Next Lecture ? Functions and Procedures root Microsoft PowerPoint - 02 - C++ Basics and Input-Output.ppt
Back
Next
About this note
By: Manas Tetarbe
Textbook:
Introduction to Engineering Programming: Solving Problems with Algorithms
MATLAB: An Introduction with Applications
Created: 2008-04-04
File Size: 16 page(s)
Views: 11
Textbook:
Introduction to Engineering Programming: Solving Problems with Algorithms
MATLAB: An Introduction with ApplicationsCreated: 2008-04-04
File Size: 16 page(s)
Views: 11
About StudyBlue
STUDYBLUE makes things that make you better at school.
Things like online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free. Join us.
“I have been getting MUCH better grades on all my tests for school. Flash cards, notes, and quizzes are great on here. Thanks!”
Kathy
Kathy