CMSC 104
Recursion & arrays Problem Set
1.) Write a recursive function power (base, exponent) that when invoked returns
exponent
base
For example, power(3,4) = 3 * 3 * 3 * 3
Assume that exponent is an integer greater than or equal to 1. Hint:
the recursion step would use the relationship
exponent ( exponent - 1 )
base = base * base
and terminating condition occurs when exponent is equal to 1 because
1
base = base
2.) Write C statements to accomplish each of the following:
Display the value of the seventh element of character array f.
Input a value into element 4 of single-subscripted floating-point array b.
Initialize each of the 5 elements of single-subscripted integer array g to 8.
Total the elements of floating-point array c of 100 elements.
Copy array a into the first portion of array b Assume float a[11], b[34];
Determine and print the smallest and largest values contained in 99-element floating point array w.
3.) Write single statements that perform each of the following single subscripted array operations:
Initialize the 10 elements of the integer array counts to zeros.
Add 1 to each of the 15 elements of integer array bonus.
Read the 12 values of floating point array monthlyTemperatures from the keyboard.
Print the 5 values of integer array bestScores in column format.
4.) Find the error(s) in each of the following statements:
a.)
char str[5];
scanf("%s", str); /* user prints hello */
b.)
int a[3];
printf("$d %d %d\n", a[1], a[2], a[3]);
c.)
float f[3] = { 1.1, 10.01, 100.01, 1000.111 };
d.)
double d[2][10];
d[1,9] = 2.345;
5.) What does the following program do?
#include
#define SIZE 10
int whatIsThis(Int [ ], int);
main()
{
int total, a[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
total = whatIsThis(a, SIZE);
printf("Total of array elemnt values is %d\n", total);
return 0;
}
int whatIsThis( int b[ ], int size )
{
if (size = = 1)
{
return b[0];
}
else
{
return b[size - 1] + whatIsThis(b, size - 1);
}
}