1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
// STEP 1:
// Complete the following function as a recursive function
// sumInts:
// sum of the positive integers <= argument n
// Parameter:
// n (integer input) largest integer to include inah s the sum
int sumInts( int n )
{
return -5;
}
// STEP 2:
// Complete the following function as a recursive function
// sumSquares:
// sum of the squares of the positive integers <= argument n
// Parameter:
// n (integer input) largest integer whose square is included in sum
int sumSquares( int n )
{
return 3;
}
// STEP 3:
// Complete the following function as a recursive function
// sumArray:
// sum of the first elements in an array
// Parameters:
// arr (integer array input) array containing values to add up
// n (integer input) number of elements to include in the sum
int sumArray( const int arr[], int n)
{
return 10;
}
// step 4:
// Complete the following function as a recursive function
// intPower
// raises a value to a power that is an integer
// Parameters:
// base (double input) base to multiply as needed
// exponent (integer input) exponent to use
// NOTE: The exponent may be Negative -- but that is an easy
// adjustment to the value you get from a Positive exponent
// (and can use a recursive call to get that)
double intPower( double base, int exponent)
{
return 3.14;
}
// STEP 5:
// Compile and run your program to see how well it does.
// STEP 6:
// Complete the following function as a recursive function
// fibonacci
// returns a number from the fibonacci series:
// f(0) = 1; f(1) = 1; f(2) = 2; f(3) = 3; f(4) = 5
// each successive element is the sum of the preceding two
// Parameter:
// pos (integer input) desired position in Fibonacci sequence
int fibonacci( int pos )
{
return 6;
}
// STEP 7:
// Complete the following function WITHOUT recursion
// fibArray
// fills an array with the fibonacci sequence, as described above
// Paramters:
// arr (integer array output) array to fill
// n (integer input) number of array elements to fill
void fibArray( int arr[], int n )
{
}
// STEP 8:
// Compile and run your code to see how it performs
// STEP 9:
// Compute the following problem using an array like STEP 7 did.
// changeWays:
// counts the number of sequences of dimes and quarters
// that add up to a particular total
// e.g. 60 cents can be supplied four different ways:
// 25+25+10, 25+10+25, 10+25+25, 10+10+10+10+10+10
// Hint: How many ways can I get my result if I first insert a dime?
// How many ways can I get my result if I first insert a quarter?
int changeWays( int cost )
{
int arr[100] = {0}; // enough array spaces for up to 500 cents
// To make best use of this array, divide values by 5
// so 75 cents would be represented at subscript 75/5
return 3;
}
// STEP 10:
// See if your last answer worked, and in a reasonable amount of time
int main()
{
int sample[10] = { 2, 3, 5, 7, 1, 9, 4, 8, 6, 10 };
int fibList[50];
int tick, tock; // for timing fibonacci
int fib; // fibonacci answer
cout << "The sum of the first 5 integers is 15 = " << sumInts(5) << endl;
cout << "The sum of the first 9 integers is 45 = " << sumInts(9) << endl << endl;
cout << "The sum of the first 3 squares is 14 = " << sumSquares(3) << endl;
cout << "The sum of the first 7 squares is 140 = " << sumSquares(7) << endl << endl;
cout << "The first four prime numbers add up to 17 = " << sumArray( sample, 4 ) << endl;
cout << "The sum of the first 10 integers is 55 = " << sumArray( sample, 10 ) << endl << endl;
cout << "2 to the 3rd power is 8 = " << intPower( 2.0, 3 ) << endl;
cout << "4 to the 0th power is 1 = " << intPower( 4.0, 0 ) << endl;
cout << "5 to the 5th power is 3125 = " << intPower( 5.0, 5 ) << endl;
cout << "10 to the -3 power is 0.001 = " << intPower( 10.0, -3 ) << endl;
cout << "0.1 to the -3 power is 1000 = " << intPower( 0.1, -3 ) << endl << endl;
tick = clock();
fib = fibonacci( 10 );
tock = clock();
cout << "Computed fibonacci(10) = 89 = " << fib << " in " <<
((tock-tick)*1.0/CLOCKS_PER_SEC) << " seconds" << endl;
tick = clock();
fib = fibonacci( 40 );
tock = clock();
cout << "Computed fibonacci(40) = " << fib << " in " <<
((tock-tick)*1.0/CLOCKS_PER_SEC) << " seconds" << endl;
tick = clock();
fibArray( fibList, 50 );
tock = clock();
cout << "Filled 50 element fibonacci array in " <<
((tock-tick)*1.0/CLOCKS_PER_SEC) << " seconds" << endl;
cout << "Fib[10] = " << fibList[10] << " and fib[40] = " << fibList[40] << endl << endl;
cout << "There are " << changeWays( 60 ) << " ways to come up with 60 cents." << endl;
cout << "There are " << changeWays( 15 ) << " ways to come up with 15 cents." << endl;
cout << "There are " << changeWays(300 ) << " ways to come up with 3 dollars." << endl;
return 0;
}
|