02 - C++ Basics and Input-Output

02 - C++ Basics and Input-Output - Engineering 101 Quote of...

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 Quote of the Day The people may be made to follow a path of action, but they may not be made to understand it. - Confucius C++ Basics and Input/Output Announcements Exit strategy! 2 exits at the front of the class 2 exits at the sides of the class 1 exit at the rear of the aircraft class From Algorithms To Programs So far we have described a few algorithms in pseudopseudo-code. PseudoPseudo-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. 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. language. This translation is done by a compiler. compiler. Writing directly in machine language is taxing for humans because it is so rudimentary. Reading A C++ Program #include <iostream> using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } 1 Reading A C++ Program Much of this program is scaffolding. 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. #include <iostream> using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program 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 <iostream> 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 <iostream> 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; #include <iostream> using namespace std; int main ( ) In this case the identifiers x and { double x, y; y are declared to be of the type cin >> x; double. double. y = x * 7; For now we will consider two cout << y; simple types: double and int. int. return 0; } Reading A C++ Program: Declaration 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. #include <iostream> using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program: Expressions The next 3 lines of the program are expressions. expressions. Expressions change and exchange data. The basic form of an expression is: expression; #include <iostream> using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Always choose the one closest to your meaning. 2 Reading A C++ Program: Expressions The first expression, cin >> x; will obtain input from the caller. The second expression, y = x * 7; multiples x by 7 and 7; assigns the result to y. The third expression, cout << y; will output y to the y; caller. #include <iostream> using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } Reading A C++ Program: Semicolons #include <iostream> using namespace std; ; Denote the end of a declaration or expression. Critical for determining order of execution. The most common errors are omitted semicolons. int main ( ) { double x, y; y; cin >> x; x; y = x * 7; 7; cout << y; y; return 0; } Reading A C++ Program: ColorColor-Coded! Scaffolding Identifiers Simple type #include <iostream> using namespace std; int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } 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; } Declaration Expressions 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. 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; } 3 Reading A C++ Program: Scaffolding int main() is a C++ construct that denotes the start of a function. 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 <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. 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? #include <iostream> include <cmath> using namespace std { There are between 1 and 7 errors in the program. How many do you see? B { 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; #include <iostream> #include <cmath> using namespace std There are between 1 and 5 errors in the program. How many do you see? Recap: Reading A C++ Program Scaffolding provides contextual information that holds the program together. Declarations provide information about the identifiers used by the program. Expressions change and exchange data. #include <iostream> using namespace std; // multiply a number by 7 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 } int main ( ) { double x, y; cin >> x; y = x * 7; cout << y; return 0; } 4 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 only directly My Program 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. system. This is done by running a compiler. On our compiler. computer system the C++ compiler is called g++. 5 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 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 ./myexec 6 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. C++ Expressions Expressions consist of operators, literals and identifiers. operators, identifiers. Literals are direct representations of data such as 2.4, 5 2.4, or 1.0e-5 (which is the way a computer writes 1.010-5). 1.0e1.0 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. 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 operator new data Operators that operate on one operand (data object) are unary, those that operate on two operands are unary, called binary. binary. * / / % + 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 7 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; 2.0/2.0 evaluates to 1.0; 5/2 evaluates to 2; 5.0/2.0 evaluates to 2.5; 4/2 evaluates to 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 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 Evaluate: A- 3 B- 3.25 C- 10 D- 10.75 Evaluate: A- 3 B- 3.25 C- 10 D- 10.75 10.0 + 3.0 / 4.0 10 + 3 / 4 8 Evaluate: A- 3 B- 3.25 C- 10 D- 10.75 Evaluate: A- 0 B- 1 C- 2 D- 3 (10 + 3) / 4 (7%2+1)/2 Other Kinds of Operators Operator Meaning insertion extraction assignment 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. 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. 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 << 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 end-ofendl, 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; 1- 5.5 2 2- 5.5 2 3- 5.52 4- 7.5 9 What Prints Out? The Extraction Operator >> 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 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. 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. identifier. int main() { int x, y; double z; Trace the program #include <iostream> using namespace std; 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 #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 #include <iostream> using namespace std; x y z 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; 10 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; Trace the program #include <iostream> using namespace std; x x 3 3 y y 7 7 z 5.0 z 8.0 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; } } cout << "The answer is x=" << x << " and y=" << y << " and z="<< z << endl; return 0; 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; Trace the program #include <iostream> using namespace std; x x 5 5 y y 7 7 z 8.0 z 1.6 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; } } cout << "The answer is x=" << x << " and y=" << y << " and z="<< z << endl; return 0; 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; Trace the program #include <iostream> using namespace std; x 5 y 2 z 1.6 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 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; } } cout << "The answer is x=" << x << " and y=" << y << " and z="<< z << endl; return 0; 11 Next Lecture Functions and Procedures 12 ...
View Full Document

Ask a homework question - tutors are online