Fifth Chapter Lesson-19 Array in C programming language

At the end of this lesson-

  • 1. You will be able to explain array.
  • 2. You will be able to explain types of array.
  • 3. You will be able to declare an array and assign the value of that array.
  • 4. You will be able to explain the advantages and disadvantages of using array. 

 

Array: Array is a kind of data structure which is a collection of variables of same data type. Array is called derived data type. An array is used to store group of data of same data type. Types of array-

  • 1. One-dimensional array
  • 2. Multidimensional array (two-dimensional,…)

 

ONE-DIMENSIONAL ARRAY IN C: A one-dimensional array (or single dimension array) is a type of linear array. Accessing its elements involves a single subscript which can either represent a row or column index.

Declaration of one dimensional Array in C:

Data_Type Array_Name [ array_size ]; 

array_size defines how many variables the array hold.The array_size must be an integer constant greater than zero. Array_Name is just a name of array followed by how to name a variable. Data_Type indicates the data type of the data that will be stored in the array. Data_Type can be any valid C data type.

Examples-

  • int id [5];
  • float marks[5];

Explain: When int id [5]; is declared as array, then five int type variables are declared as the following.

  • int id [0];
  • int id [1];
  • int id [2];
  • int id [3];
  • int id [4];

 

Initialization of an Array: The values of an array can be initialized in three ways. They are-

  • 1. At the time of array declaration
  • 2. After array declaration
  • 3. At the time of program execution

 

At the time of array declaration: The following way how an array (int id [5];) can be initialized.

int id [5]={101, 102, 103, 104, 105};

The vales will be assigned in array as follows –

  • int id [0]=101;
  • int id [1]=102;
  • int id [2]=103;
  • int id [3]=104;
  • int id [4]=105;

After array declaration: The following way how an array (int id [5];) can be initialized after array declaration.

int id [5]; // array declaration

Than value assign to the array-

  • id [0]=101;
  • id [1]=102;
  • id [2]=103;
  • id [3]=104;
  • id [4]=105;

At the time of program execution: Single variable of array can be initialized normally. All the variables of array can also be initialized using loop statement. Using loop statement, int id [5]; array initialization-

 

TWO DIMENSIONAL ARRAY IN C: C language supports multidimensional arrays also. The simplest form of a multidimensional array is the two-dimensional array. Two dimensional array is nothing but array of array. Both the row’s and column’s index begins from 0.

Declaration of two dimensional Array in C:

Data_Type Array_Name [ row_size ][ column_size ]; 

row_size and column_size indicate number of row and column of array respectively. The row_size and column_size must be an integer constant greater than zero. Array_Name is just a name of array followed by how to name a variable. Data_Type indicates the data type of the data that will be stored in the array. Data_Type can be any valid C data type.

Example: To store the elements of a 3×4 matrice in an array, It should be declared as follows-

int mat [3][4];

 

Advantages of using array:

  • 1. It can be used to represent multiple data items of same type by using only single name.
  • 2. It can be used to implement other data structures like linked lists, stacks, queues, trees, graphs etc.
  • 3. Iterating the arrays using their index is faster compared to any other methods like linked list etc.
  • 4. It does not allocate any extra space/ memory for its elements. Hence there is no memory overflow or shortage of memory in arrays.
  • 5. 2D arrays are used to represent matrices.

Disadvantages of using array:

  • 1. We must know in advance that how many elements are to be stored in array.
  • 2. Array is static structure. It means that array is of fixed size. The memory which is allocated to array can not be increased or reduced.
  • 3. Since array is of fixed size, if we allocate more memory than requirement then the memory space will be wasted. And if we allocate less memory than requirement, then it will create problem.
  • 4. The elements of array are stored in consecutive memory locations. So insertions and deletions are very difficult and time consuming.

 

Lesson Evaluation-

Knowledge Based Questions:

a. What is array?

Comprehension Based Questions:

  • b. ‘Array and variable are not same’-explain.
  • b. ‘Array reduces the complexity of program’-explain.
  • b. ‘Same type of multiple data can be stored in a single name’-explain. 

Creative Questions:

Multiple Choice Questions:

 


Written by,

Leave a Reply

Your email address will not be published. Required fields are marked *