// Illustrates how to declare an array dynamically, // and how to delete the array. #include #include using namespace std; void MakeArray(int*& newArray, const int Size); void InitArray(int A[], const int Size); void PrintArray(const int A[], const int Size); void DelArray(int*& newArray); void main() { int *myArray = NULL; int mySize; // Get dimension of array from user: cout << "Enter dimension of array: "; cin >> mySize; cout << endl << endl; // Allocate array dynamically using new operator: MakeArray(myArray, mySize); // Initialize array: InitArray(myArray, mySize); // Print array: PrintArray(myArray, mySize); // Deallocate array storage: DelArray(myArray); } // QTP 1: What does the syntax *& mean about the first parameter? // QTP 2: Is that necessary in order for this function to work // correctly? Why? void MakeArray(int*& newArray, const int Size) { newArray = new int[Size]; } // QTP 3: What happens if the second actual parameter is too large? void InitArray(int A[], const int Size) { for (int idx = 0; idx < Size; idx++) { A[idx] = idx; } } void PrintArray(const int A[], const int Size) { cout << "Idx Address Value" << endl; for (int idx = 0; idx < Size; idx++) { cout << setw( 3) << idx << setw(12) << &A[idx] << setw( 5) << A[idx] << endl; } } void DelArray(int*& newArray) { delete [] newArray; }