COP 3223H meeting -*- Outline -*- * Friday problems with C pointers ** capturing patterns of assignments *** initialize an array ------------------------------------------ FOR YOU TO DO write a function extern void set_all_array(double *a, int sz, double val); that takes an array a of doubles, an int sz, and a double val such that all i from 0 to sz-1 are legal indexes and val is not NaN, and initializes each element with index 0..sz-1 to the value val. ------------------------------------------ ** Code that uses swap *** reverse an array ------------------------------------------ FOR YOU TO DO Write a function extern void reverse(char *s); that takes a null-terminated string s, and reverses it in place. So, if the length of s is len, then at the end *s is the old value of *(s+len-1) and vice versa *(s+1) is the old value of *(s+len-2) and vice versa *(s+2) is the old value of *(s+len-3) and vice versa etc. For example: char str[] = {'U', 'C', 'F'}; reverse(str); ok(strcmp(str,"FCU") == 0); ------------------------------------------ ... #include #include "reverse.h" // assigns: *x, *y; // effect: *x == old(*y) and *y == old(*x); void cswap(char *x, char *y) { char temp = *y; *y = *x; *x = temp; } // requires: s is a null-terminated string // let int len = strlen(s); // ensures: for all i: 0 <= i && i < len: s[i] == old(s[len-1-i]); void reverse(char *s) { int len = strlen(s); char *front, *rear; for (front = s, rear = s+(len-1); front < rear; front++,rear--) { cswap(front, rear); } } *** insertion sort ------------------------------------------ FOR YOU TO DO Write a function, extern bool palindrome(const char *sentence) that takes a string, sentence, and returns true just when sentence is a palindrome, so that it reads the same forwards and backwards, ignoring space and punctuation. Note that palindrome may not change any characters in its argument. ------------------------------------------