UNIT 3
ARRAYS
Definition of ARRAY:
An array is defined as the collection of similar type of data items stored at contiguous
memory locations.
Arrays are the derived data type in C programming language which can store the
primitive type of data such as int, char, double, float, etc.
It also has the capability to store the collection of derived data types, such as pointers,
structure, etc. The array is the simplest data structure where each data element can be
randomly accessed by using its index number.
C array is beneficial if you have to store similar elements. For example, if we want to store
the marks of a student in 6 subjects, then we don't need to define different variables for the
marks in the different subject. Instead of that, we can define an array which can store the
marks in each subject at the contiguous memory locations.
By using the array, we can access the elements easily. Only a few lines of code are required
to access the elements of the array.
Advantages of Arrays
 In an array, accessing an element is very easy by using the index number.
 The search process can be applied to an array easily.
 2D Array is used to represent matrices.
 For any reason a user wishes to store multiple values of similar type then the Array can be
used and utilized efficiently.
Disadvantages of Arrays
Array size is fixed: The array is static, which means its size is always fixed. The memory which
is allocated to it cannot be increased or decreased.
Array is homogeneous: The array is homogeneous, i.e., only one type of value can be store in the
array. For example, if an array type “int“, can only store integer elements and cannot allow the
elements of other types such as double, float, char so on.
Array is Contiguous blocks of memory: The array stores data in contiguous (one by one)
memory location.
Insertion and deletion are not easy in Array: The operation insertion and deletion over an array
are problematic as to insert or delete anywhere in the array, it is necessary to traverse the array
and then shift the remaining elements as per the operation. This operation cost is more.
Wastage of memory: Allocating more memory than the requirement leads to wastage of memory
space and less allocation of memory also leads to a problem.
Applications of Arrays:
 Array stores data elements of the same data type.
 Maintains multiple variable names using a single name. Arrays help to maintain large data under a
single variable name. This avoids the confusion of using multiple variables.
 Arrays can be used for sorting data elements. Different sorting techniques like Bubble sort,
Insertion sort, Selection sort etc use arrays to store and sort elements easily.
 Arrays can be used for performing matrix operations. Many databases, small and large, consist of
one-dimensional and two-dimensional arrays whose elements are records.
 Arrays can be used for CPU scheduling.
 Lastly, arrays are also used to implement other data structures like Stacks, Queues, Heaps, Hash
tables etc.
Example:
You use arrays all the time in programming. Whenever you have to keep track of an ordered
list of items, you will end up using an array. Be it a list of songs, a list of books, or list of
anything for that matter. Even in the JSON data format, you will often use an array to hold a
list of objects.
Databases generally provide a different functionality, as in they let you keep data over time. You
can retrieve that data and update it and view it from other computers using other web browsers.
You will often retrieve information from a database and store it for local processing in an array.
So arrays are an important concept in programming languages.
Types of Arrays:
In c programming language, arrays are classified into three types. They are as follows...
1. Single Dimensional Array / One Dimensional Array
2. Multi Dimensional Array
 Two Dimensional Array
 Three Dimensional Array
 Four Dimensional Array
