/* This program demonstrates the use of dynamic memory allocation. * It also shows how to dynamically allocate and use a 2 dimensional array of strings. */ #include #include using namespace std; /* We can pass arrays into functions and receive them as pointers. * When we do this, we can either use the usual array notation with [] or just dereference the pointers. * If we do use pointers, we should also increment the pointers to have them move on to the next element. * This function swaps the contents of two equal sized integer arrays. */ void swap(int *a, int *b, int size) { int temp; for(int i=0; i< size; i++) { temp = *a; *a = *b; *b = temp; a++; b++; } } /* This function gets an array as a parameter and returns another array, where each element is the element of the old array multiplied by 5. * We cannot return whole arrays, but we can return pointers to arrays, and that's the same thing. * If the array was created in compile time (without new), this would cause issues if we try to dereference in main * Retruning a dynamically allocated array ensures that we have access to data outside the function as well. */ int * multiply (int *arr, int size) { int *b = new int[size]; // the new operator is used to dynamically allocate the new array. for(int i=0; i< size; i++) b[i] = arr[i] * 5; return b; } int main() { int count, max; cout<<"Enter the number of names: "; cin>>count; cout<<"Enter the size of each string: "; cin>>max; int arr[] = {5,12,15,-9}; // array fo integeres int *newarr = multiply(arr, 4); // The new array is returned here. We still have access to it. cout<<"The new array is: "<>newcount; cin.getline(st,10);//to get rid of trailing newline char ** newlist = new char*[newcount]; cout<<"Enter the extra names: "; for(int i=0; i