Reading Assignment



--------------------------------------------------------------------------------
example1
--------------------------------------------------------------------------------
/* in c and c++ when you declare an array you must use a constant for the size
   a constant is 5 or 489 , somthing that cannot be anything else. You cannot use a variable that 
   is not a constant, or at least that is the rules. Recent versions of gcc , ming and other compilers
   allow this now but it is NOT valid c++ in the strictest sense */

int main()
{
  int size=0;

  Prompt("how large would you like the array to be",size);
  /* size now contains an integer value */

  /* when compiling with the most recent version of GCC this is technically
	 possible but from the 80s untill 2011 you could ONLY use a constant as the size
     for creating a static array. trying this would not work and although it does compile
     it is not legitimate c++ */

  int array1[size];  // not valid c++ but with modern compilers it will compile



  /* this has been possible for many many years and this is the normal
	 mode to create a dynamic array , so this is how we are going to do it
	 in this class as this is the most common method
	 we use the new keyword and store the result into a pointer */

  int * array2 = new int[size]; // valid c++ size does not need to be a constant


  for (int loop=0;loop<size; loop++)
    {
	  array1[loop] = CreateRandomNumber(222,333);
      array2[loop] = CreateRandomNumber(222,333);
    }


  /* we could change the size of array2 if we needed to but
     array1 cannot be changed  */

  delete[] array2; // array2 was allocated with new so we must explicity delete it
  return 0;
}



--------------------------------------------------------------------------------
example2

we can change the size of a dynamically allocated array
here we will take an existing array, delete it and make a new one
we use the same pointer so in a way we have changed the size of our array 
--------------------------------------------------------------------------------

int main()
{
  int size = 5;
  int * array2 = new int[size];

  for (int loop=0;loop<5; loop++)
    array2[loop] = CreateRandomNumber(222,333);


  // now for some reason we need to make our array larger

  delete[] array2; // clean up our old array

  size *=2;  // double the variable size

  array2 = new int[size]; // set our pointer to a larger array


  delete[] array2; // array2 was allocated with new so we must explicity delete it


  return 0;
}


--------------------------------------------------------------------------------
example3

we can change the size of a dynamically allocated array
but what if we want to keep our data that is there already? 
well we need to copy it.. make a new array, copy our values over , set the pointer to the 
new array

--------------------------------------------------------------------------------

int main()
{
  int size = 5;

  int * array2 = new int[size];


  for (int loop=0;loop<5; loop++)
    array2[loop] = CreateRandomNumber(222,333);


  // now for some reason we need to make our array larger
  // create a temp pointer and allocate a 2nd larger array1
  int * temp = new int[ size *2 ];

  // copy the values from array2 to the second array
  for(int loop=0;loop < size;loop++)
    temp[loop] = array2[loop];

  // clean up our old array
  delete[] array2;

  // set array2 to point to the new array
  array2 = temp;


  // now we can add in 5 more values
  for (int loop=0;loop<5; loop++)
    array2[loop+5] = CreateRandomNumber(222,333);



  delete[] array2; // array2 was allocated with new so we must explicity delete it
  return 0;
}