CSSE 120 -- Intro. to Software Development

Homework 27

  1. Don't forget to refer to the Python vs C comparison document. You may find this helpful as you try to do things in C that you already know how to do in Python. Feel free to suggest things that we might add to this document.
  2. Complete the assigned reading for the next session: Schildt: Pages 230-235 (File input and output) Pages 254-255 (File parsing)
  3. (18 pts) Complete the Angel quiz over the reading assignment. You'll find the quiz on the course Angel page, under Lessons → Homework → Homework 27 → Files

    The IntArray Program is due at the 11:59 PM on Friday of 10th week, but there are also milestones that are due at the beginning of Sessions 28 and 29:

    Before Session 28 (10 points for having it done on time): 
    Complete everything through part i of question 5.

    Before Session 29 (10 points for having it done on time): 
    Complete everything through part p of question 5.

    You must do this assignment using Eclipse and the SmarterArrays project that you checked out from your individual SVN repository in class. Be sure that you checked out that project into your Eclipse C workspace, not your Python workspace.

    You should commit after completing each part, and include the letter of the part you completed in your commit message. This helps us award credit for each function you finish.

    Overview: An IntArray is "smarter" than a normal C array.  It is a start toward implementing something that behaves more like a Python list.  In particular, an IntArray knows its size, can dynamically change its size, can disallow attempts to do something with an "element" whose index beyond its size, and allows insertion into the middle, and automatically allocates more space when needed.  It is your job to implement these (and some other) behaviors.  A C array is not sufficient to keep track of everything we need to know, so we created a simple struct type that you will use.  It has a member called length that you will use to keep track of the list's current size.
  4. (3 points) In the source file main.c, edit the initial comment to include your name and today's date.
  5. (228 points, in addition to the 20 milestone-completion points discussed above) Stubs for all of the functions have been provided for you in main.c. The file also contains specifications of each function in comments.

    We've provided all of the test code in the functions main() and runTests(). We've included return statements before each test so that you can get one set of tests working before moving on to the next.

    The file expected.txt in the project gives the expected output for parts a through g. You can use it to check your work. Note that some of the values printed are uninitialized, and so will be different on your computer and between runs.

    You are to complete the definitions of all of the functions below. The tests are written expecting that you will complete them in the order listed. For each part, you'll need to comment out a return statement to allow the tests for that part to run.

    1. (15 points) Implement makeIntArray(), printArray(), and freeIntArray(). We will begin these together in class.
    2. (12 points) Make sure your "constructor", printing, and freeing work for a range of sizes, including size 0.
    3. (10 points) Implement makeZeroedIntArray().
    4. (12 points) Implement set() and get() functions for mutating and accessing IntArrays. For this part, you do not need to check to see that the indexes are in bounds (i.e., non-negative and not larger than the array size). Your functions should initially return FALSE.
    5. (8 points) Add bounds checking to set() and get(). Your functions should print an error and return TRUE if an out-of-bounds index is passed to the function, and return FALSE otherwise.
    6. (25 points) Implement pascalsTriangle().
    7. (10 points) Stress tests. Comment out the return 0; statement in the main() function. This will allow a memory stress test to run. The stress test calls your pascalsTriangle() function with larger and larger values, doubling in size each time. If your program has a memory leak, this stress test will detect it.

      You will probably want to comment out the body of printArray() for this test also, otherwise your program will spend lots of time printing and it will take much longer to reach large memory sizes.

      In our tests, when we failed to free memory correctly in pascalsTriangle(), the program "died" at a stress test size of 40960, with the error message unable to allocate enough memory for array of size 22563. Without the memory leak, our program made it to a stress test size of over 2.6 million before we stopped it.

      Add a comment near the end of main() indicating how large a stress test size your program reached.

    8. (6 points) Reflection. On the next homework, we will extend the idea of SmarterArrays to create lists that have more of the capabilities of lists in Python. Think about how you might change our IntArray struct so we could create initially empty IntArrays and append to them. Don't change your code, but add a comment near the end of main() describing what you might do.
    9. (0 points) Before submitting your final version, uncomment the return line before the stress test, to keep the stress test from running. Also uncomment the body of printArray() so the grader can check the functionality of your functions. Commit your final version to your SVN repository.


    10. End of first milestone


      Next we add extra functionality to some existing code.

    11. (4 points) Edit the IntArray struct to add an additional field, capacity. For our updated IntArray, the length field represents the size (number of items actually in the list). The capacity represents how many items the list could hold before we have to make it bigger.
    12. (4 points) Update makeIntArray() and makeZeroedIntArray() so that they set both the length and the capacity to the given size.

      Make sure all your previous functions, apart from the memory stress test, still work. The tests for  resizeIntArray(),should print a bunch of empty arrays. The other tests should be unchanged.

      Next we'll add some new functionality.
    13. (20 points) Implement resizeIntArray() It should change the capacity of the IntArray, but not its size.
          void resizeIntArray(IntArray *a, int newsize)
      If newsize is smaller than the current length (not capacity) of *a, this function should do nothing.
    14. (10 points) Add a function
          IntArray makeEmptyIntArray()
      
      that takes no arguments and returns an empty (size is 0) IntArray with an initial capacity of 5.
    15. (15 points) Write a function

          void append(IntArray* a, int newElement) 
      
      to add the given element to the end of the given array. You will need to adjust the size. If the array has reached its capacity, use your resizeInArray() function to double the array's capacity.

      Doubling gives us room for future growth. If we just grew by one, we would have to keep growing with every additional append, which is really inefficient.

    16. (6 points) Write a function
          int* getMemoryLocation(IntArray* a)
      
      that returns the address of the internal array.
    17. (6 points) Write functions
          int getCapacity(IntArray* a)
          
          int getSize(IntArray* a)
      
      that do what their names suggest.


    18. End of second milestone


    19. (20 points) Add a function
             boolean insert(IntArray* aPtr, int index, int value) 
      that works like Python's insert method. That is, the given value should be inserted into the given array and all subsequent values in the array should be moved to the next higher index. For full credit, your program must resize the array to make room for the additional element when that is necessary. The return value signifies whether there was an index out-of-bounds error. For full credit, your program must check for out-of-bounds indexes and return TRUE if (and only if) so, refusing to do the insertion. Add code to main() to test your new function.

      Clarification of "out of bounds": given an IntArray of size 4, attempting to insert at position 3 is OK, it will push the old element in position 3 back one. Attempting to insert at position 4 is OK, since it will do the equivalent of appending. But attempting to insert at position 5 is bad, since there would be a gap between the old array and the new element inserted.
    20. (25 points) Create a driver function that will read a bunch of integers from the console and store them in an IntArray created using makeEmptyIntArray() and extending using append().

      After each integer is read, you should print the memory address of the internal array, its current size, its current capacity, and the integer contents of the array (on a single line).

      For example, a run might look like:

      stored at 003D3D80, with size = 1 and capacity = 5, contents = [3]
      stored at 003D3D80, with size = 2 and capacity = 5, contents = [3,7]
      stored at 003D3D80, with size = 3 and capacity = 5, contents = [3,7,8]
      stored at 003D3D80, with size = 4 and capacity = 5, contents = [3,7,8,2]
      stored at 003D3D80, with size = 5 and capacity = 5, contents = [3,7,8,2,4]
      stored at 003D5DF0, with size = 6 and capacity = 10, contents = [3,7,8,2,4,0]
      stored at 003D5DF0, with size = 7 and capacity = 10, contents = [3,7,8,2,4,0,12]
      stored at 003D5DF0, with size = 8 and capacity = 10, contents = [3,7,8,2,4,0,12,4]
      stored at 003D5DF0, with size = 9 and capacity = 10, contents = [3,7,8,2,4,0,12,4,4]
      stored at 003D5DF0, with size = 10 and capacity = 10, contents = [3,7,8,2,4,0,12,4,4,7]
      stored at 003D5E20, with size = 11 and capacity = 20, contents = [3,7,8,2,4,0,12,4,4,7,9]
      
    21. (20 points) Modify your function from the previous step so that it reads integers from a file instead. See the file reading code that we did in class for an example. Test it with the file ints.txt that is in your Eclipse project.
    22. BONUS (20 points): Write a function that works like printIntArray(), but instead of printing, returns what would be printed as a string. Your function's signature should be
          char* toString(IntArray* ar)
      
      Add code that uses your function to demonstrate that it works. Pay attention to the memory allocation issues that we have discussed and that you have worked with during this assignment.