1. Single Dimensional Array / One Dimensional Array
In c programming language, single dimensional arrays are used to store list of values of same
datatype. In other words, single dimensional arraysare used to store a row of values. In single
dimensional array, data is stored in linear form. Single dimensional arrays are also called as one-
dimensional arrays, Linear Arrays or simply 1-D Arrays. This type of array consists of only
one subscript.
Declaration of Single Dimensional Array
We use the following general syntax for declaring a single dimensional array…
datatype arrayName [ size ] ;
Example Code
int rollNumbers [60] ;
The above declaration of single dimensional array reserves 60 continuous memory locations of 2
bytes each with the name rollNumbers and tells the compiler to allow only integer values into
those memory locations.
Initialization of Single Dimensional Array
1st Method:We use the following general syntax for declaring and initializing a single
dimensional array with size and initial values.
datatype arrayName [ size ] = {value1, value2, ...} ;
Example Code
int marks [6] = { 89, 90, 76, 78, 98, 86 } ;
The above declaration of single dimensional array reserves 6 contiguous memory locations of 2
bytes each with the name marks and initializes with value 89 in first memory location, 90 in
second memory location, 76 in third memory location, 78 in fourth memory location, 98 in fifth
memory location and 86 in sixth memory location.
2nd Method:We can also use the following general syntax to intialize a single dimensional array
without specifying size and with initial values...
datatype arrayName[ ] = {value1, value2, ...} ;
The array must be initialized if it is created without specifying any size. In this case, the size of
the array is decided based on the number of values initialized.
Example Code
int marks [] = { 89, 90, 76, 78, 98, 86 } ;
char studentName [] = "btechsmartclass" ;
In the above example declaration, size of the array 'marks' is 6 and the size of the
array 'studentName' is 16. This is because in case of character array, compiler stores one extra
character called 0 (NULL) at the end.
Accessing Elements of Single Dimensional Array
In c programming language, to access the elements of single dimensional array we use array name
followed by index value of the element that to be accessed. Here the index value must be enclosed
in square braces.
Index value of an element in an array is the reference number given to each element at the time of
memory allocation. The index value of single dimensional array starts with zero (0) for first
element and incremented by one for each element. The index value in an array is also called
as subscript or indices.
We use the following general syntax to access individual elements of single dimensional array...
arrayName [ indexValue ]
Example Code
marks [2] = 99 ;
In the above statement, the third element of 'marks' array is assigned with value '99'.
Example program refer in observation book “C program for storing and printing 1-D
array”
2. 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
assigned with value 10.
Example program refer in observation book “C program for storing and printing 2-D
array”
Declaration of Three Dimensional Array
Similarly, you can declare a three-dimensional (3d) array.
Here, the array y can hold 24 elements.
Initialization of a 3d array
You can initialize a three-dimensional array in a similar way like a two-dimensional array. Here's
an example,
datatype arrayName [size1][size2][size3] = {set of values} ;
// 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 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] = %dn", 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][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
Various Array Operations
Following are the various operations supported by an array.
 Traverse − print all the array elements one by one.
 Insertion − add an element at given index.
 Deletion − delete an element at given index.
 Search − search an element using given index or by value.
 Update − update an element at given index.
Traverse Operation
In traversing operation of an array, each element of an array is accessed exactly for once for
processing. This is also called visiting of an array.
Insertion Operation
Insert operation is to insert one or more data elements into an array. Based on the requirement,
new element can be added at the beginning, end or any given index of array.
Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all elements of
an array.
Search Operation
You can perform a search for array element based on its value or its index.
Update Operation
Update operationrefersto updating an existing element from the array at a given index.

