Topic 1: Help with P5 (as requested) * hints on Bubble Sort * 2 For Loops * outer loop: N-1 passes * inner loop: pair-by-pair comparisons and swaps ------------------------------------------------------------ Topic 2: Pointer Notation: Review Consider this code: int x; int *p1, *p2, *p3; int list[4]; p1 = &list[2]; p2 = &x; p3 = list + 1; //not shown: code to give list[0-3] and x values ... Here is the resulting view of memory: values addresses ----------------- x | 1000 | 200 ----------------- ... ... ----------------- p1 | 502 | 300 ----------------- p2 | 200 | 301 ----------------- p3 | 501 | 302 ----------------- ... ... ----------------- list[0] | 10 | 500 ----------------- list[1] | 20 | 501 ----------------- list[2] | 30 | 502 ----------------- list[3] | 40 | 503 ----------------- What are the values of the following variables and expressions? *p1 *p2 *p3 *(p2 + 1) list *list list + 2 *(list +2) &p1 p3[2] *(p3-1) ------------------------------------------------------------ Topic 3: Calling a function on part of an array. * Consider this function: int sumArray(int list[], int n) { int i, sum = 0; for (i = 0; i < n; i++) sum += list[i]; return sum; } * Now consider the main function: int main() { int scores[10]; int sum1, sum2; /* not shown: code to fill up array */ sum1 = sumArray(scores, 10); sum2 = sumArray(scores+2, 8); ... } * notes: * scores+2 is the address of scores[2], equivalent to &scores[2] * this is memory just after the 2nd call: ----------------- scores[0] | | 50 ----------------- scores[1] | | 51 ----------------- scores[2] | | 52 ----------------- scores[3] | | 53 ----------------- ... list ------------------ | 52 | 100 ------------------ * thus, the function simply starts its loop at scores[2], instead of at scores[0] ------------------------------------------------------------ Demonstration: 2 ways to write a function called fill that inserts the same value "num" into each element of an array. Exercise 1: void fill(int list[], int n, int num) { int i; for (i = 0; i < n; i++) list[i] = num; } Exercise 2: void fill(int *p, int n, int num) { int i; for (i = 0; i < n; i++) { *p = num; p++; } } ------------------------------------------------------------ * * * * *