// ============================================ // Week 5: Functions (II) (p. 3) void swap (int x, int y); int main() { int a = 10, b = 20; cout << a << " " << b << endl; swap(a, b); cout << a << " " << b << endl; } void swap (int x, int y) { int temp = x; x = y; y = temp; } // ============================================ // Week 5: Functions (II) (p. 6) void shiftArray (int [], int); int main() { int num[5] = {1, 2, 3, 4, 5}; shiftArray(num, 5); for (int i = 0; i < 5; i++) cout << num[i] << " "; return 0; } void shiftArray (int a[], int len) { int temp = a[0]; for (int i = 0; i < len - 1; i++) a[i] = a[i + 1]; a[len - 1] = temp; } // ============================================ // Week 5: Functions (II) (p. 9) void printArray (const int [5], int); int main() { int num[5] = {1, 2, 3, 4, 5}; printArray(num, 5); return 0; } void printArray (const int a[5], int len) { for (int i = 0; i < len; i++) cout << a[i] << " "; cout << endl; } // ============================================ // Week 5: Functions (II) (p. 20) #include using namespace std; int myMax (int [], int); int main () { int a[5] = {7, 2, 5, 8, 9}; cout << myMax (a, 5); return 0; } int myMax (int a[], int len) { int max = a[0]; for (int i = 1; i < len; i++) { if (a[i] > max) max = a[i]; } return max; } // ============================================ // Week 5: Functions (II) (p. 27) // myMax.h const int LEN = 5; int myMax (int [], int); void print (int); // myMax.cpp #include using namespace std; int myMax (int a[], int len) { int max = a[0]; for (int i = 1; i < len; i++) { if (a[i] > max) max = a[i]; } return max; } void print (int i) { cout << i; } // main.cpp #include #include "myMax.h" using namespace std; int main () { int a[LEN] = {7, 2, 5, 8, 9}; print (myMax (a, LEN)); return 0; } // ============================================ // Week 5: Functions (II) (p. 31) #include #include using namespace std; int main() { int rn = 0; for (int i = 0; i < 10; i++) { rn = rand(); cout << rn << " "; } return 0; } // ============================================ // Week 5: Functions (II) (p. 33) #include #include using namespace std; int main() { srand(0); int rn = 0; for (int i = 0; i < 10; i++) { rn = rand(); cout << rn << " "; } return 0; } // ============================================ // Week 5: Functions (II) (p. 35) // left #include #include #include using namespace std; int main() { srand(time(0)); // good int rn = 0; for (int i = 0; i < 10; i++) { rn = rand(); cout << rn << " "; } return 0; } // right #include #include #include using namespace std; int main() { int rn = 0; for (int i = 0; i < 10; i++) { srand(time(0)); // bad rn = rand(); cout << rn << " "; } return 0; }