review chapter 1 2-3
TRANSCRIPT
![Page 1: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/1.jpg)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition
Review
Chapter 1, 2, and 3
![Page 2: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/2.jpg)
Processing a C++ Program#include <iostream>using namespace std;int main() { cout << "My first C++ program." << endl;
system("pause"); return 0;
} ============================================
Sample Run: My first C++ program.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2
![Page 3: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/3.jpg)
The Problem Analysis–Coding–Execution Cycle
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 3
![Page 4: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/4.jpg)
Processing a C++ Program
• To execute a C++ program:– Step 1: Analyze the problem– Step 2: Design an algorithm – Step 3: Create a Program (Coding)
• Use an editor to create a source code in C++, Save the program on a secondary storage device (filename.cpp)
– Step 4: Directives begin with # are processed by the preprocessor
– Step 5: Use the compiler to:• Check that the program obeys the rules• If yes, translate C++ programs into machine language (object
code)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 4
![Page 5: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/5.jpg)
Processing a C++ Program (cont'd.)
– Step 6: Linking: • Combines object program with other programs
provided by the SDK to create an executable code
– Step 7: Loading: • Loads executable program into main memory
– Step 8: Execution• CPU executes the program one instruction at a
time.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 5
![Page 6: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/6.jpg)
A C++ Program
#include <iostream>
using namespace std;
int main()
{
int num;
num = 6;
cout << "My first C++ program." << endl;
cout << "The sum of 2 and 3 = " << 5 << endl;
cout << "7 + 8 = " << 7 + 8 << endl;
cout << "Num = " << num << endl;
return 0;
}
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 6
![Page 7: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/7.jpg)
Comments
• Comments used for the reader, not for the compiler
• Two types:– Single line
// This is a C++ program. It prints the sentence:// Welcome to C++ Programming.
– Multiple line/* You can include comments that can occupy several lines.*/
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 7
![Page 8: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/8.jpg)
Reserved Words (Keywords)
• Reserved words, keywords, or word symbols– Example:
• int• float• double• char• const• void• return
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 8
![Page 9: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/9.jpg)
Identifiers• Consist of letters, digits, and the underscore
character (_)
• Must begin with a letter or underscore
• C++ is case sensitive – NUMBER is not the same as number
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 9
![Page 10: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/10.jpg)
Identifiers (cont'd.)
• Legal identifiers in C++:firstconversionpayRate
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 10
![Page 11: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/11.jpg)
Simple Data Types int : integers (numbers without a decimal), four bytes
long, short, signed, unsigned
char : Used for characters: letters, digits, and special symbols. Enclosed in single quotes, one byte'A', 'a', '0', '*', '+', '$', '&‘, ' ‘
float: decimal numbers , represents any real number, four bytes
double: decimal numbers , represents any real number, eight bytesbool : true or false (0), one byte.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 11
![Page 12: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/12.jpg)
Arithmetic Operators and Operator Precedence
• C++ arithmetic operators: + addition - subtraction * multiplication / division % modulus operator
• +, -, *, and / can be used with integral and floating-point data types
• Operators can be unary or binary
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 12
![Page 13: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/13.jpg)
Order of Precedence
• All operations inside of () are evaluated first
• *, /, and % are at the same level of precedence and are evaluated next
• + and – have the same level of precedence and are evaluated last
• When operators are on the same level, Performed from left to right
• 3 * 7 - 6 + 2 * 5 / 4 + 6 means(((3 * 7) – 6) + ((2 * 5) / 4 )) + 6
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 13
![Page 14: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/14.jpg)
Expressions
• If the two operands are integers• Yields an integral result• Example: 5/2 2
• If the two operands are floating-point• Yields a floating-point result• Example: 5.0 / 2.0 2.5
• If mixed operands • Integer is changed to floating-point • Result is floating-point• Example: 5 / 2.0 2.5
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 14
![Page 15: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/15.jpg)
Type Conversion (Casting)
• Cast operator: provides explicit type conversion (value of one type is changed to another type)static_cast<dataTypeName>(expression)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 15
![Page 16: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/16.jpg)
Type Conversion (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 16
![Page 17: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/17.jpg)
string Type
• Sequence of zero or more characters
• Enclosed in double quotation marks
• Null: a string with no characters
• Each character has relative position in a string– Position of the first character is 0
• Length of a string : is the number of characters in the string.– Example: length of "William Jacob" is 13
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 17
![Page 18: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/18.jpg)
Constants
• const: is a memory location whose content can’t change during execution
• The syntax to declare a named constant is:
• In C++, const is a reserved word
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 18
![Page 19: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/19.jpg)
Increment and Decrement Operators
• Increment operator: increment variable by 1– Pre-increment: ++variable– Post-increment: variable++
• Decrement operator: decrement variable by 1– Pre-decrement: --variable– Post-decrement: variable--
• What is the difference between the following?
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 19
x = 5;y = ++x;
x = 5;y = x++;
![Page 20: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/20.jpg)
Compound Assignments
+=, -=, *=, /=, and %=
• Example: x *= y; x = x * y ;
x *= y+2 x= x * (y+2) ;
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 20
![Page 21: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/21.jpg)
I/O Streams
• Use iostream header file to extract (receive) data from keyboard and send output to the screen– Contains definitions of two data types:
• istream: input stream• ostream: output stream
– Has two variables:• cin: stands for common input• cout: stands for common output
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 21
![Page 22: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/22.jpg)
Output
• The syntax of cout and << is:
– Called an output statement
• The stream insertion operator is <<
• Expression evaluated and its value is printed at the current cursor position on the screen
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 22
![Page 23: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/23.jpg)
Output (cont'd.)
• A manipulator is used to format the output– Example: endl causes insertion point to
move to beginning of next line
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 23
![Page 24: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/24.jpg)
Output (cont'd.)
• The new line character is '\n' – May appear anywhere in the string
cout << "Hello there.";cout << "My name is James.";
• Output:Hello there.My name is James.
cout << "Hello there.\n";cout << "My name is James.";
• Output :Hello there.My name is James.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 24
![Page 25: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/25.jpg)
Output (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 25
![Page 26: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/26.jpg)
setw
• Outputs the value of an expression in specific columns– cout << setw(5) << x << endl;
• If number of columns exceeds the number of columns required by the expression– Output of the expression is right-justified– Unused columns to the left are filled with
spaces
• Must include the header file iomanipC++ Programming: From Problem Analysis to Program Design, Fifth Edition 26
![Page 27: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/27.jpg)
Input (Read) Statement
• cin is used with >> to gather input
• The stream extraction operator is >>
• For example, if miles is a double variable
cin >> miles;• Causes computer to get a value of type double• Places it in the variable miles
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 27
![Page 28: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/28.jpg)
Input (Read) Statement (cont'd.)
• Using more than one variable in cin allows more than one value to be read at a time
• For example, if feet and inches are variables of type int, a statement such as: cin >> feet >> inches;
– Inputs two integers from the keyboard– Places them in variables feet and inches
respectively
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 28
![Page 29: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/29.jpg)
cin and the Extraction Operator >> (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 29
![Page 30: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/30.jpg)
cin and the Extraction Operator >> (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 30
![Page 31: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/31.jpg)
cin and the Extraction Operator >> (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 31
![Page 32: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/32.jpg)
cin and the Extraction Operator >> (cont'd.)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 32
![Page 33: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/33.jpg)
Input/Output and the string Type– Entering a char value into an int or double variable causes serious errors, called input failure
– cin can read a string into a variable of the data type string (without spaces)
– The function getline() reads until end of the current line (with spaces)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 33
![Page 34: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/34.jpg)
Syntax Errors & Logical Errors
• Syntax errors – Reported by the compiler
int x; //Line 1int y //Line 2: errordouble z; //Line 3y = w + x; //Line 4: error
• Logic errors – Typically not caught by the compiler– Spot and correct using cout statements– Temporarily insert an output statement– Correct problem– Remove output statement
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 34
![Page 35: Review chapter 1 2-3](https://reader034.vdocuments.net/reader034/viewer/2022052505/55620910d8b42a00138b4703/html5/thumbnails/35.jpg)
HW1: Submission next meeting