0% found this document useful (0 votes)
67 views

Module 3 Notes of Intruduction To C Programming

The document discusses arrays and two-dimensional arrays in C programming. It defines arrays as collections of homogeneous elements that can be accessed using subscripts. Two-dimensional arrays use two subscripts to access row and column elements. The document provides examples of declaring, initializing, accessing, and performing operations on arrays and 2D arrays, including traversing, inserting/deleting elements, merging, searching, sorting, and calculating transpose and sums of matrices.

Uploaded by

dileepkumarscd
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Module 3 Notes of Intruduction To C Programming

The document discusses arrays and two-dimensional arrays in C programming. It defines arrays as collections of homogeneous elements that can be accessed using subscripts. Two-dimensional arrays use two subscripts to access row and column elements. The document provides examples of declaring, initializing, accessing, and performing operations on arrays and 2D arrays, including traversing, inserting/deleting elements, merging, searching, sorting, and calculating transpose and sums of matrices.

Uploaded by

dileepkumarscd
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

MODULE 3

FUNCTIONS AND ARRAYS

Array
Array is a collection of homogeneous (of same data type) elements.

An array is declared as follows

data_type array_name[array_size];

Accessing the elements of an array:

Subscript(index) is used to access array elements. For


example,

//Set each element of the array to -1

int i, marks[10];
for (i=0;i<10;i++)
marks[i]=-1;

Calculating the address of array elements:

A[k]=Base_Address(A) + w*(k-lower_bound)

For example,

Given an array int marks[]={99, 67, 78, 56, 88, 90, 34, 85}. Calculate the address of marks[4]
if the base address = 1000.

Solution: Address(Marks[4]) = 1000 + 2(4-0)

=1000 + 8 = 1008

Calculating the length of an array:

Length = upper_bound – lower_bound + 1

For example,

(Refer class notes)

Length = upper_bound – lower_bound + 1

=4–0+1=5
Initializing arrays during declaration:

type array_name[size] = {list of values};

For example,

int marks[5] = {90, 82, 78, 95, 88}

Programmer may omit the size of the array during initialization

For example,

int marks[] = {98, 97, 90}

If the number of values provided is less than the number of elements in the array, the un-
assigned elements are filled with zeros.

For example,

(Refer class notes)

Inputting values from the keyboard:

int i, marks[10];
for(i=0;i<10;i++)
scanf(“%d”, &marks[i]);

Assigning values to individual elements:

A simple assignment statement can be written as

marks[3]=100;

Example 2, program to copy elements from one array to another array

int i, arr1[10], arr2[10];


arr1[1] = { 10 , 20 , 30 , 40 , 50 }
for(i=0;i<n;i++)
arr2[i] = arr1[i];
Example 3, Program to fill an array with even numbers

Int i, arr[10];
for (i=0; i<10; i++)
arr[i] = i*2
Operations on Array

1. Traversing an array
2. Inserting an element in an array
3. Deleting an element from an array
4. Merging two arrays
5. Searching an element in an array
6. Sorting an array in ascending or descending order

Traversing an array: Traversing an array means accessing each and every element of the array
for a specific purpose.

Example 1: Write a program to read and display n numbers using an array.

#include <stdio.h>
void main()
{
int a[100], i, n;
printf (“Enter the number of elements\n”);
scanf(“%d”, &n);
printf(“Enter the elements:\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Elements of the array are:\n”);
for (i=0;i<n;i++)
printf(“%d\n”,a[i]);
}

Example 2: Write a program to print the position of the smallest of n numbers

