BCSC 1102 : Intro To Programming Week 10
Dr. Shem Mbandu Angolo, PhD
The Co-operatetive University of Kenya
September - December 2024
Contents
1 Recursion 2
1.1 Basic Concept of Recursion . . . . . . . . . . . . . . . . . . . . . 2
1.2 Example: Factorial of a Number . . . . . . . . . . . . . . . . . . 2
1.3 Example: Fibonacci Sequence . . . . . . . . . . . . . . . . . . . . 2
2 Preprocessor Directives 3
2.1 Common Preprocessor Directives . . . . . . . . . . . . . . . . . . 3
2.2 Example: Macro Definition . . . . . . . . . . . . . . . . . . . . . 3
2.3 Example: Conditional Compilation . . . . . . . . . . . . . . . . . 4
3 Command Line Arguments 4
3.1 Accessing Command Line Arguments . . . . . . . . . . . . . . . . 4
3.2 Example: Command Line Arguments . . . . . . . . . . . . . . . . 5
3.3 Example: Sum of Numbers . . . . . . . . . . . . . . . . . . . . . 5
4 Conclusion 5
1
1 Recursion
Recursion is a programming technique where a function calls itself directly or
indirectly. It is used to solve problems that can be broken down into smaller,
similar sub-problems.
1.1 Basic Concept of Recursion
A recursive function has two main parts:
• Base Case: The condition under which the recursion ends.
• Recursive Case: The part of the function that calls itself with modified
arguments.
1.2 Example: Factorial of a Number
The factorial of a non-negative integer n is the product of all positive integers
less than or equal to n. It is denoted as n!.
1 # include < stdio .h >
2
3 // Recursive function to calculate factorial
4 int factorial ( int n ) {
5 if ( n == 0) {
6 return 1; // Base case
7 } else {
8 return n * factorial ( n - 1) ; // Recursive case
9 }
10 }
11
12 int main () {
13 int num = 5;
14 printf (" Factorial of % d is % d \ n " , num , factorial (
num ) ) ;
15 return 0;
16 }
1.3 Example: Fibonacci Sequence
The Fibonacci sequence is a series of numbers where each number is the sum of
the two preceding ones, usually starting with 0 and 1.
1 # include < stdio .h >
2
3 // Recursive function to calculate Fibonacci sequence
4 int fibonacci ( int n ) {
2
5 if ( n == 0) {
6 return 0; // Base case
7 } else if ( n == 1) {
8 return 1; // Base case
9 } else {
10 return fibonacci ( n - 1) + fibonacci ( n - 2) ; //
Recursive case
11 }
12 }
13
14 int main () {
15 int num = 10;
16 for ( int i = 0; i < num ; i ++) {
17 printf ("% d " , fibonacci ( i ) ) ;
18 }
19 printf ("\ n ") ;
20 return 0;
21 }
2 Preprocessor Directives
Preprocessor directives are lines included in the code of programs preceded by a
hash sign (#). These lines are processed by the preprocessor before the actual
compilation of code begins.
2.1 Common Preprocessor Directives
• #define: Defines a macro.
• #include: Includes a header file.
• #undef : Undefines a macro.
• #ifdef, #ifndef, #if, #else, #elif, #endif : Conditional compilation.
2.2 Example: Macro Definition
Macros are defined using the #define directive. They are used to define con-
stants or functions.
1 # include < stdio .h >
2
3 # define PI 3.14159
4 # define CIRCLE_AREA ( radius ) ( PI * ( radius ) * ( radius ) )
5
6 int main () {
3
7 double r = 5.0;
8 printf (" Area of the circle with radius %.2 f is %.2
f \ n " , r , CIRCLE_AREA ( r ) ) ;
9 return 0;
10 }
2.3 Example: Conditional Compilation
Conditional compilation is used to compile code selectively based on certain
conditions.
1 # include < stdio .h >
2
3 # define DEBUG
4
5 int main () {
6 int a = 5 , b = 10;
7
8 # ifdef DEBUG
9 printf (" Debug : a = %d , b = % d \ n " , a , b ) ;
10 # endif
11
12 int sum = a + b ;
13 printf (" Sum : % d \ n " , sum ) ;
14
15 return 0;
16 }
3 Command Line Arguments
Command line arguments are parameters passed to the program when it is
invoked from the command line. They allow the user to specify input values for
the program.
3.1 Accessing Command Line Arguments
In C, command line arguments are accessed using the parameters of the main()
function: int argc, char *argv[].
• argc: Argument count, the number of command line arguments.
• argv: Argument vector, an array of strings representing the arguments.
4
3.2 Example: Command Line Arguments
This example demonstrates how to access and use command line arguments in
a C program.
1 # include < stdio .h >
2
3 int main ( int argc , char * argv []) {
4 printf (" Number of arguments : % d \ n " , argc ) ;
5 printf (" Arguments :\ n ") ;
6 for ( int i = 0; i < argc ; i ++) {
7 printf (" argv [% d ]: % s \ n " , i , argv [ i ]) ;
8 }
9 return 0;
10 }
3.3 Example: Sum of Numbers
A program that calculates the sum of numbers provided as command line argu-
ments.
1 # include < stdio .h >
2 # include < stdlib .h >
3
4 int main ( int argc , char * argv []) {
5 if ( argc < 2) {
6 printf (" Usage : % s < numbers >\ n " , argv [0]) ;
7 return 1;
8 }
9
10 int sum = 0;
11 for ( int i = 1; i < argc ; i ++) {
12 sum += atoi ( argv [ i ]) ; // Convert argument to
integer and add to sum
13 }
14
15 printf (" Sum : % d \ n " , sum ) ;
16 return 0;
17 }
4 Conclusion
Understanding recursion, preprocessor directives, and command line arguments
is essential for efficient C programming. Recursion helps in solving complex
problems with simpler sub-problems. Preprocessor directives allow for code
5
management before compilation, and command line arguments provide flexibil-
ity in input handling.