Recursive Functions
Recursive Functions
Recursion is the process of calling the same function again and again until some
condition is satisfied.
This process is used for repetitive computation.
Syntax:
function_name()
{
function_name();
}
Types of Recursion
a) Direct Recursion
b) Indirect Recursion
a) Direct Recursion
A function is directly recursive if it calls itself.
Functionname1( )
{
….
Functionname1 ( ); // call to itself
….
}
Example
A function is said to be directly recursive if it explicitly calls itself. Here, the
function Func() calls itself for all positive values of n, so it is said to be a
directly recursive function.
{
if (n == 0)
return n;
else
return (Func (n–1));
}
b) Indirect Recursion
Function calls another function, which in turn calls the original function.
Functionname1 ( )
{
…
Functionname2 ( );
…
}
Functionname1 ( )
{
…
Functionname1 ( );// function (Functionname2) calls (Functionname1)
…
}
Example
A function is said to be indirectly recursive if it contains a call to another
function which ultimately calls it. These two functions are indirectly recursive as
they both call each other.
int Funcl (int n)
{
if (n == 0)
return n;
else
}
int Func2(int x)
{
return Func1(x–1);
}
/*C program to find factorial of a given number using recursion*/
#include< stdio.h> #include<conio.h> void main()
{
int fact(int); int num,f; clrscr();
printf(“enter the number”); scanf(“%d”,&num); f=fact(num);
printf(“ the factorial of %d= %d”, num, f);
}
int fact(int x)
{
int f;
if(x==1)
return(1);
else
f=x*fact(x-1); //recursive function call
return (f);
}
Output:
Enter the number 5
The factorial of 5=120
ARRAYS
Introduction to Arrays
An Array is a collection of similar data elements
These data elements have the same data type
The elements of the array are stored in consecutive memory locations and are
referenced by an index
Definition
An array is a data structure that is used to store data of the same type. The
position of an element is specified with an integer value known as index or
subscript.
Example
Declaration of an Array
Array has to be declared before using it in C program. Declaring array means
specifying three things.
Data_type Data Type of Each Element of the array
Array_name Valid variable name
Size Dimensions of the Array
Arrays are declared using the following syntax:
type name[size]
Here the type can be either int, float, double, char or any other valid data type. The
number within the brackets indicates the size of the array, i.e., the maximum number of
elements that can be stored in the array.
Example: i) int marks[10]
ii) int a[5]={10,20,5,56,100}
The declaration of an array tells the compiler that, the data type, name of the
array, size of the array and for each element it occupies memory space. Like for int data
type occupies 2 bytes for each element and for float occupies 4 bytes for each element
etc. The
size of the array operates the number of elements that can be stored in an array.
Initialization of arrays
Elements of the array can also be initialized at the time of declaration as in the
case of every other variable. When an array is initialized, we need to provide a value for
every element in the array. Arrays are initialized using the following syntax:
The values are written with curly brackets and every value is separated by a comma.
It is a compiler error to type array_name
specify [size]of
more number = {values
list ofthan
values};
the number of elements in
the array.
Example: int marks [5] = {90, 92, 78, 82, 58};
a 1 0
char b[5]={‘A’.’r’,’r’};
}
Output:
Enter 5 numbers one by one
57364
The maximum number in the array is 7
Program 1.33
/*Program for reversing an array*/
#include<stdio.h>
{
int a[10], i;
int n;
printf(“Enter the maximum number of elements\n”);
scanf(“%d”, &n);
for(i=0; i<n; i++)
{
scanf(“%d”,&a[i]);
}
printf(“Array in the reverse order\n”);
for(i=n–1; i>=0; i--)
{
printf(“%d\t”, a[i]);
}
getch( );
}
Output
Enter the maximum number of elements
5 11 12 13 14 15
Array in the reverse order
15 14 13 12 11
Program 1.34
/* Program to calculate sum of array content */
# include<stdio.h>
void main( )
{
int a[20], n, i, sum = 0;
print f(“\n Enter the size of the array:”);
scanf(“%d”, &n)
printf (“\n Enter the %d numbers one by one:”);
for (i=0; i<n; i++)
{
scanf(“%d”, &a[i]);
sum = sum + a[i];
}
printf (“The sum of array content = %d”, sum);
getch( );
}
Output
MULTI-DIMENSIONAL ARRAY
A multi-dimensional array is an array that has more than one dimension. It is an
array of arrays; an array that has multiple levels. The simplest multi-dimensional
array is the 2D array, or two-dimensional array and 3D or three-dimensional
array.
1 2 3 6 7
9 10 5 0 4
a[3][5]
3 1 2 1 6
Declaration
a 1 4 6
2 0 0
}
}
/* Program module to sum colwise */
for(i=0;i<n;i++)
{
colsum=0;
for(j=0;j<n1;j++)
colsum+=a[j][i];
printf(“col no=%d sum=%d\n “,i,colsum);
}
diasum=0;
for(i=0;i<n;i++)
for(j=0;j<n1;j++)
if(i==j) diasum+=a[i][j];
printf(“Principle diagonal sum %d\n”,diasum);
/ * Program module to sum off diagonal */
diasum=0;
for(i=0;i<n;i++)
{
j= -n1;
diasum +=a[i][j];
}
printf(“Off diagonal sum%d\n”,diasum);
}
Output
Enter order [row][col] of the
matrix 3 3
Enter 9 elements
123456789
Sum of all elements
45 row no = 0 sum =
6 row no = 1 sum =
15 row no = 2 sum =
24 col no = 0 sum =
12 col no = 1 sum =
15 col no = 2 sum =
18
Principle diagonal sum
15 Off diagonal sum
15Three-Dimensional
Arrays Initialization of a
3d array
Initialize a three-dimensional array in a similar way to a two-dimensional array.
Example
int test[2][3][4] = {
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}};
Program 1.36
Write a C Program to store and print 12 values entered by the
user #include <stdio.h>
int main()
{
int test[2][3][2];
printf("Enter 12 values: \
n"); for (int i = 0; i < 2; +
+i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
scanf("%d", &test[i][j][k]);
}
}
}
// Printing values with the proper
index. printf("\nDisplaying values:\
n");
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 2; ++k)
{
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j][k]);
}
}
}
return 0;
}
Output
Enter 12 values:
1
2
3
4
5
6
7
8
9
10
11
12
Displaying
Values: test[0][0]
[0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12