C Practice Final

Summary

Fill in the code in practice_final.cpp
You are allowed full access to your prior work, internet searches, and all course materials.

Setup

Download  practice_final.zip. A Makefile (with gdb debug support) is included for your convenience.
Use wget and unzip in your terminal window to accomplish downloading the files.

Run the make by simply typing make:
make

Run the unit tests. (To get full credit your code must pass the unit tests and instructor code inspection):
./test
Run the valgrind utility on your code to make sure you do not have any memory leaks:
valgrind ./test

Submission

Place your completed practice_final.cpp files in a directory named practice_final at the top level of your git repo. This is optional as this practice exam will not be graded

Grading

Practicum grading is typically:

Review

  1. Arrays can be declared with an explicit size:
    char buffer[MAXSIZE+1] ;
      // hold a string of at most MAXSIZE characters + terminating NUL ('\0')

  2. Arrays can be initialized, which also sets the array's size and the initial contents:
    char mesg[] = "Hello!" ;  // a 7 element array - 6 characters in Hello! + terminating NUL

  3. An array name is a constant pointer to the first (0th) array element; thus:
    mesg == &mesg[0] ;  // address of the first character in the message.
    mesg[0] == *mesg ;  // the 0th element of an array is the same as dereferencing mesg as a pointer.

  4. Pointer variables (of the appropriate type) can be point to an array declaration:
    char *pmesg = mesg ;    // *pmesg == mesg[0] == *mesg == 'H'

  5. Pointers can be dereferenced to get or set the value they point to:
    char ch ;
    ch = *pmesg ;  // ch == 'H'
  6. Pointers can be indexed like arrays  (rarely done but useful at times):
    ch = pmesg[4] ;  // ch == 'o'

  7. Pointers can have integers added or subtracted to generate new pointers:
    char *pnew = NULL ;
    pnew = pmesg + 4 ;  // pnew points to the fourth character past pmesg (here, *pnew == 'o')

  8. Pointer difference gives the "distance" in underlying type units between two pointers:
    int dist = 0 ;
    dist = pnew - pmesg ;  // dist == 4

  9. ++ and -- work on pointers, adjusting them by the size of the underlying type:
    p++ ;           // increment p; points to the next character
    q-- ;           // decrement q; points to the previous character
    dist = q - p ;  // dist == 2
  10. Pointer can combine both dereferencing and pointer increments and decrements in one statement:
    pmesg = mesg ;
    ch = *pmesg++ ;     // ch == 'H' and pmesg == &mesg[1]
    pnew = mesg + 4 ;
    ch = *pnew-- ;     // ch == 'o' and pnew == &mesg[3]

    pnew = mesg ;
    ch = *++pmesg ;     // ch == 'e' and pmesg == &mesg[1]
    pnew = mesg + 4 ;
    ch = *--pnew ;     // ch == 'l' and pnew == &mesg[3]