0% found this document useful (0 votes)
16 views14 pages

C Programming: Recursion and Arrays

The document explains recursion as a method for solving complex problems by having functions call themselves, illustrated with a factorial example. It also covers arrays in C programming, detailing their declaration, initialization, and types, including single and multi-dimensional arrays, along with examples of usage. Additionally, it discusses applications of arrays in various operations such as matrix manipulation, search algorithms, and sorting algorithms.

Uploaded by

Ummineni rajasri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

C Programming: Recursion and Arrays

The document explains recursion as a method for solving complex problems by having functions call themselves, illustrated with a factorial example. It also covers arrays in C programming, detailing their declaration, initialization, and types, including single and multi-dimensional arrays, along with examples of usage. Additionally, it discusses applications of arrays in various operations such as matrix manipulation, search algorithms, and sorting algorithms.

Uploaded by

Ummineni rajasri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Recursion isthe technique of making a function call itself.

This technique provides a way to


break complicated problems down into simple problems which are easier to solve.

void recurse()

... .. ...

recurse();

... .. ...

int main()

... .. ...

recurse();

... .. ...

Fact program:

#include <stdio.h>

int factorial(int n);

int main()

int n,res;

Printf(“enter the n value”);

Scanf(“%d”,&n);

res=factorial(n);

printf(“the factorial of %d”,n,res);

int factorial(int n)
{

if (n > 1)

return n * factorial(n - 1);

} else

return 1;

Output: Factorial of 5 is 120

Array: An array is a special type of variable used to store multiple values of same data type at
a time.

An array can also be defined as follows...

An array is a collection of similar data items stored in continuous memory locations with
single name.

Declaration of an Array

In C programming language, when we want to create an array we must know the datatype of
values to be stored in that array and also the number of values to be stored in that array.

We use the following general syntax to create an array...

datatype arrayName [ size ] ;

Syntax for creating an array with size and initial values

datatype arrayName [ size ] = {value1, value2, ...} ;

Syntax for creating an array without size and with initial values

datatype arrayName [ ] = {value1, value2, ...} ;

In the above syntax, the datatype specifies the type of values we store in that array
and size specifies the maximum number of values that can be stored in that array.

Example Code
int a [3] ;

Here, the compiler allocates 6 bytes of contiguous memory locations with a single name 'a'
and tells the compiler to store three different integer values (each in 2 bytes of memory) into
that 6 bytes of memory. For the above declaration, the memory is organized as follows...

In the above memory allocation, all the three memory locations have a common name 'a'. So
accessing individual memory location is not possible directly. Hence compiler not only
allocates the memory but also assigns a numerical reference value to every individual
memory location of an array. This reference number is called "Index" or "subscript" or
"indices". Index values for the above example are as follows...

Accessing Individual Elements of an Array

The individual elements of an array are identified using the combination of 'arrayName' and
'indexValue'. We use the following general syntax to access individual elements of an array...

arrayName [ indexValue ] ;

For the above example the individual elements can be denoted as follows...

For example, if we want to assign a value to the second memory location of above array 'a',
we use the following statement...

Example Code

a [1] = 100 ;
The result of the above assignment statement is as follows...

Types of Arrays in C

In c programming language, arrays are classified into two types. They are as follows...

1. Single Dimensional Array / One Dimensional Array

2. Multi Dimensional Array

Multi Dimensional Array

An array of arrays is called as multi dimensional array. In simple words, an array created with
more than one dimension (size) is called as multi dimensional array. Multi dimensional array
can be of two dimensional array or three dimensional array or four dimensional array or
more...

Most popular and commonly used multi dimensional array is two dimensional array. The 2-D
arrays are used to store data in the form of table. We also use 2-D arrays to create
mathematical matrices.

Declaration of Two Dimensional Array

We use the following general syntax for declaring a two dimensional array...

datatype arrayName [ rowSize ] [ columnSize ] ;

Example Code

int matrix_A [2][3] ;

The above declaration of two dimensional array reserves 6 continuous memory locations of 2
bytes each in the form of 2 rows and 3 columns.

Initialization of Two Dimensional Array


We use the following general syntax for declaring and initializing a two dimensional array
with specific number of rows and coloumns with initial values.

datatype arrayName [rows][colmns] = {{r1c1value, r1c2value, ...},{r2c1, r2c2,...}...} ;

Example Code

int matrix_A [2][3] = { {1, 2, 3},{4, 5, 6} } ;

The above declaration of two-dimensional array reserves 6 contiguous memory locations of 2


bytes each in the form of 2 rows and 3 columns. And the first row is initialized with values 1, 2
& 3 and second row is initialized with values 4, 5 & 6.

We can also initialize as follows...

Example Code

int matrix_A [2][3] = {

{1, 2, 3},

{4, 5, 6}

};

Accessing Individual Elements of Two Dimensional Array

In a c programming language, to access elements of a two-dimensional array we use array


name followed by row index value and column index value of the element that to be
accessed. Here the row and column index values must be enclosed in separate square braces.
In case of the two-dimensional array the compiler assigns separate index values for rows and
columns.

We use the following general syntax to access the individual elements of a two-dimensional
array...