#include<stdio.h>
void main()
{
int a[100], i, n, smallest, pos;
printf(“Enter the number of elements\n”);
scanf(“%d”,&n);
printf(“Enter the elements:\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
smallest = a[0];
pos = 0;
for (i=1;i<n;i++)
{
if(a[i]<smallest)
{
smallest = a[i];
pos = i;
}
}
printf(“The position of the smallest element:%d”,pos);
}

Inserting an element in an array: Inserting an element in an array means adding a new data
element to an already existing array.

Example: Write a program to insert a number at a given location in an array

#include<stdio.h>
void main()
{
int a[100], pos, i,n;
printf(“Enter the number of elements\n”);
scanf(“%d”,&n);
printf(“Enter elements:\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter new element to be inserted\n”);
scanf(“%d”,&num);

printf(“Enter the position where new item to be inserted\n”);


scanf(“%d”,&pos);
for (i=n-1;i>=pos;i--)
a[i+1]=a[i];
a[pos]=num;
n++;
printf(“Elements of the array after inserting new element:\n”);
for(i=0;i<n;i++)
printf(“%d”,a[i]);
}

Deleting an element from an array: Deleting an element from an array means removing a data
element from an already existing array.

Example: Write a program to delete a number from a given location in an array

#include<stdio.h>
void main()
{
int a[100], pos, i, n;
printf(“Enter the number of elements\n”);
scanf(“%d”,&n);
printf(“Enter the elements:\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter the position of element to be deleted\n”);
scanf(“%d”,&pos);
printf(“Deleted element:%d”,a[pos]);
for(i=pos;i<n-1;i++)
a[i]=a[i+1];
n--;
printf(“Elements of array after deletion of an element:\n”);
for(i=0;i<n;i++)
printf(“%d”,a[i]);
}

Merging two arrays: Copying two arrays into a new array is known as merging.

Example: Write a program to merge two unsorted arrays

#include<stdio.h>
void main()
{
int a[100],b[100], c[100],n1,n2,n3,i,index;
printf(“Enter the number of elements of first array\n”);
scanf(“%d”,&n1);
printf(“Enter the first array elements\n”);
for(i=0;i<n1;i++)
scanf(“%d”,&a[i]);
printf(“Enter the number of elements of second array\n”);
scanf(“%d”,&n2);
printf(“Enter the second array elements\n”);
for(i=0;i<n2;i++)
scanf(“%d”,&b[i]);
index=0;
for(i=0;i<n1;i++)
{
c[index]=a[i];
index++;
}
for(i=0;i<n2;i++)
{
c[index]=b[i];
index++;
}
n=n1+n2;
printf(“The elements of merged array are:\n”);
for(i=0;i<n;i++)
printf(“%d”,c[i]);
}

Searching an element in an array: Find whether a particular value is present in the array or
not.

Example: Linear search or Sequential search

#include<stdio.h>
void main()
{
int a[100], n, i, found, key;

printf(“Enter size of the array\n”);


scanf(“%d”,&n);
printf(“Enter array elements:\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter key element to be searched\n”);
scanf(“%d”,&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
found=1;
pos=i;
break;
}
}
if(found==1)
printf(“Element found at position %d”,pos);
else
printf(“Element not found\n”);
}
Example: Binary search

#include<stdio.h>
void main()
{
int a[100], n, i, found, key;
int low,high,mid
printf(“Enter size of the array\n”);
scanf(“%d”,&n);
printf(“Enter array elements:\n”);
scanf(“%d”,&a[i]);
low=0;
high=n-1;
printf(“Enter key element to be searched\n”);
scanf(“%d”,&key);
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
found=1;
pos=mid;
break;
}
else if(key<a[mid])
high=mid-1;
else
low=mid+1

if(found==1)
printf(“Element found at position %d”,pos);
else
printf(“Element not found\n”);
}
Sorting an array: Arranging elements in an ascending or descending order.

Example: Bubble sort

#include<stdio.h>
void main()
{
int a[100], n, i, j;
printf(“Enter the size of array\n”);
scanf(“%d”,&n);
printf(“Enter the array elements\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

Two dimensional array

Two dimensional array is specified using two subscripts. One subscript denotes row and the
other denotes column.

A Two-Dimensional array is declared as follows

data_type array_name[row_size][column_size];

Ex: int marks[3][3];


Initializing two dimensional array

int marks[2][3] = {90, 87, 78, 68, 62, 71};

int marks[2][3] = { {90, 87, 78}, {68, 62, 71} };


int marks[ ][3] = { {90, 87, 78}, {68, 62, 71} }; // Only size of the row can be omitted

int marks[2][3] = {0}; //Two dimensional array initialized to Zero

int marks[2][3] = {{50, 60, 70}}; // Initializes first row

[Refer class notes]

Accessing the elements of two dimensional array

Example: Write a program to print the elements of a 2D array.

#include<stdio.h>
void main()
{
int a[2][2] = {12, 34, 56, 32}
int i, j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“%d”,a[i][j]);
}
printf(“\n”);
}