arrays.docx

  • 1.
    UNIT 3 ARRAYS Definition ofARRAY: An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations. By using the array, we can access the elements easily. Only a few lines of code are required to access the elements of the array. Advantages of Arrays  In an array, accessing an element is very easy by using the index number.  The search process can be applied to an array easily.  2D Array is used to represent matrices.  For any reason a user wishes to store multiple values of similar type then the Array can be used and utilized efficiently. Disadvantages of Arrays Array size is fixed: The array is static, which means its size is always fixed. The memory which is allocated to it cannot be increased or decreased. Array is homogeneous: The array is homogeneous, i.e., only one type of value can be store in the array. For example, if an array type “int“, can only store integer elements and cannot allow the elements of other types such as double, float, char so on. Array is Contiguous blocks of memory: The array stores data in contiguous (one by one) memory location. Insertion and deletion are not easy in Array: The operation insertion and deletion over an array are problematic as to insert or delete anywhere in the array, it is necessary to traverse the array and then shift the remaining elements as per the operation. This operation cost is more. Wastage of memory: Allocating more memory than the requirement leads to wastage of memory space and less allocation of memory also leads to a problem.
  • 2.
    Applications of Arrays: Array stores data elements of the same data type.  Maintains multiple variable names using a single name. Arrays help to maintain large data under a single variable name. This avoids the confusion of using multiple variables.  Arrays can be used for sorting data elements. Different sorting techniques like Bubble sort, Insertion sort, Selection sort etc use arrays to store and sort elements easily.  Arrays can be used for performing matrix operations. Many databases, small and large, consist of one-dimensional and two-dimensional arrays whose elements are records.  Arrays can be used for CPU scheduling.  Lastly, arrays are also used to implement other data structures like Stacks, Queues, Heaps, Hash tables etc. Example: You use arrays all the time in programming. Whenever you have to keep track of an ordered list of items, you will end up using an array. Be it a list of songs, a list of books, or list of anything for that matter. Even in the JSON data format, you will often use an array to hold a list of objects. Databases generally provide a different functionality, as in they let you keep data over time. You can retrieve that data and update it and view it from other computers using other web browsers. You will often retrieve information from a database and store it for local processing in an array. So arrays are an important concept in programming languages. Types of Arrays: In c programming language, arrays are classified into three types. They are as follows... 1. Single Dimensional Array / One Dimensional Array 2. Multi Dimensional Array  Two Dimensional Array  Three Dimensional Array  Four Dimensional Array 1. Single Dimensional Array / One Dimensional Array In c programming language, single dimensional arrays are used to store list of values of same datatype. In other words, single dimensional arraysare used to store a row of values. In single dimensional array, data is stored in linear form. Single dimensional arrays are also called as one- dimensional arrays, Linear Arrays or simply 1-D Arrays. This type of array consists of only one subscript. Declaration of Single Dimensional Array We use the following general syntax for declaring a single dimensional array… datatype arrayName [ size ] ; Example Code int rollNumbers [60] ;
  • 3.
    The above declarationof single dimensional array reserves 60 continuous memory locations of 2 bytes each with the name rollNumbers and tells the compiler to allow only integer values into those memory locations. Initialization of Single Dimensional Array 1st Method:We use the following general syntax for declaring and initializing a single dimensional array with size and initial values. datatype arrayName [ size ] = {value1, value2, ...} ; Example Code int marks [6] = { 89, 90, 76, 78, 98, 86 } ; The above declaration of single dimensional array reserves 6 contiguous memory locations of 2 bytes each with the name marks and initializes with value 89 in first memory location, 90 in second memory location, 76 in third memory location, 78 in fourth memory location, 98 in fifth memory location and 86 in sixth memory location. 2nd Method:We can also use the following general syntax to intialize a single dimensional array without specifying size and with initial values... datatype arrayName[ ] = {value1, value2, ...} ; The array must be initialized if it is created without specifying any size. In this case, the size of the array is decided based on the number of values initialized. Example Code int marks [] = { 89, 90, 76, 78, 98, 86 } ; char studentName [] = "btechsmartclass" ; In the above example declaration, size of the array 'marks' is 6 and the size of the array 'studentName' is 16. This is because in case of character array, compiler stores one extra character called 0 (NULL) at the end. Accessing Elements of Single Dimensional Array In c programming language, to access the elements of single dimensional array we use array name followed by index value of the element that to be accessed. Here the index value must be enclosed in square braces. Index value of an element in an array is the reference number given to each element at the time of memory allocation. The index value of single dimensional array starts with zero (0) for first element and incremented by one for each element. The index value in an array is also called as subscript or indices. We use the following general syntax to access individual elements of single dimensional array... arrayName [ indexValue ] Example Code marks [2] = 99 ; In the above statement, the third element of 'marks' array is assigned with value '99'. Example program refer in observation book “C program for storing and printing 1-D array”
  • 4.
    2. Multi DimensionalArray 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 ;
  • 5.
    In the abovestatement, the element with row index 0 and column index 1 of matrix_A array is assigned with value 10. Example program refer in observation book “C program for storing and printing 2-D array” Declaration of Three Dimensional Array Similarly, you can declare a three-dimensional (3d) array. Here, the array y can hold 24 elements. Initialization of a 3d array You can initialize a three-dimensional array in a similar way like a two-dimensional array. Here's an example, datatype arrayName [size1][size2][size3] = {set of values} ; // 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 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] = %dn", i, j, k, test[i][j][k]); } } }
  • 6.
    return 0; } Output: Enter 12values: 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][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 Various Array Operations Following are the various operations supported by an array.  Traverse − print all the array elements one by one.  Insertion − add an element at given index.  Deletion − delete an element at given index.  Search − search an element using given index or by value.  Update − update an element at given index. Traverse Operation In traversing operation of an array, each element of an array is accessed exactly for once for processing. This is also called visiting of an array. Insertion Operation Insert operation is to insert one or more data elements into an array. Based on the requirement, new element can be added at the beginning, end or any given index of array.
  • 7.
    Deletion Operation Deletion refersto removing an existing element from the array and re-organizing all elements of an array. Search Operation You can perform a search for array element based on its value or its index. Update Operation Update operationrefersto updating an existing element from the array at a given index.