ARRAY
Unit Structure :
2.0 Objectives
2.1 Introduction
2.2 One Dimensional Array- Memory Representation, Traversing,
Insertion, Deletion, Searching, Sorting, Merging of Arrays.
2.3 Multidimensional Arrays- Memory Representation, General
Multidimensional arrays
2.4 Sparse Arrays- Sparse Matrix, Memory Representation of special
kind of matrices
2.5 Advantages and Limitations of Arrays
2.6 Summary
2.7 References
2.8 Questions
2.0 OBJECTIVES
At the end of this unit, the student will be able to
✔ Describe the memory representation of one dimensional array and the
operation on array.
✔ Illustrate the concept of M-Dimensional array.
✔ Explain the need of Sparse array.
✔ Compare and constrast between different types of arrays.
2.1 INTRODUCTION OF ARRAYS
● An array is a data structure used to process multiple elements with the
same data type when a number of such elements are known.
● Arrays form an important part of almost all-programming languages.
● It provides a powerful feature and can be used as such or can be used
to form complex data structures like stacks and queues.
● An array can be defined as an infinite collection of
homogeneous(similar type) elements.
● This means that an array can store either all integers, all floating point
numbers, all characters, or any other complex data type, but all of
same type.
● Arrays are always stored in consecutive memory locations.
● Types of Arrays
15
, There are two types of arrays
● One Dimensional Arrays
● Two Dimensional Arrays
2.2 ONE DIMENSIONAL ARRAY- MEMORY
REPRESENTATION, TRAVERSING, INSERTION,
DELETION, SEARCHING, SORTING, MERGING OF
ARRAYS
1. One Dimensional Arrays
● A one-dimensional array is one in which only one subscript
specification is needed to specify a particular element of the array.
● A one-dimensional array is a list of related variables. Such lists are
common in programming.
● One-dimensional array can be declared as follows :
Data_type var_name[Expression];
2. Initializing One-Dimensional Array
2.1 ANSI C allows automatic array variables to be initialized in
declaration by constant initializers as we have seen we can do for scalar
variables.
2.2 These initializing expressions must be constant value; expressions with
identifiers or function calls may not be used in the initializers.
2.3 The initializers are specified within braces and separated by commas
as shown in below declarations
int ex[5] = { 10, 5, 15, 20, 25} ;
char word[10] = { 'h', 'e', 'l', 'l', 'o' } ;
2.4 Example
// Program to print the element of Array
#include <stdio.h>
int main()
{
/*an array with 5 rows and 2 columns*/
int a[5][2] = {{0,0},{1,2},{2,4},{3,6},{4,8}};
int i,j;
/* Output each array elements value*/
for(i=0; i<5; i++);
{
for(j=0; j<2; j++);
{
printf("a[%d][%d]=%d",i,j,a[i][j]);
}
}
return 0;
16