UMBC CS 201, Fall 06
countdown
Program
/* File: countdown.c
* Author: Richard Chang
* Date: pre 1997
* Modified by: Sue Evans
* Date: 11/8/05
* Section: 101
* EMail: bogar@cs.umbc.edu
*
* A very simple example of a recursive function.
*/
#include <stdio.h>
void Countdown (int) ;
int main()
{
int n ;
printf("Engaging the auto-destruct sequence\n") ;
printf("Enter number of seconds to auto-destruct: ") ;
scanf("%d", &n) ;
printf("Counting down from %d\n", n) ;
Countdown(n) ;
return 0;
}
/******************************************
* Function Name: Countdown ()
* Countdown() uses recursion to count down from k to 0
*
* Inputs: integer to count down from
* Outputs: returns nothing
* Side effect: prints to screen the counting down and
* "KABOOM!!!"
******************************************/
void Countdown (int k)
{
/* The BASE CASE */
if (k == 0)
{
printf("KABOOM!!!\n") ;
}
/* The GENERAL RULE - recursive case */
else
{
printf(" %d seconds to auto-destruct\n", k) ;
Countdown (k - 1) ;
}
}
Output
linux1[95]% a.out
Engaging the auto-destruct sequence
Enter number of seconds to auto-destruct: 9
Counting down from 9
9 seconds to auto-destruct
8 seconds to auto-destruct
7 seconds to auto-destruct
6 seconds to auto-destruct
5 seconds to auto-destruct
4 seconds to auto-destruct
3 seconds to auto-destruct
2 seconds to auto-destruct
1 seconds to auto-destruct
KABOOM!!!
linux1[96]% a.out
Engaging the auto-destruct sequence
Enter number of seconds to auto-destruct: 17
Counting down from 17
17 seconds to auto-destruct
16 seconds to auto-destruct
15 seconds to auto-destruct
14 seconds to auto-destruct
13 seconds to auto-destruct
12 seconds to auto-destruct
11 seconds to auto-destruct
10 seconds to auto-destruct
9 seconds to auto-destruct
8 seconds to auto-destruct
7 seconds to auto-destruct
6 seconds to auto-destruct
5 seconds to auto-destruct
4 seconds to auto-destruct
3 seconds to auto-destruct
2 seconds to auto-destruct
1 seconds to auto-destruct
KABOOM!!!
linux1[97]%
CSEE
|
201
|
201 F'06
|
lectures
|
news
|
help
Last Modified - Tuesday, 22-Aug-2006 07:14:19 EDT