Example: Write a program to read and display a matrix

#include<stdio.h>
void main()
{
int mat[3][3], i, j;
printf(“Enter the elements of a matrix\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf(“%d”,&mat[i][j]);
}

printf(“The matrix is:\n”);


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf(“\t %d”,mat[i][j]);
printf(“\n”);
}

Example: Write a program to generate Pascal’s triangle

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1

#include<stdio.h>

void main()
{
int arr[7][7]={0};
int row=2, col, i, j;
arr[0][0] = arr[1][0] = arr[1][1] = 1;
while(row<7)
{
arr[row][0] = 1;
for(col=1; col<=row; col++)
arr[col][row] = arr[col-1][row-1] + arr[row-1][col];
row++;
}

for(i=0;i<7;i++)
{
for(j=0;j<=i;j++)
printf(“\t %d”, arr[i][j]);
}
}

Operations on two dimensional arrays

1. Transpose of a matrix
2. Sum of matrices
3. Difference between matrices
4. Product of matrices

Transpose of matrix:

#include<stdio.h>

void main()
{
int mat[3][3], t_mat[3][3], i, j;

printf (“Enter the elements of the matrix\n”);


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”, &mat[i][j]);
}
}

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
t_mat[j][i] = mat[i][j];
}
}

printf(“Transpose of given matrix is:\n”);


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“\t %d”,t_mat[i][j]);
}
printf(“\n”);
}

Sum of matrices:

Example: Write a program to add two matrices and display the result

#include<stdio.h>
void main()
{
int mat1[5][5], mat2[5][5], sum_mat[5][5];
int row1, row2, col1, col2;

printf(“Enter the number of rows for matrix 1:\n”);


scanf(“%d”,&row1);

printf(“Enter the number of columns for matrix 1:\n”);


scanf(“%d”,&col1);

printf(“Enter the number of rows for matrix 2:\n”);


scanf(“%d”,&row2);

printf(“Enter the number of columns for matrix 2:\n”);


scanf(“%d”,&col2);

if (row1 !=row2 || col1!=col2)


{
printf(“Addition is not possible\n”);
exit();
}

printf(“Enter the elements of matrix 1:\n”);


for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
scanf(“%d”,&mat1[i][j]);
}
}

printf(“Enter the elements of matrix 2:\n”);


for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
scanf(“%d”,&mat2[i][j]);
}
}
rows_sum=rows1;
cols_sum=cols1;

for(i=0;i<rows_sum;i++)
{
for(j=0;j<cols_sum;j++)
{
sum_mat[i][j] = mat1[i][j] + mat2[i][j];
}
}

printf(“Sum of given two matrices :\n”);


for(i=0;i<rows_sum;i++)
{
for(j=0;j<cols_sum;j++)
{
scanf(“\t %d”,&sum_mat[i][j]);
}
printf(“\n”);
}
}

Multiplication of matrices:

Example: Write a program to compute product of two matrices

#include<stdio.h>
#include<stdlib.h>
int main( )
{
int m,n,p,q,row,col,k,a[3][3],b[3][3],c[3][3];
printf("Enter the order of matrix A\n");
scanf("%d%d",&m,&n);
printf("enter order of matrix B\n");
scanf("%d%d",&p,&q);

if(n!=p)
{
printf("Matrix Multiplication is not possible\n");
exit(0);
}

printf("Enter the elements into matrix A\n");


for(row=0;row<m;row++)
{
for(col=0;col<n;col++)
{
scanf("%d",&a[row][col]);
}
}

printf("Enter the elements into matrix B\n");

for(row=0;row<p;row++)
{
for(col=0;col<q ;col++)
{
scanf("%d",&b[row][col]);
}
}

for(row=0;row<m;row++)
{
for(col=0;col<q;col++)
{
c[row][col]=0;
for(k=0;k<n;k++)
{
c[row][col]= c[row][col]+a[row][k]*b[k][col];
}
}
}

printf("The elements of matrix A are\n");

for(row=0;row<m;row++)
{
for(col=0;col<n;col++)
{
printf("%d",a[row][col]);
}
printf("\n");
}

printf("The elements of matrix B are\n");


for(row=0;row<p;row++)
{
for(col=0;col<q;col++)
{
printf("%d",b[row][col]);
}
printf("\n");
}

printf("Product of Matrix A and B is\n");


for(row=0;row<m;row++)
{
for(col=0;col<q;col++)
{
printf("%d",c[row][col]);
}
printf("\n");
}

Multidimensional arrays

A multidimensional array is an array of arrays

Refer figure 12.32 from Reema Thareja book


Program: Write a program to read and display a 2 x 2 x 2 array

#include<stdio.h>
Void main()
{
int array[2][2][2], i, j, k;

printf(“Enter the elements of the matrix\n”);


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
scanf(“%d”, &array[i][j][k]);
}
}
}

