
Lecture_01.ppt
- Количество слайдов: 25
Week 1 Algorithms and Data Structures
About me Orazbekov Sayatbek Kairbekovich Education: 1995 -2006 – 8 th Gymnasium, Zhezkazgan 2006 -2010 – Suleyman Demirel University, Almaty 2011 -2012 – Heriot-Watt University, Edinburgh Work in IT since 2012 Married, have 2 kids Orazbekov. sayatbek@gmail. com International IT University
1. 1 General Notes About C++ and This Course What do you need? Books: C++ How to Program, Fifth (fourth) Edition By H. M. Deitel & Associates C++A Beginner’s Guide By Herbert Schildt Absolute C++ By Walter Savitch IDE: Microsoft Visual C++ 2010 (Express or Professional editions) Sites: http: //cplus. com/ http: //e-practice. org http: //www. iitu. kz/ Your Mind (Brain)
1. 3 Basics of a Typical C++ Environment Input/output cin Standard input stream Normally keyboard cout Standard output stream Normally computer screen cerr Standard error stream Display error messages 4
1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 1. 2: fig 01_02. cpp Single-line // A first program in C++. Left brace { begins comments. #include <iostream> Function main returns an function body. Preprocessor directive to integer value. Statements end with a // function main begins program executioninclude input/output Function main appears semicolon ; . int main() stream header file exactly once in every C++ { <iostream>. program. . std: : cout << "Welcome to C++!n"; return 0; // indicate that program ended successfully } // end function main Name cout belongs to Stream insertion operator. namespace std. Keyword return is one of several means to exit function; value 0 indicates program terminated successfully. Corresponding right brace } ends function body. Welcome to C++! 5
1. 31 A Simple Program: Printing a Line of Text 6
1 2 3 4 5 6 7 8 9 10 11 12 13 // Fig. 1. 4: fig 01_04. cpp // Printing a line with multiple statements. #include <iostream> // function main begins program execution int main() { std: : cout << "Welcome "; std: : cout << "to C++!n"; Multiple stream insertion statements produce one line of output. return 0; // indicate that program ended successfully } // end function main Welcome to C++! 7
1 2 3 4 5 6 7 8 9 10 11 12 // Fig. 1. 5: fig 01_05. cpp // Printing multiple lines with a single statement #include <iostream> // function main begins program execution Using newline characters int main() to print on multiple lines. { std: : cout << "Welcomentonn. C++!n"; return 0; // indicate that program ended successfully } // end function main Welcome to C++! 8
1. 4 Variables Location in memory where value can be stored Common data types int - integer numbers char - characters double - floating point numbers Declare variables with name and data type before use integer 1; integer 2; int sum; Can declare several variables of same type in one declaration Comma-separated list integer 1, integer 2, sum; 9
1. 5 Memory Concepts std: : cin >> integer 1; integer 1 45 integer 2 72 Assume user entered 45 std: : cin >> integer 2; Assume user entered 72 sum = integer 1 + integer 2; sum 117 10
1. 6 Data types 11
1. 6 C++ Data Types The guaranteed minimum range for each type as specified by the ANSI/ISO C++ standard 12
1. C++ Data Types 60, 000 is within the range of an unsigned short int, but is typically outside the range of a signed short int. Thus, it will be interpreted as a negative value when assigned to i. 13
1. 7 Arithmetic calculations * Multiplication / Division Integer division truncates remainder 7 / 5 evaluates to 1 % Modulus operator returns remainder 7 % 5 evaluates to 2 14
1. 8 Decision Making: Equality and Relational Operators if structure Make decision based on truth or falsity of condition If condition met, body executed Else, body not executed Equality and relational operators Equality operators Same level of precedence Relational operators Same level of precedence Associate left to right 15
1. 8 Decision Making: Equality and Relational Operators 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 // // Using if statements, relational // operators, and equality operators. #include <iostream> using std: : cout; // program uses cout using std: : cin; // program uses cin using std: : endl; // program uses endl // function main begins program execution int main() { int num 1; // first number to be read from user int num 2; // second number to be read from user cout << "Enter two integers, and I will tell youn" << "the relationships they satisfy: "; cin >> num 1 >> num 2; // read two integers if ( num 1 == num 2 ) cout << num 1 << " is equal to " << num 2 << endl; using statements eliminate need for std: : prefix. Declare variables. Can write cout and cin without std: : prefix. if structure compares values of num 1 and num 2 to test for equality. If condition is true (i. e. , values are equal), execute this statement. if structure compares values of num 1 and num 2 to test for inequality. If condition is true (i. e. , values are not equal), if ( num 1 != num 2 ) cout << num 1 << " is not equal to " << num 2 << endl; execute this statement. 17
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 if ( num 1 < num 2 ) cout << num 1 << " is less than " << num 2 << endl; if ( num 1 > num 2 ) cout << num 1 << " is greater than " << num 2 << endl; if ( num 1 <= num 2 ) cout << num 1 << " is less than or equal to " << num 2 << endl; Statements may be split over several lines. if ( num 1 >= num 2 ) cout << num 1 << " is greater than or equal to " << num 2 << endl; return 0; // indicate that program ended successfully } // end function main 18
Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than or equal to 12 Enter two integers, and I will tell you the relationships they satisfy: 7 7 7 is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7 19
for Repetition Statement example
Examples Using the for Statement Vary the control variable from 1 to 100 in increments of 1. for ( int i = 1; i <= 100; i++ ) Vary the control variable from 100 down to 1 in increments of -1 (that is, decrements of 1). for ( int i = 100; i >= 1; i-- )
Examples Using the for Statement Vary the control variable from 7 to 77 in steps of 7. for ( int i = 7; i <= 77; i += 7 ) Vary the control variable from 20 down to 2 in steps of -2. for ( int i = 20; i >= 2; i -= 2 )
Summing integers with the for statement.
Readings: C++ How to Program, By H. M. Deitel Chapter 1. Introduction to Computers, the Internet and World Wide Web Chapter 2. Introduction to C++ Programming
Thanks for your attention! 25
Lecture_01.ppt