// CIS 235 Exercise 5B - write and test the following functions // - function names ending in I - use loops // - function names ending in R - use recursion, NO LOOPS #include using namespace::std; // NOTE: const char * p denotes a C++ string // which is an array of characters ending in the NULL character '\0' int productI(const int data[], int cellsUsed); int productR(const int data[], int cellsUsed); int countCharI(const char * p, char lookFor); int countCharR(const char * p, char lookFor); int raiseToPowerI(int base, int exponent); // assume exponent > 0 // raiseToPower(2,4) returns 2*2*2*2 ==> 16 int raiseToPowerR(int base, int exponent); // assume exponent > 0 bool equalI(const char * left, const char * right); // are the 2 C++ strings equal or not bool equalR(const char * left, const char * right); // are the 2 C++ strings equal or not char largestI(const char * p); char largestR(const char * p); void main() { int info[5] = { 4, 8, 2, 9, 6}; cout << "Testing product" << endl << productI(info, 5)<< endl << productR(info, 5 ) << endl; cout << "Testing countChar" << endl << countCharI("mississippi", 'i') << endl << countCharR("mississippi", 'i') << endl; cout << "Testing raiseToPower" << endl << raiseToPowerI(5,4) << endl << raiseToPowerR(5,4) << endl; cout << "Testing equalI" << endl << equalI("abcde", "abcde") << endl << equalI("abxde", "abcde") << endl << equalI("abc" , "abcde") << endl << equalI("abcde", "abc" ) << endl << equalI("abcde", "abcdg") << endl; cout << "Testing equalR" << endl << equalR("abcde", "abcde") << endl << equalR("abxde", "abcde") << endl << equalR("abc" , "abcde") << endl << equalR("abcde", "abc" ) << endl << equalR("abcde", "abcdg") << endl; cout << "Testing largestI" << endl << largestI("xabcde") << endl << largestI("abxcde") << endl << largestI("abcdex") << endl; cout << "Testing largestR" << endl << largestR("xabcde") << endl << largestR("abxcde") << endl << largestR("abcdex") << endl; return; } // ****************************************************************** int productI(const int data[], int cellsUsed) { int prod = 1; for( int i = 0; i < cellsUsed; i++) { prod = prod * data[i]; } return prod; } int productR(const int data[], int cellsUsed) { if ( cellsUsed == 0 ) return 1; return data[0] * productR(data + 1, cellsUsed - 1); } int countCharI(const char * p, char lookFor) { const char * pt; int count = 0; for( pt = p ; *pt; pt++) // *pt is the same logic as *pt != '\0' { if ( *pt == lookFor ) count++; } return count++; } int countCharR(const char * p, char lookFor) { const char * pt = p; if ( *pt == '\0' ) return 0; return (*pt == lookFor?1:0) + countCharR(p + 1, lookFor); } int raiseToPowerI(int base, int exponent) // assume exponent > 0 { int prod = 1; for( int i = 1; i <= exponent; i++) { prod = prod * base; } return prod; } int raiseToPowerR(int base, int exponent) // assume exponent > 0 { if ( exponent == 1 ) return base; return base * raiseToPowerR(base,exponent - 1 ); } bool equalI(const char * left, const char * right) { // check for very special case, the two parameters are the same, i.e. they hold the same addresses if ( left == right ) return true; // HERE I really want to compare addresses, not data const char * pl = left; const char * pr = right; while ( *pl && *pr ) // continue while both pointers dereferenced are not the null char '\0' { if( *pl != *pr ) return false; pl++; // adjust the address to point to the next character pr++; } // exit the loop, at least one pointer points to the null char // if the C++ strings are the same length, both pointers should point to the null char return *pl == *pr; } bool equalR(const char * left, const char * right) { // check for very special case, the two parameters are the same, i.e. they hold the same addresses if ( left == right ) return true; // HERE I really want to compare addresses, not data if ( *left != *right ) return false; // HERE I am comparing data if ( *left == '\0' && *right == '\0' ) return true; // Both C++ strings have reached the null character return equalR(left+1, right+1); // compare the two C++ strings after the current first character } char largestI(const char * p) { char largest = '\0'; for ( int i = 0; p[i] != '\0'; i++) // HERE I AM USING SUBSCRIPTS, NOT POINTERS ==> PREFER POINTERS { if ( p[i] > largest ) largest = p[i]; } return largest; } char largestR(const char * p) { if ( *p == '\0' ) return '\0'; // Empty C++ string, so return null char char largestToRight = largestR(p + 1); // find the largest char to the right of the current char; return (*p >= largestToRight ? *p : largestToRight); } /* Testing product 3456 3456 Testing countChar 4 4 Testing raiseToPower 625 625 Testing equalI 1 0 0 0 0 Testing equalR 1 0 0 0 0 Testing largestI x x x Testing largestR x x x Press any key to continue . . . */