main() functions. Write the simplest
main() needed to get the job done. The point here
is not to write complete programs, but to exercise your
problem-solving skills. In this context, main() is
merely a tool.
F(0) = 1 F(1) = 1 F(n) = F(n-1) + F(n-2), for n > 1How many function calls of the form F(something) are made when computing F(5)? How many times are F(1) and F(0) called for? In your solution, show a trace of the execution of this function call. Here's an example of tracing the execution of F(3): (Notice the indentation pattern; it will help you understand what's happening.)
F(3) calls F(2)
F(2) calls F(1)
F(1) returns 1
F(2) calls F(0)
F(0) returns 1
F(2) returns (1+1) = 2
F(3) calls F(1)
F(1) returns 1
F(3) returns (2+1) = 3
So, the call to F(3) generates 1 call to F(2), 2 calls to
F(1), and 1 call to F(0), for a total of 4 additional F calls
(besides the call to F(3) that originated the rest).
int pow(int x, int n)
{
return x * pow(x, n-1);
}
6. [3 points] Suppose you're given an array of
integers. A recursive way to find the smallest element in the
array is to compare the smallest element of the first
half of the array with the smallest element of the second
half of the array and return the smallest of those two
numbers. The recursive nature of this solution comes from the
fact that finding the smallest element of a given half of the
original array (either the lower half or the top half) is
exactly the same problem as the original problem, only now it's
applied to a smaller array. For example, a trace of the
execution of this algorithm (call it recMin) on the array
{ 73, -21, -22, 48, 0, 724, 214, -93, 1721, 4 } goes as follows:
recMin on (73, -21, -22, 48, 0, 724, 214, -93, 1721, 4)
calls recMin on (73, -21, -22, 48, 0)
calls recMin on (73, -21, -22)
calls recMin on (73)
returns 73
calls recMin on (-21, -22)
calls recMin on (-21)
returns -21
calls recMin on (-22)
returns -22
returns min(-21, -22) = -22
returns min(73, -22) = -22
calls recMin on (48, 0)
calls recMin on (48)
returns 48
calls recMin on (0)
returns 0
returns min(48, 0) = 0
returns min(-22, 0) = -22
calls recMin on (724, 214, -93, 1721, 4)
calls recMin on (724, 214, -93)
calls recMin on (724)
returns 724
calls recMin on (214, -93)
calls recMin on (214)
returns 214
calls recMin on (-93)
returns -93
returns min(214, -93) = -93
returns min(724, -93) = -93
calls recMin on (1721, 4)
calls recMin on (1721)
returns 1721
calls recMin on (4)
returns 4
returns min(1721, 4) = 4
returns min(-93, 4) = -93
returns min(-22, -93) = -93
Write a recursive function int recMin(int a[],
unsigned int begin, unsigned int end) which returns the
smallest element of the integer array a using the idea
described above. Also write a main function to test your
recursive function.
The press() function should flip the button's
on/off state, then print the button's label and whether the
button is now on or off. Answer the following questions:
isOn ?
© 2003 UC Riverside Department of Computer Science & Engineering. All rights reserved.