printf(“The matrix is:\n”);

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
printf(“%d\t”, array[i][j][k]);
}
}
}
Functions

C enables programmers to break up a program into segments known as functions.

The following code shows that the main() function calls func1()

main() func1()
{ {
............; ..............;
............; }
func1();
............;
............;
}

Why functions are needed

i. Dividing a program into separate well-defined functions facilitates each function


to be written and tested separately. The following code shows top-down approach
of solving a problem.

main() func1() func2() func3()


{ { { {
............; .............; ............; ...........;
............; .............; ............; ...........;
func1(); func2(); func3(); return;
............; .............; ............; }
............; .............; ............;
return 0; return; return;
} } }
ii. Understanding, coding, and testing multiple separate functions is easier than doing
it for one big function.

iii. If all the statements written inside only main() function then it increases the
program size which makes difficulty in maintaining the program because of
limited memory space.

iv. All the libraries in C contain a set of functions which are pre-written and pre-
tested. Programmers can use these functions which speed up program
development.

v. If program is divided into smaller sub-programs then different programmers can


work on the sub-programs individually.

vi. Like C libraries, programmers can also write their functions and use them in the
main program or any other programs.

Function declaration/Function prototype

return_data_type function_name(data_type variable1, data_type variable2, ......);

Example,

int func(int, char, float); //It is optional to include argument names

or

int func(int num, char ch, float num)

The function declaration is global. Therefore, the declared function can be called from any
point in the program.

Example,

Write a program to add two integers using functions

#include<stdio.h>
int sum(int a, int b);
int main()
{
int num1, num2, total=0;

printf(“Enter the first number:\n”);


scanf(“%d”, &num1);

printf(“Enter the second number:\n”);


scanf(“%d”, &num2);

total = sum(num1, num2);

printf(“Total = %d”, total);


}

int sum(int a, int b)


{
int result; r
result = a + b;
return result;
}

A function may have more than one return statement.

Example,

#include<stdio.h>
int check_relation(int a, int b);

