Lecture 03 - Engineering 101 Lecture 3 9/11/07 C++ Basics...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Lecture 3 9/11/07 C++ Basics Quote of the Day If you take no thought about what is distant, you will find sorrow near at hand. Confucius From last lecture Sequence Selection Iteration Greatest Common Factor 1: Request a value for a 2: Request a value for b 3: if a b then assign b to c 4: if a < b then assign a to c 5: while (remainder(a, c)0 OR remainder(b, c)0) 6: subtract one from c 7: return c 8: end From last lecture Scaffolding Identifiers Simple type #include <iostream> using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Declaration Expressions Reading A C++ Program: Scaffolding #include <iostream> 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 <iostream> 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 <iostream> 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. #include <iostream> using namespace std; // multiply a number by 7 int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } 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. Reading A C++ Program: Scaffolding #include <iostream> 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 <iostream> using namespace std; // multiply a number by 7 int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } #include <iostream> include <cmath> using namespace std { C 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) <<endl; { A Look at this error riddled program. Which are the declarations, which are the expressions and which are the scaffolding? B #include <iostream> include <cmath> using namespace std { C 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) <<endl; { A B C: scaffolding, A: declarations, B: expressions #include <iostream> include <cmath> using namespace std { There are between 1 and 7 errors in the program. How many do you see? 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) <<endl; { 1 #include <iostream> #include <cmath> using namespace std; 2 int main( ) 3 { 4 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; 5 cout << "The answer is:" << sqrt(z) <<endl; return 0; 6 } 7 7 scaffolding declarations expressions errors #include <iostream> #include <cmath> 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 <iostream> #include <cmath> using namespace std; 1 int main( ) { 2 double a, b, c; cout << "Enter your weight in pounds: " << endl; 3 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 } 4 scaffolding declarations expressions errors Recap: Reading A C++ Program #include <iostream> Scaffolding provides using namespace std; contextual information that // multiply a number by 7 holds the program together. Declarations provide int main ( ) information about the { double x, y; identifiers used by the cin >> x; program. y = x * 7; Expressions change and cout << y; exchange data. return 0; } 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. Caller (Linux OS) The OS can My Program only directly Data input call machine Data language output executables. Data Interpreted vs. Compiled Programming Languages 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... 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. 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 STEP 1: You use OS to start a machine code executable called a text editor. machine code files You (the user) The computer OS text editor g++ compiler text files Creating and Compiling a C++ Program STEP 2: Using that program you create a text file called Project1.cpp machine code files You (the user) The computer OS text editor Project1.cpp g++ compiler text files Creating and Compiling a C++ Program STEP 3: You then use the OS to start the g++ compiler, another machine code executable machine code files You (the user) The computer OS text editor Project1.cpp g++ compiler text files Creating and Compiling a C++ Program STEP 4: g++ then translates Project1.cpp to a machine language executable called Project1 machine code files You (the user) The computer OS text editor Project1.cpp g++ compiler Project1 text files Creating and Compiling a C++ Program STEP 5: Using the OS you can then run the new executable that g++ created, it may create files machine code files You (the user) The computer OS text editor Project1.cpp g++ compiler fakefile.dat Project1 text files The command to compile a C++ program is: g++ mycode.cpp o myexec Creating and Compiling a C++ Program 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. News Flash! Machines Powered by Heart Muscles Pumps Walkers Swimmers http://www.technologyreview.com/Biotech/19352/ News Flash! Pumps See link "A curled triangular piece of plastic walks across the bottom of a petri dish as muscle tissue repeatedly contracts" "Another triangular sheet, with a different arrangement of heartmuscle cells, mimics the movement of a fish's tail to swim" Walkers Swimmers News Flash! Muscle is made to grow on the plastic using protein "cues" that dictate alignment Muscles currently only survive for a few weeks, so improvements need to be made Other articles: iBody article in New Scientist Magazine http://www.tmcnet.com/usubmit/2007/08/24/2886494.htm 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.0105). 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. 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 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 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; 5/2 evaluates to 2; 4/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; 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 / 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 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 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 Next Lecture Input / Output ...
View Full Document

This note was uploaded on 03/27/2008 for the course ENGR 101 taught by Professor Ringenberg during the Fall '07 term at University of Michigan.

Ask a homework question - tutors are online