The following exercises cover the topic of recursion and should help
you prepare for the in-lab activities. As with all prelabs/labs, if you
are
not sure of the answer you may go to any EWS lab on campus and check
your results by using the gcc compiler, or you can ask anyone of the
CS101 staff
for help. In order to understand the material needed to answer these
prelab questions please read the following material:
1. Fill in the blank below with the correct C code to dynamically
allocate an array named arr
that holds 20 elements of data type char and then copies the contents
of the vegetable array into arr .
char vegetable[] = "Rutabaga";
char * arr;
/* allocate space of 20 characters */
arr = malloc(___________________________________________);
/* copy the values of vegetable into into the new array */
strcpy( arr , vegetable);
2. Fill in the blanks below with the correct C code to dynamically
allocate an array named albums
that holds 1000 elements of datatype
Album.
typedef struct
{
char name[40]; /* track name or song name */
int length; /* in seconds */
} Track;typedef struct
Album * albums;
{
int cdno; /* cd number */
char title[30];
char artist[20];
int year;
int num_tracks; /* number of tracks */
int quantity;
float price;
float sales[MONTHS]; /* cd sales ($US) over the 12 months in 2005 */
Track tracks[20]; /* info for each track */
} Album;albums = calloc(_________________ , __________________________);
#include <stdio.h>
int mystery( int n)
{
if ( n <= 1) /* Voom! --- means base case (or simplest) case of the problem */
return n;
else
return mystery(n - 2); /* mystery is a recursive function because it calls itself */
}
void main(void)
{
printf(" %i ", mystery(1));
printf(" %i ", mystery(2));
printf(" %i ", mystery(3));
printf(" %i ", mystery(4));
printf(" %i ", mystery(5));
}
#include <stdio.h>
int euclid( int a, int b)<
{
if ( (a % b) == 0) /* if b divides into a without remainder ... Voom!*/
return b;
else
return euclid(b, a % b);
}
void main(void)
{
printf(" %i ", euclid(2,4));
printf(" %i ", euclid(2,5));
printf(" %i ", euclid(4, 16));
printf(" %i ", euclid(12,18));
}
#include <stdio.h>
int recFunc (int[], int);
void main (void)
{
int data[] = { 7, 6, 3, 8, 5, 9};
printf("%i \n", recFunc(data, 5));
}
int recFunc (int array[], int endpoint)
{
int temp;
if (endpoint == 0) /* Voom! */
return array[0];
temp = recFunc(array, endpoint-1);
if (temp >= array[endpoint])
return temp;
return array[endpoint];
}
int fibonacci(int n)
{
/* Voom! check whether n equals 0 or n equals 1 */
if ( _____________________ || ____________________ )
return ______________________ ; /* not 0 or 1 but something that depends on n */
/* else use the formula for Fn shown above */
else
return _______________________________________________ ;
}
. Pascal's formula,
written below
#include <stdio.h>
int binomial(int n, int k)
{
/* Voom! check whether n equals 0 or k equals 0 or n equals k */
if( _______________________________________________________________ )
return 1;
else
/* else use Pascal's formula shown above */
return binomial( n - 1 , __________________________________) +
binomial(________________________________________ , __________________________________);
}
void main(void)
{
printf("(0, 0) = %i \n", binomial(0,0)); /* prints 1 */
printf("(1, 0) = %i \n", binomial(1,0)); /* prints 1 */
printf("(1, 1) = %i \n", binomial(1,1)); /* prints 1 */
printf("(10, 10) = %i \n", binomial(10,10)); /* prints 1 */
printf("(3, 2) = %i \n", binomial(3,2)); /* prints 3 */
printf("(4, 2) = %i \n", binomial(4,2)); /* prints 6 */
}