CS1411 - Introduction to Programming Principles I

Programming / Lab Assignment 7

Warning: Starting last week there will be points deducted if the assignmnent is not submitted in the correct format. Please see the submission guidelines!

Smart Integer Array

You are supposed to write functions for a "smart integer array". These function will define and handle integer arrays.

Your program will consist of 3 Files: main.cpp, smartArray.h, smartArray.cpp. The main function should be in main.cpp, the struct definition and all function headers in smartArray.h and the function definitions in smartArray.cpp

Please protect you .h file agains multiple inclusion as seen in class and the book.

Use a struct similar to the following for your smart arrays:

struct smartArray {
  int *data;
  int count;
  int size;
}
Where data is a pointer to the actual data, count contains the number of elements, and size the size number of elements allocated

Write the following functions:

void init(smartArray &array) should initialize data to NULL, count and size to 0

void free(smartArray &array) should free the memory for data and then call init to reset all members

int count(smartArray array) should return the count member

void reserve(smartArray &array, int newSize) will do nothing if newSize is less than size. However, if newSize > size, this will:

  • Allocate a new int array with at newSize elements
  • If data!= NULL copy all the used elements (up to index count-1) into the new array
  • Free the memory for the old data array
  • Set data to point to the new memory array
  • Update size to have the newSize

void add(smartArray &array, int element) first, checks if there is enough space in the array: if size==count then calls reserve(array,count+10) to reserve more space. Then adds the new element at index count, and increases count.

int elementAt(smartArray array, int index) returns element at index index. Uses assertions to make sure index >= 0 and index < count.

void setElementAt(smartArray &array, int index, int newValue) sets element at index index to newValue. If index == count it uses add function to ensure that there is enough space. Uses assertion to make sure index >= 0 and index <= count.

void printElements(smartArray &array) outputs all the elements from the array to the screen, in one line, separated by a space

The only part that will be graded is smartArray.cpp and smartArray.h. You may use additional functionality in you main function / file and other files if you need it for testing

Use a main function simlar to the following to test your program. You may use a different main function if all the functionality can be tested with it:

int main()
  smartArray a;
  int i;
  init(a);
  add(a,1);
  add(a,2);
  setElementAt(a,2,3);
  cout << elementAt(a,1) << endl;
  setElementAt(a,0,3);
  printElements(a);
  cout << endl;
  for (i=0;i<25;i++) add(a,i);
  printElements(a);
  free(a);
  return 0;
} 

Your programs should include comments whith you name, testnumber, etc. in the top. See also the Submission guidelines

The assignment is due Friday Oct 22, 6pm. You have to submit it electronically as stated in the submission guidelines!

Lost? See the Help! page.