Summary: In this laboratory you will gain experience writing functions in the C programming language.
Prerequisites: C control structures, I/O in C.
Contents:
a.
Enter the following C program into a new file, and then read it
carefully. Before running your program, think about what values you expect
to be printed before and after the function call to
modify_i. Now run your program. Did
it print the values you expected?
#include <stdio.h>
/* function prototype */
void modify_i(int i);
int main() {
int i = 1;
printf("The value of i before the function call is %d.\n", i);
modify_i(i);
printf("The value of i after the function call is %d.\n", i);
return 0;
}
/* this simple function modifies the value of its input parameter */
void modify_i(int i) {
i = i + 2;
}
Parameters are passed by value in C, meaning that when a function is called, the values of the arguments are copied into the corresponding parameters. Once the copies have been made, there is no persistent link between the arguments (in the calling function) and the parameters (in the callee). Thus, if a function changes the values stored in its parameters, that has no effect on the orginal arguments. If you do not understand this clearly, please ask.
a. A grocery store is selling oranges for 9 cents apiece, or 1 dollar per dozen.
Write a function price(int oranges) that accepts a number of oranges and returns the total price for that many oranges.
Then write a main function that you can use to test your function. Your program should prompt the user for the number of oranges, and then compute and output the total price.
b. Write a function isLeapYear(int year) that returns 1 if the input year is a leap year, or 0 otherwise. (A function that returns true or false, as this one essentially does, is called a predicate function.)
Hint: A year is a leap year if it is evenly divisible by 4 but not by 100, except that it is a leap year after all if it is also divisible by 400. For example, the year 1700 was not a leap year, but the years 2000 and 1996 were.
Of course, you will also want to write a main function that you can use to test your program.
c. Now add a function day_of_year(int month, int day, int year) to your previous program. This function should compute and return a number between 1 and 366 that specifies the (numbered) day of the year of the input date. Your function should make use of the function you wrote in the previous problem.
a.
Write a function that computes the distance between two points given in
Cartesian coordinates, such as (x0, y0) and
(x1,y1). Recall that the distance between these two points
is given by the square root of [(x0-x1)2 +
(y0-y1)2]. The prototype for your function could be:
double distance(double x0, double y0, double x1, double y1);
What pre-conditions are appropriate for this function?
Now write a main function that allows a user to input two
points (i.e., two pairs of (x,y) coordinates), and then calls
your distance function to compute the distance between them.
b. Modify your program from the previous exercise such that it prompts for three pairs of Cartesian coordinates, computes the distance between each pair, and outputs the shortest distance between any of the pairs. (You should not modify your distance function. Only modify your main function.)
What is the least number of times the distance function will need to be called to solve this problem? Does your program call it this many times, or more often?
c. Write a function ndigits(int k) that accepts a positive integer k and returns the number of (decimal) digits in k.
Hint: To do this, divide the input number by 10 repeatedly until you reach 0. The number of divisions needed is the number of digits in k.
Be sure to test your function!
a. In general, any loop can be re-written in a recursive fashion (although doing so is not always straight-forward). Write and test a recursive version of the iterative function below. Your function should print "Hello" n times.
void hello(int n) {
int i;
for (i = 0; i < n; i++) {
printf("Hello\n");
}
}
b. Write a function with prototype "int fibonacci(int n);" that uses recursion to compute and return the nth Fibonacci number. In case you don't remember it exactly, the definition of the Fibonacci numbers is given below.
F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2), for n > 1Now write a main function that calls your fibonacci function several times in order to print a listing of the first 46 fibonacci numbers: F(0) through F(45). What is the running time of fibonacci()?
c. Now can you write an iterative version of the fibonacci function (ie, use loops instead of recursion) such that it runs more quickly for large values of n?