arrayName [ rowIndex ] [ columnIndex ]

Example Code

matrix_A [0][1] = 10 ;

In the above statement, the element with row index 0 and column index 1 of matrix_A array
is assinged with value 10.

Multi-dimensional Arrays in C
C programming language allows multidimensional arrays. Here is the general form of a
multidimensional array declaration −

type name[size1][size2]...[sizeN];

For example, the following declaration creates a three dimensional integer array −

int threedim[5][10][4];

Two-dimensional Arrays

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional


array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer
array of size [x][y], you would write something as follows −

type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier. A two-
dimensional array can be considered as a table which will have x number of rows and y
number of columns. A two-dimensional array a, which contains three rows and four columns
can be shown as follows −

Thus, every element in the array a is identified by an element name of the form a[ i ][ j ],
where 'a' is the name of the array, and 'i' and 'j' are the subscripts that uniquely identify each
element in 'a'.

Initializing Two-Dimensional Arrays

Multidimensional arrays may be initialized by specifying bracketed values for each row.
Following is an array with 3 rows and each row has 4 columns.

int a[3][4] = {

{0, 1, 2, 3} , /* initializers for row indexed by 0 */

{4, 5, 6, 7} , /* initializers for row indexed by 1 */

{8, 9, 10, 11} /* initializers for row indexed by 2 */


};

The nested braces, which indicate the intended row, are optional. The following initialization
is equivalent to the previous example −

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

Accessing Two-Dimensional Array Elements

An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and
column index of the array. For example −

int val = a[2][3];

The above statement will take the 4th element from the 3rd row of the array. You can verify it
in the above figure. Let us check the following program where we have used a nested loop to
handle a two-dimensional array −

#include <stdio.h>

int main ()

int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};

int i, j;

/* output each array element's value */

for ( i = 0; i < 5; i++ ) {

for ( j = 0; j < 2; j++ ) {

printf("a[%d][%d] = %d\n", i,j, a[i][j] );

return 0;

}
When the above code is compiled and executed, it produces the following result −

a[0][0]: 0

a[0][1]: 0

a[1][0]: 1

a[1][1]: 2

a[2][0]: 2

a[2][1]: 4

a[3][0]: 3

a[3][1]: 6

a[4][0]: 4

a[4][1]: 8

As explained above, you can have arrays with any number of dimensions, although it is likely
that most of the arrays you create will be of one or two dimensions.

Example 1: Two-dimensional array to store and print values

Example 2: Sum of two matrices

// C program to find the sum of two matrices of order 2*2

#include <stdio.h>

int main()

float a[2][2], b[2][2], result[2][2];

// Taking input using nested for loop

printf("Enter elements of 1st matrix\n");

for (int i = 0; i < 2; ++i)

for (int j = 0; j < 2; ++j)


{

printf("Enter a%d%d: ", i + 1, j + 1);

scanf("%f", &a[i][j]);

printf("Enter elements of 2nd matrix\n");

for (int i = 0; i < 2; ++i)

for (int j = 0; j < 2; ++j)

printf("Enter b%d%d: ", i + 1, j + 1);

scanf("%f", &b[i][j]);

for (int i = 0; i < 2; ++i)

for (int j = 0; j < 2; ++j)

result[i][j] = a[i][j] + b[i][j];

printf("\nSum Of Matrix:");

for (int i = 0; i < 2; ++i)

for (int j = 0; j < 2; ++j)

printf("%.1f\t", result[i][j]);

if (j == 1)

printf("\n");

return 0;
}

Run Code

Output

Enter elements of 1st matrix

Enter a11: 2;

Enter a12: 0.5;

Enter a21: -1.1;

Enter a22: 2;

Enter elements of 2nd matrix

Enter b11: 0.2;

Enter b12: 0;

Enter b21: 0.23;

Enter b22: 23;

Sum Of Matrix:

2.2 0.5

-0.9 25.0

Example 3: Three-dimensional array

// 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;
}

Run Code

Output

Enter 12 values:

10

11

12

Displaying Values:

test[0][0][0] = 1

test[0][0][1] = 2

test[0][1][0] = 3

test[0][1][1] = 4

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

Applications of Arrays in C

In c programming language, arrays are used in wide range of applications. Few of them are as
follows...

● Arrays are used to Store List of values

In c programming language, single dimensional arrays are used to store list of values of same
datatype. In other words, single dimensional arrays are used to store a row of values. In single
dimensional array data is stored in linear form.

● Arrays are used to Perform Matrix Operations

We use two dimensional arrays to create matrix. We can perform various operations on
matrices using two dimensional arrays.

● Arrays are used to implement Search Algorithms

We use single dimensional arrays to implement search algorihtms like ...

1. Linear Search

2. Binary Search

● Arrays are used to implement Sorting Algorithms

We use single dimensional arrays to implement sorting algorihtms like ...

1. Insertion Sort

2. Bubble Sort

3. Selection Sort

4. Quick Sort

5. Merge Sort, etc.,

● Arrays are used to implement Datastructures

We use single dimensional arrays to implement datastructures like...


1. Stack Using Arrays

2. Queue Using Arrays

● Arrays are also used to implement CPU Scheduling Algorithms

You might also like