// Practice for next week's lab quiz in which you'll be // asked to write several small functions that do things // with arrays. For today's work, please complete the // definitions of the four functions at the bottom of this // file. Thanks! // NOTE: A few more ideas for you to practice later in the week // are listed at the very bottom of this file! // Directives #include #include #include using namespace std; // Function prototypes: int max(int data[], int size); void copy(int source[], int target[], int size); int count(char x, char data[], int size); void reverse(int data[], int size); /////////////////////////////////////////////////////////////////////////////// int main( ) { int idata[12] = {-2, -8, -99, -1000, -1, -4, -33, -9, -33, -9, -4, -3}; char cdata[9] = {'a', 'b', 'c', 'd', 'e', 'f', 'e', 'h', 'i'}; int target[12]; int i; // A few very simple tests of the functions: cout << "The max value in idata is: " << max(idata, 12) << endl; copy(idata, target, 12); cout << "The values in the target array are now: " << endl; for (i = 0; i < 12; ++i) cout << target[i] << ' '; cout << endl; cout << "The char e appears this many times: " << count('e', cdata, 9); reverse(target, 12); cout << "After reverseing, the values in the target array are now: " << endl; for (i = 0; i < 12; ++i) cout << target[i] << ' '; cout << endl; return EXIT_SUCCESS; } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // The max function has an int array parameter called data. // The number of items in data is given by the size parameter, // which is guaranteed to be positive. // The return value is the largest element in the array. // Please make sure that your function works even if all // numbers in the array are negative! int max(int data[], int size) { // MICHAEL'S SOLUTION int i; // loop control int big; // Biggest seen so far big = data[0]; for (i=1; i < size; ++size) { if (data[i] > big) big = data[i]; } return big; } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // The copy function has two int array parameters called // source and target. The number of items in each array is // given by the size parameter, which could be as small as zero. // The work done by the function is to copy all the elements // from source into target. void copy(int source[], int target[], int size) { // MICHAEL'S SOLUTION int i; // loop control for (i = 0; i < size; ++i) { target[i] = source[i]; } } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // The count function has a char array parameter called data. // The number of items in data is given by the size parameter, // which may be as small as zero. The return value of the // function is the number of times the character x appears // in data (where x is the first parameter of the function). int count(char x, char data[], int size) { // MICHAEL'S SOLUTION int i; // loop control int answer = 0; // How many times x appeared for (i=0; i