2.4 Arrays in c programming

 Arrays: -

  • An array is a collection of elements of the same data type stored in contiguous memory locations.
  • It is a derived data type and a homogenous data structure.
  • It is a variable which can store multiple values of the same type under a single variable name, making it easier to manage and manipulate related data.
  • The size of an array is fixed.
  • To access the elements of an array, we use indexes or subscripts accordingly. array index or subscript is used to identify array elements address.
  • Array index or subscript starts with 0 to n-1.

 Declaration of an array: -

  • The declaration of an array means telling the compiler to create an array variable and reserving memory for a fixed number of elements of the same data type.

Initialization of an array: -

  • Initializing an array means assigning initial values to its elements.


Types of arrays: -

  • Arrays are classified based on the number of dimensions they have:

                1. One-dimensional Arrays

                2. Two-dimensional Arrays

                3. Multi-dimensional Arrays

 

1) One-dimensional Arrays: -

  • One-dimensional array is a collection of elements of the same data type that are used to store in a single row in contiguous memory locations using a single index.
  • It is also called as single dimensional array.
  • The elements can be accessed using index which always starts from 0.

            Declaration of One- dimensional Arrays: -


            Initialization of One- dimensional Arrays: -


Operations of one-dimensional array: -

a) Reading array elements: -

  • To read the elements of an array variable, we use single loop (for, while, do while) because we have only one index or subscript in one dimensional array.



b) Printing or accessing array elements: -

  • To print the elements from an array variable, we use single loop (for, while, do while) because we have only one index or one subscript in one dimensional array.



Example 1: -



output: -


Example 2: -



output: -



2) Two-dimensional Arrays: -

  • Two-dimensional array is a collection of elements of the same data type that are used to store in a row and columns in contiguous memory locations using a dual index.
  • It is also called as double dimensional array.
  • The elements can be accessed using index which always starts from row 0 and column 0.

 

Declaration two-dimensional Arrays: - 


Initializing two-dimensional Arrays: -



     Operations of two-dimensional array: -

a) Reading array elements: -

To read the elements of an array variable, we use two loops (for, while, do while) because we have two indexes or subscripts in two-dimensional array.


b) Printing or accessing array elements: -

To print the data or elements from an array variable, we use two loops (for, while, do while) because we have two indexes or subscripts in two-dimensional array.


Example1: -



output: -



Example 2: -



output: -



3) Multi-dimensional Arrays: -

  • A multi-dimensional array is an array of arrays.
  • It allows us to store data in a tabular form (like rows and columns) or even in higher dimensions.
  • Multi-dimensional arrays can be termed as nested arrays.

       Declaration of multi-dimensional array: - 


Initialisation of multi-dimensional array: - 


Example: -



output: -


Dynamic Arrays: -

  • A dynamic array is an array whose size can be changed during runtime and can be resized (increased or decreased) during program execution.
  • unlike static arrays which have a fixed size determined at compile time.
  • Dynamic arrays use memory allocation functions and pointers to manage memory from the heap.

            Key Functions for Dynamic Arrays: - these functions used from <stdlib.h>


Example: -




output: -




Popular posts from this blog

operators in c programming

Variables in c