CS 13011 Computer Science IA Procedural Programming Final









![Arrays • Array declarations • int x[index]; • index must be integer and constant Arrays • Array declarations • int x[index]; • index must be integer and constant](https://slidetodoc.com/presentation_image/8277994e31c53d2ba62d52eabef4454f/image-10.jpg)







- Slides: 17

CS 13011 Computer Science IA: Procedural Programming Final Exam 1

C++ Basics • Variable • 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 ns 1; • using namespace ns 2; • Use namespaces to refer to variables (possibly with the same name) • ns 1: : x • ns 2: : x

Operations • Data types • 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?

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

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

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

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

Functions • Pre-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

Functions (cont'd) • Function prototype • int add 1(int i); • int add 1(int); • Function declaration • int add 1(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)
![Arrays Array declarations int xindex index must be integer and constant Arrays • Array declarations • int x[index]; • index must be integer and constant](https://slidetodoc.com/presentation_image/8277994e31c53d2ba62d52eabef4454f/image-10.jpg)
Arrays • Array declarations • int x[index]; • index must be integer and constant • 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 • for loop • sum, max, min, avg, search a key, etc.

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

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

Strings (cont'd) • Operators • +, += • 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

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(string 2)- appends string 2 to the end of the string • erase(start, number)- removes number of characters starting from start

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

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
