Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS Computer Science IA: Procedural Programming

Similar presentations


Presentation on theme: "CS Computer Science IA: Procedural Programming"— Presentation transcript:

1 CS 13011 Computer Science IA: Procedural Programming
Final Exam 1

2 C++ Basics Variable I/O Operators Namespace Declarations
Variable naming conventions Assignment of values to variables I/O Operators cin>> cout<< #include <iostream> using std::cout; using std::endl; Namespace Different programmers write their own source code in different namespaces using namespace ns1; using namespace ns2; Use namespaces to refer to variables (possibly with the same name) ns1::x ns2::x

3 Operations Data types Constant (vs. variable) Operators in C++
int, double, float, bool, char, string, … Constant (vs. variable) Literal constant Named constant Operators in C++ +, - (unary) +, -, *, /, % (binary) Conditional operator: ? = (ternary) Precedence among operators How to evaluate an expression with operators?

4 Operations (cont'd) Assignment operator Compound operators
= Compound operators += -= *= /= %= Increment/decrement operators ++, --

5 Operations (cont'd) Comparison operators Logical operators ==, !=
>, <, >=, <= Logical operators &&, ||, !

6 Selection Statements Boolean expression if statement (syntax)
Evaluation if statement (syntax) if … if … else … Nested if: if … else if … else … if … else switch Conversion between switch and nested if

7 Repetition Statements
while do … while for nested loops: for … for while … while do do … while while

8 Functions Pre-defined functions Programmer-defined functions
pow(), sqrt(), abs() Type changing function: static_cast<…>() srand(), rand() Range of rand()? rand()%100? time() Programmer-defined functions function name argument(s) return value function call function invocation nested function call

9 Functions (cont'd) Function prototype Function declaration
int add1(int i); int add1(int); Function declaration int add1(int i) { ... } Local vs. global variables Global constants/variables Call-by-value vs. call-by-reference Value-returning function vs. void function return x; vs. return; (or simply without this statement)

10 Arrays Array declarations Initialization
int x[index]; index must be integer and constant Initialization Initialization list int x[] = {1, 2, 3}; int x[3] = {1, 2, 3}; int x[10] = {1, 2, 3}; for loop for (int i=0; i<size; i++) Access to an element in the array x[i] – range of index I Assignment: x[i] = 1; Read values: int a = x[i]+1; Iterate over all elements sum, max, min, avg, search a key, etc.

11 Arrays (cont'd) Passing array into the function Arrays
E.g., void func(int x[]); Passing by reference const array: do not modify contents in the array Arrays

12 Strings String declarations Assignment Functions string str;
#include <string> Assignment str="hello"; cin>>str; getline(cin, str); Functions cin.peek(); cin.get(); cin.unget();

13 Strings (cont'd) Operators String functions
+, += comparison operators (>, <, >=, <=, ==, !=) String functions size() - current string size (number of characters currently stored in string length()- same as size() max_size() - maximum number of characters in string allowed on this computer empty() - true if string is empty str[i] – element in the string Range of index i

14 Strings (cont'd) String functions
find(substring) - returns the position of the first character of substring in the string rfind(substring) - same as find, search in reverse find_first_of(substring) - find first occurrence of any character of substring in the string find_last_of(substring) - find last occurrence of any character of substr in the string insert(start, substring)- inserts substring starting from position start insert(start, number, character) – inserts number of character starting from position start replace (start, number, substring)- replaces number of characters starting from start with substring the number of characters replaced need not be the same append(string2)- appends string2 to the end of the string erase(start, number)- removes number of characters starting from start

15 Strings (cont'd) Passing string to function
By value By reference Return string as the output of the function

16 File Input/Output <fstream>
using std::ifstream; and using std::ofstream; ifstream fin; fin.open("infilename.txt"); fin>>str; while (getline(fin, line)) { ... } fin.close(); ofstream fout; fout.open("outfilename.txt"); fout << str; fout.close(); fail(); exit(0); void myfunc(ifstream &myinfile); // passing by reference

17


Download ppt "CS Computer Science IA: Procedural Programming"

Similar presentations


Ads by Google