#include #include using namespace std; //Answers written by Professor Jennifer B. Sartor void addLogo(char** myNameArr, int numRowToAdd, int numCols) { myNameArr[numRowToAdd] = new char[numCols]; myNameArr[numRowToAdd] = "is cool."; } template < typename T > T* createArray(int size, T firstElem) { T* array = new T[size]; array[0] = firstElem; return array; } int main() { /*Create a vector of chars, and copy each element of the char array above (myStr) into it. Then create an iterator, and use it to go through the vector and count the number of times that the character "e" occurs in the string. */ vector< char > vecStr (myStrLength); for (int j = 0; j < myStrLength; j++) { vecStr[j] = myStr[j]; } vector< char >::iterator myIter; int countE = 0; for(myIter = vecStr.begin(); myIter != vecStr.end(); myIter++) { if (*myIter == 'E') { countE++; } } cout << "The string has " << countE << " e's." << endl; /*Create an array of integers using dynamic allocation (new). Your array should be of length 10. Print out the elements (without initializing them), and then delete the memory you allocated. */ int* arrayInt = new int[10]; for (int i = 0; i < 10; i++) { cout << arrayInt[i] << " "; } cout << endl; delete [] arrayInt; /*IMPORTANT QUESTION. Create a 2 dimensional array of characters using dynamic allocation. Initialize the first row of the array to contain your first name, and the second row your last name. Then pass that array to a function that you define that adds another row that contains "is cool." Your initial array can be declared with 3 rows, but the function should allocate the last row.*/ char** myLogo = new char*[3]; for (int i = 0; i < 2; i++) { myLogo[i] = new char[10]; } myLogo[0] = "Jennifer"; myLogo[1] = "Sartor"; addLogo(myLogo, 2, 10); for (int i = 0; i < 3; i++) { cout << myLogo[i] << " "; delete [] myLogo[i]; } cout << endl; delete [] myLogo; /*What is the difference between the following 2 declarations? */ int* const intPtr1; const int* intPtr2; /*The first is a constant pointer to an int that is not constant. The second is a non-constant pointer to a constant int. We can do intPtr2++, but not intPtr1++. We can change *intPtr1 (the int that it points to), but not for *intPtr2. */ /*IMPORTANT QUESTION. Write a template function that takes an integer and one element of type T. The function creates an array of type T of the specified size (1st parameter), and initializes the first element of the array to the 2nd parameter. The function then returns the array. */ int* myIntArray = createArray(8, 8); cout << "Did the method initialize the first element of my int array to 8?: " << myIntArray[0] << endl; double* myDoubArray = createArray(8, 3.1415); cout << "Did the method initialize the first element of my double array to 3.1415?: " << myDoubArray[0] << endl; delete [] myIntArray; delete [] myDoubArray; //PROFESSOR SARTOR SAYS THAT THE ANSWERS TO THE FOLLOWING ARE DEPENDENT ON YOUR CLASS DEFINITION //PLEASE ASK IF YOU HAVE SPECIFIC QUESTIONS. /*Write a class definition for a car, with a few member variables, and some getter and setter methods for those member variables. Then, here in main, create an instance of that class, and call its methods.*/ /*Add a constructor to your car class. Does it also need a destructor? */ /*Divide your class into a separate .h and .cpp file. Make sure methods that should be constant are declared with "const". */ /*Create an object of your class using a pointer and dynamic allocation. */ return 0; }