Array in C Programming
Array in C Programming
1
2
LEARNING OBJECTIVES:
Describe multi- 3
Explain dynamic arrays
dimensional arrays
INTRODUCTION:
4 4
What is array?
An array is a fixed size sequenced collection of elements of the SAME
datatype.
Syntax to declare array:
datatype variable-name [size];
5
We don’t need to declare values for
elements. Like below:
6
One-dimensional:
Just by declaring one variable we can put an INDEX number which is merely the number variables.
We can index starting with 0 which is preferred by computer.
But we shouldn’t get confused so we can state indexing with 1.
7
7
OR,
8
8
Compile Time Initialization:
We can initialize the elements of array in same way as
the ordinary variable when they are declared.
9
Character array Initialization :
In this case the declared size has more initialized elements. The compiler will
create error. 10
Run time initialization:
An array can be Explicitly initialized at run time. This approach is usually applied for
initializing user input data. Using for loop can help in this case.
11
11
We can’t leave size empty in array
12
12
Searching And Sorting:
Sorting: The process of arranging elements in the list according to their values, in ascending or
descending order. A sorted list is called an ordered list. Sorted lists are especially important in list
searching because they facilitate rapid search operation.
Important and simple sorting techniques:
Searching: The process of finding the location of the specific element in a list. The specified element is often
called the search key. If the search key with list element values, the search is said to be successful else
unsuccessful.
The most commonly used search techniques are:
Sequential Search
Binary Search
13
13
TWO-DIMENSIONAL ARRAYS:
We represent a particular value in matrix using 2 subscripts such as vrc here, r is for row
and c is for column.
Syntax:
datatype array_name[row_size][column_size];
Like single-dimensional arrays, each dimension of the array is indexed from zero to its maximum size-1.
The first index selects the row and the second index selects the column within row.
1414
2D Array Compile time initialization:
Array size declaration and values initialized in braces:
int table[2][3]={0, 0, 0, 1, 1, 1};
OR,
The initialization is done
row by row
int table[2][3] = {{0, 0, 0},{1, 1, 1} };
OR,
int table[2][3] = { {0, 0, 0},
{1, 1, 1} We can initialize a two
}; dimensional array in the
form of matrix
15
15
OR,
int table[ ][3] = { When the array is completely
{0, 0, 0}, initialized with all values, explicitly,
{1, 1, 1} we need not specify the size of the
}; dimension
*****************************
int table[2][3] = {
{1,2}, If the values are missing in
{2} initialize, they are
}; automatically set to Zero
*****************************
int table[2][3] = {{0}, {0}, {0}};
or, When all the elements are
to be initialized to Zero, this
int table[2][3] = {0, 0}; short-cut may be used 16
16
#include <stdio.h>
int main () {
return 0;
} 17
17
2D matrix Run time Input and Display:
#include<stdio.h>
#define MAX 10
int main()
{ printf("Your entered 2D matrix of %dX%d
int array[MAX][MAX],i, j, r, c; elements:\n", r, c);
for(i=0;i<r; i++)
printf("Enter row and column number:\n"); {
scanf("%d %d", &r, &c); for(j=0;j<c; j++)
{
printf("Enter %d X %d elements:\n", r, c); printf("%5d", array[i][j]);
for(i = 0; i <r; i++) }
{ printf("\n");
for(j=0;j<c; j++) }
{ return 0;
printf("Enter array[%d][%d]: ",i+1,j+1); }
scanf("%d", &array[i][j]);
}
}
18
18
19
19
MULTI-DIMENSIONAL ARRAYS:
datatype arrary_name[s1][s2]…..[si];
here si is size of i-th dimension.
Examples:
int survey[3][5][12]; >>holds: 3*5*12=180 integer type elements<<
20
20
Declaration and Initialization 3D Array :
#include<stdio.h>
int main() {37, 38, 39}
{ },
int i, j, k; };
int arr[3][3][3]=
{ printf(":::3D Array Elements:::\n\n");
{ for(i=0;i<3;i++)
{11, 12, 13}, {
{14, 15, 16}, for(j=0;j<3;j++)
{17, 18, 19} {
}, for(k=0;k<3;k++)
{ {
{21, 22, 23}, printf("%d\t", arr[i][j][k]);
{24, 25, 26}, }
{27, 28, 29} printf("\n");
}, }
{ printf("\n");
{31, 32, 33}, }
{34, 35, 36}, return 0; 21
21
}
22
22
Dynamic Array:
We create arrays at compile time. An array created at compile
time by specifying SIZE in the source code has a fixed size and
cannot be modified at run time. The process of allocating
memory at compile time is known as Static Memory Allocation.