void main()
{
int a=3, b=5, res;

res = check_relation(a,b);

if(res = = 0)
printf(“Equal\n”);
if(res = = 1)
printf(“a is greater than b\n”);
if(res = = -1)
printf(“ a is less than b\n”);

int check_relation(int a, int b)


{
if(a = = b)
return 0;
else if (a>b)
return 1;
else
return -1;
}

Passing parameters to functions

When a function is called, the calling function may have to pass values to the called function.
There are two ways in which arguments or parameters can be passed to the called function.
They include,
i. Call by value: Variables are passed by calling function to the called function
ii. Call by reference: Address of variables are passed by the calling function to the
called function.

Call by value:

Example,

#include<stdio.h>
void add(int);
void main()
{
int num = 2;
printf(“The value of num before calling the function = %d\n”, num);

add(num);

printf(“The value of num after calling the function = %d”, num);


}

void add(int n)
{
n = n+10;
printf(“The value of num in the called function = %d”, n);
}

Output:

The value of num before calling the function = 2


The value of num after calling the function = 12
The value of num in the called function = 2

Pros and Cons of call by value:


Pros: Arguments can be variables, literals or expressions.
Cons: Take a lot time to copy resulting in performance penalty.

Call by reference

#include<stdio.h>
void add(int *n);
void main()
{
Int num = 2;

printf(“The value of num before calling the function = %d”, num);


add(&num);

printf(“The value of num after calling the function = %d”, num);


}

void add(int *n )
{
*n = *n + 10;
printf(“The value of num in the called function = %d”, *n);
}

Output:

The value of num before calling the function = 2


The value of num after calling the function = 12
The value of num in the called function = 12

Advantages of call by reference:

i. Provides greater time and space efficiency.


ii. The change in called function gets reflected in the calling function.
iii. In case we need to return multiple values, pass those arguments by reference.

Disadvantages of call by reference:


When an argument is passed using call by address, it becomes difficult to tell whether that
argument is meant for input, output or both.

Example,

Program 1: Write a program to swap the value of two variables.

#include<stdio.h>
void swap_call_by_val(int, int);
void swap_call_by_ref(int *, int *);
void main()
{
int a=1, b=2, c=3, d=4;
printf(“In main function, a=%d and b=%d”, a, b);
swap_call_by_val(a, b);
printf(“In main function after call to swap by value function, a=%d and b=%d”, a, b);
printf(“In main function, c=%d and b=%d”, a, b);
swap_call_by_ref(&c, &d);
printf(“In main function after call to swap by value function, c=%d and d=%d”, c, d);
}

void swap_call_by_val(int a, int b)


{
int temp;
temp = a;
a = b;
b = temp;
printf(“In call by value function a=%d and b=%d”, a, b);
}

void swap_call_by_ref(int *c, int *d)


{
int temp;
temp = *c;
*c = *d;
*d = temp;
printf(“In call by value function c=%d and d=%d”, *c, *d);
}

Program 2: Write a program to find the biggest of three integers using functions

#include<stdio.h>
int greater (int a, int b, int c);
void main()
{
int num1, num2, num3, large;

printf(“Enter the first number: \n”);


scanf(“%d”, &num1);

printf(“Enter the second number: \n”);


scanf(“%d”, &num2);

printf(“Enter the third number: \n”);


scanf(“%d”, &num3);

large = greater(num1, num2, num3)

printf(“Largest number = %d”, large);


}

void greater(int a, int b, int c)


{
if(a>b && a>c)
return a;
if(b>a && b>c)
return b;
else
return c;
}

Program 3: Write a program to swap two numbers using call by value

#include<stdio.h>
void swap(int, int);
void main()
{
int x, y;
printf(“Enter the value of x:”);
scanf(“%d”, &x);
printf(“Enter the value of y:”);
scanf(“%d”, &y);

printf(“Values before swapping\n”);


printf(“x=%d y=%d”, x, y);
swap(&x, &y);

printf(“Values after swapping\n”);


printf(“x=%d y=%d”, x, y);
}

void swap(int a, int b)


{
int temp;
temp = a;
a = b;
b = temp;
}

Scope of variables

Scope mean the accessibility and visibility of the variables at different points in the program.
A variable has following four types of scope:

i. Block
ii. Function
iii. Program
iv. File

Block scope:

Block is a group of statements enclosed within opening and closing brackets { }.

If a variable is declared within a block then as soon as the control exits that block, the
variable will cease to exist. Such a variable is known as local variable and which is in block
scope.

Function scope:

Function scope indicates that a variable is active and visible from the beginning to the end of
a function.In C, only the goto label has function scope.

Example,
return main()
{
.
.

loop: /* A goto label has function scope */


.
.
goto loop;
.
.
}

In this example, the label is visible from the beginning to the end of the main() function.
Therefore, there should not be more than one label with the same name inside the main()
function.

Program scope:

Global variables are created at the beginning of program execution and remain existence
throughout the period of execution of the program.

These variables retain their value so that they can be used from every function in the
program.

It is recommended to declare global variables on top of the program code.

If we declare local variable that has same as global variables then function use the local
variable and ignore the global.

Example,

#include<stdio.h>
int x=10;
void print();
void main()
{
printf(“The value of x in the main = %d”, x);
int x = 2;
printf(“The value of local variable x in the main = %d”, x);
print();
}
void print()
{
printf(“The value of x in the print = %d”, x );
}

Output:

The value of x in the main = 10


The value of local variable x in the main = 2
The value of x in the print = 10

Storage classes

Storage classes defines the scope (visibility) and lifetime of variables and functions declared
within a C program.

General syntax for specifying the storage class of a variable

<storage_class_specifier><data_type><variable_name>

Different storage classes are discussed below

i. ‘auto’ storage class

● Syntax: auto int x;

● This storage class is used to explicitly declare a variable with automatic storage.

● It is the default storage class for variables declared inside a block.

● In the above example, x is an integer that has automatic storage.

● If not initialized, the variable holds garbage value.

● The auto variables are stored in primary memory.

● Scope of the variable is local to the block in which it is declared.


● Example:

void func1()
{
int a=10; //auto integer local to func1()
printf(“a=%d”, a);
}

void func2()
{
int a=20; //auto integer local to func2()
printf(“a=%d”, a);
}

void main()
{
int a=30; //auto integer local to main()
func1();
func2();
printf(“a=%d”, a);
}

Output:
a=10
b=20
c=30

ii. ‘register’storage class

● Syntax: register int x

● Variable is stored in the register.

● If not initialized, the variable holds garbage value.

● Scope of the variable is local to the block in which it is declared.


● Example,

void main()
{
int a=3, b=5, res;
res = exp(a,b);
printf(“Result = %d”, res);
}
int exp(int a, int b)
{
register int res=1; //register variable
int i;
for(i=1;i<=b;i++)
res=res*a;
return res;
}

iii. ‘extern’ storage class

● Syntax: extern int x;

● Visible to all the program files.

● If not initialized, the variable is filled with zero.

● Declared and defined at the beginning of a source file.

● Example,

// FILE1.c

#include<stdio.h>
#include<FILE2.c>

int x;
void print(void);

void main()
{
x=10;
printf(“ x in FILE1 = %d”, x);
print();
}

//FILE2.c

#include<stdio.h>
extern int x;
void print()
{
printf(“x in FILE 2 = %d”, x);
}

main()
{
//statements
}

Output:
x in FILE1 = 10
x in FILE2 = 10

iv. ‘static’ storage class

● Syntax: static int x=10

● Static variables have lifetime over the entire program

● Static is the default storage class for all global variables

● If not initialized, then variable is filled with zero.

● Example,

void print()
void main()
{
printf(“First call of print());
print();
printf(“Second call of print());
print();
printf(“Third call of print());
print();
}

void print()
{
static int x;
int y = 0;
printf(“Static integer variable, x = %d”, x);
printf(“Integer variable, y=%d”, y);
x++;
y++;
}

Output:

First call of print()


Static integer variable, x=0
Integer variable, y=0

Second call of print()


Static integer variable, x=1
Integer variable, y=0

Second call of print()


Static integer variable, x=2
Integer variable, y=0

Recursive function

If a function calls itself then the function is called recursive function.

Example programs,

1. Program to find the GCD (Greatest Common Divisor)

int GCD(int, int);


void main()
{
int num1, num2, res;
printf(“Enter two numbers\n”);
scanf(“%d %d”, &num1, &num2);
res = GCD(num1, num2);
printf(“GCD = %d”, res)
}

int GCD(int x, int y)


{
int rem;
rem = x%y;
if(rem == 0)
return y;
else
return (GCD(y, rem));
}

2. Factorial of a number

#include<stdio.h>
int fact(int);

void main()
{
int num, factorial;
printf( “Enter the number: ”);
scanf(“%d”, &num);
factorial = fact(num);
printf(“Factorial of a number = %d”, factorial);
}

int fact(int n)
{
if(n==1)
return 1;
else
return (n*fact(n-1));
}

3. Program to generate Fibonacci series

#include<stdio.h>
int fibonacci(int);
void main()
{
Int n, i=0, res;
printf(“Enter the number of terms\n”);
scanf(“%d”, &n);
printf(“Fibonacci series: \n”)
for(i=0; i<n; i++)
{
res = fibonacci(i);
printf(“%d\t”, res);
}
}

int fibonacci(int n)
{
if(n = =0)
return 0;
else if (n = = 1)
return 1;
else
return (fibonacci(n-1) + fibonacci(n-2));
}

Passing arrays to functions


[Refer class notes]

Passing two-dimensional arrays to functions.


[Refer class notes]

You might also like