CSSE 120 -- Intro. to Software Development

Homework 29 – due Friday, 11:59pm

  1. Take a look at 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. There is no Angel quiz for the next session.
  3. This assignment involves extending the SmarterArrays project that you worked on for Homework 28.
  4. This exercise involves extending the IntArray structure to implement more operations like in Python lists. First we have to update some existing code.
    1. (2 points) Edit the IntArray struct to add an additional fields, capacity. For our updated IntArray the length represents the number of items actually in the list. The capacity represents how many items the list could hold before we have to make it bigger.
    2. (2 points) Update makeIntArray() and makeZeroedIntArray() so that they set both the length and the capacity to the given size.
    3. (2 points) Update resizeIntArray() so it changes the capacity of the IntArray, but not its length.
    4. Make sure all your functions from Homework 28 , apart from the memory stress test, still work. The tests for part 5g of HW28, resizeIntArray(), should print a bunch of empty arrays. The other tests should be unchanged.
  5. Next we'll add some new functionality.
    1. (5 points) Add a function
          IntArray makeArray()
      
      that takes no arguments and returns an empty (length == 0) IntArray with an initial capacity of 5.
    2. (9 points) Write a function

          void append(IntArray* ar, int newElement) 
      
      to add the given element to the back of the given array. You will need to adjust the length. 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.

    3. (3 points) Write a function
          int* getMemoryLocation(IntArray* ar)
      
      that returns the address of the internal array.
    4. (4 points) Write functions
          int getCapacity(IntArray* ar)
          
          int getLength(IntArray* ar)
      
      that do what their names would suggest.
    5. (13 points) Create a driver function that will read a bunch of integers from the console and store them in an IntArray created using makeArray() and extending using append().

      After each integer is read, you should print the memory address of the internal array, its current length, 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 length = 1 and capacity = 5, contents = [3]
      stored at 003D3D80, with length = 2 and capacity = 5, contents = [3,7]
      stored at 003D3D80, with length = 3 and capacity = 5, contents = [3,7,8]
      stored at 003D3D80, with length = 4 and capacity = 5, contents = [3,7,8,2]
      stored at 003D3D80, with length = 5 and capacity = 5, contents = [3,7,8,2,4]
      stored at 003D5DF0, with length = 6 and capacity = 10, contents = [3,7,8,2,4,0]
      stored at 003D5DF0, with length = 7 and capacity = 10, contents = [3,7,8,2,4,0,12]
      stored at 003D5DF0, with length = 8 and capacity = 10, contents = [3,7,8,2,4,0,12,4]
      stored at 003D5DF0, with length = 9 and capacity = 10, contents = [3,7,8,2,4,0,12,4,4]
      stored at 003D5DF0, with length = 10 and capacity = 10, contents = [3,7,8,2,4,0,12,4,4,7]
      stored at 003D5E20, with length = 11 and capacity = 20, contents = [3,7,8,2,4,0,12,4,4,7,9]
      
    6. (10 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. Here is a file of integers that you should copy and paste into a new text file named "ints.txt" inside your Eclipse project.
    7. (8 points) Write and test a function
          int index(IntArray* ar, int value) 
      
      that searches the given array for the given value and returns the value's index if found. Make your code do something sensible if the value is not found.
    8. BONUS (10 points): Write a function that works like printIntArray(), but instead of printing, returns a string. Your function's signature should be
          char* toString(IntArray* ar)
      
      Add code that uses your function to demonstrate that it works.