Lecture 1 Introduction

Programming Basic

building blocks in a C++ program:

the execution starts at the main method

Pasted image 20240204184528.png

Array (C++)

array: for storing multiple variables of the same type

int A[n]; // for small array
int* A = new int[n]; // for large array
int A[7]; // create an array
x = A[2]; // copy value A[2] to variable x
A[3] = 5; // store value 5 to A[3]

Pasted image 20240204185328.png

Control Flow (C++)

sequence (step by step)

selection (choose a branch)

if statement, switch statement
Pasted image 20240204190834.png

#include <iostream>
using namespace std;
 
int main ()
{
   // 局部变量声明
   char grade = 'D';
 
   switch(grade)
   {
   case 'A' :
      cout << "很棒!" << endl; 
      break;
   case 'B' :
   case 'C' :
      cout << "做得好" << endl;
      break;
   case 'D' :
      cout << "您通过了" << endl;
      break;
   case 'F' :
      cout << "最好再试一下" << endl;
      break;
   default :
      cout << "无效的成绩" << endl;
   }
   cout << "您的成绩是 " << grade << endl;
 
   return 0;
}

selection (choose a branch)

repeat the execution of the loop body (a sequence of statement) for multiple times

example: print "hello" 5 times

while loop:

int i=5;
while (i>0) {
	cout << "hello" << endl;
	i--;
}

for loop:

for (<init>; <cond>; <update>) {
   // body
}

// example
for (int i=0; i<5; i++) {
   cout << "hello" << endl;
}

how to solve a problem? by an algorithm?

a problem for human (in the real world)

sort poker cards on hand

computational problem

algorithms

Algorithm: a well defined sequence of steps for solving a computational problem

Selection-Sort (Array A[0..n–1])
1. for integer i ← 0 to n–2
2.     k ← i
3.     for integer j ← i+1 to n–1
4.         if A[k] > A[j] then
5.             k ← j
6.     swap A[i] and A[k]
void SelectionSort(int* A, int n) {
    int i, j, k, temp;
    for (i = 0; i < n–1; i++) {
        k = i;
        for (j = i + 1; j < n; j++) {
            if (A[k] > A[j])
                k = j;
        }
        int temp = A[i];
        A[i] = A[k];
        A[k] = temp;
    }
}

int main() {
    // read input
    SelectionSort(myarray, length);
    // write output
    // ...
}

Algorithm design paradigms

Incremental approach

Build a solution into a larger solution
E.g., we have a sorted subarray A[0..i–1], then append an item to obtain a sorted subarray A[0..i]

Recursive approach (or divide-and-conquer)

Recursive Approach: Simple Example

int F (int n) {
	if (n == 1) // base case
		return 1; 
	else 
		return n * F(n–1); // recursive case
}

Pasted image 20240204204539.png

Selection Sort

Example: recursive selection sort
Input: an subarray A[i..n–1] 5| 2| 4| 9| 7
Output : an subarray A[i..n–1] in the ascending order

Recur-Selection-Sort (A, i, n)
1. if i < n-1 then
2.     k ← i
3.     for integer j ← i+1 to n–1
4.         if A[k] > A[j] then
5.             k ← j
6.     swap A[i] and A[k]
7.     Recur-Selection-Sort (A, i+1, n) // a recursive call

Data structure

Let S be a set of items, and x be a search key

Foo Bar