void TestObject::reverseArray() { for (int i = 0; i < m_numberOfStrings / 2; i++) { swapStrings(m_arrayOfStrings[m_numberOfStrings - i - 1], m_arrayOfStrings[i]); } }
// Problem 4: (20 points) // Use the selection sort algorithm to alphabetically sort the strings in the 2D character array "strings". // If you are unfamiliar with selection sort, here is a visual example: https://en.wikipedia.org/wiki/Selection_sort // NOTE: 'A' and 'a' should be considered alphabetically equivalent. // NOTE: You MUST use the selection sort algorithm for your program to produce the correct output. // NOTE: You MUST incorporate your "swapStrings" function to recieve full points for this part. // See output provided in word document for example input and output. void sortStrings(char strings[5][32]) { /* selection sort */ int i, j; i = j = 0; for(; i < 5; i++) { for(j = i; j < 5; j++) { if(strings[i][0] > strings[j][0]) { swapStrings(strings[i], strings[j]); } } } }
// Problem 3: (10 points) // hw04 update: Rewrite this function to perform the same task, using only pointer operations. // You must use pointer operations only. If you use array operations, you will recieve no credit for this part. // You can use the swapStrings() function if you'd like, but are not required to do so. // You may modify the code you submitted for hw03 or the solution code. // // Use any sorting algorithm to alphabetically sort the strings in the 2D character array "strings". // NOTE: 'A' and 'a' should be considered alphabetically equivalent. // See output provided in word document for hw03 for example input and output. void sortStrings(char strings[5][32]) { char *t, *s; t = s = NULL; int i = 0, j; /* Insertion sort */ for(i = 0; i < 5; i++) { t = strings[i]; for(j = i; j < 5; j++) { s = strings[j]; if(strcmp(t, s) > 0) { swapStrings(t, s); } } } }