Next in line, we are learning about recursive functions - the function that calls itself from within its function; backtracking. It is in standard C. I appreciate all who take a look in advance.
The code we were given to use is as follows:
My program thus far is the following:
When I compile the program, I get the following:
The code we were given to use is as follows:
Code:
if x <= 0.....f(x) = 0
else............f(x) = f(x - 1) + 2
My program thus far is the following:
Code:
#include <stdio.h>
#include <math.h>
int main()
{
//Variable local to main.
int x, y;
int fun(int);
//Input
printf("Welcome. \n");
printf("Enter x to find f(x): \n");
scanf("%d", x);
//Call to the recursive function to find x.
y = fun(x);
//Output.
printf("The result is is: %d \n", y);
return 0;
}
//Function that returns the recursive value.
int fun(int x)
{
//Base case.
if(x <= 0)
return 0;
//Recursive function.
else
return fun(x - 1) + 2;
}
When I compile the program, I get the following:
Code:
Welcome.
Enter x to find f(x):
8
[Session started at 2010-04-16 14:55:50 -0400.]
GNU gdb 6.3.50-20050815 (Apple version gdb-966) (Tue Mar 10 02:46:18 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "powerpc-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 66952.
(gdb)