HW1
Homework 1: Due Sep 14, 9:00 AM.
1. A Fibonacci number Fn is defined as follows:
Fn = Fn-1 + Fn-2
F0 = 0; F1 = 1
That is, any Fibonacci number is defined as the sum of the
previous two Fibonacci numbers.
Write three separate functions,
int fib_while(n)
int fib_for(n)
int fib_do(n)
that each takes an integer number n as an argument
and return F(n) as the output.
In the main program ask the user to input an integer as
input. Compute F(n) separately using each of
these functions and print it.
What is the maximum value of n for which you can
compute F(n) using your program?
2. Write a function
int mySubstr(char *s1, char *s2)
that takes two strings (character pointers) s1, s2
as input and finds out if s2 is a substring of s1.
You can assume that both the strings are null terminated,
that is the last character of each string is NULL.
If s2 is a substring of s1,
mySubstr(char *s1, char *s2) should return 1;
otherwise it should return 0.
In the main program, ask the user to enter two strings as input,
one for s1 and the other for s2.
Call the function mySubstr(char *s1, char *s2) and print the output.
3. Question for bonus credit:
Recursion is an important part of programming and computer science
in general. Recursion is the property where a function calls itself.
For example,
int f1(&lsaquo arguments &rsaquo)
{
// Some code here
f1(&lsaquo some_other_argument &rsaquo)
}
It looks like this would go to an infinite loop where funtion f1
keeps on calling itself forever without termination.
The key for this to work correctly is that,
you should specify a terminating condition. That is,
when the terminating condition holds good f1 should not
call itself, but should come out of the loop and return the output.
Define a function called
which computes the Fibonacci number defined above in the first problem.
fib_recursion(n) should call itself. The terminating conditions are
when n = 0 or n = 1.
In the main program, ask the user to input an integer number.
Call fib_recursion(n) and print the output.
Ramana Isukapalli
Last modified: Tue Sep 9 15:42:06 EDT 2008