× Home
Next Lec → ← Previous Lec

Arrays

Most Commonly used dimensions of Array :

Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as mactires, and use the general term arrays when the number of dimensions is unspecified or unimportant.

Why do we need arrays?

Advantages of arrays

  • It is used to represent multiple data items of same type by using only single name.
  • Accessing an item in a given array is very fast!
  • 2 Dimensional arrays makes it easy in mathematical applications as it is used to represent a matrix.

Properties of array

  • Data in an array is stored in contiguous memory locations in RAM
  • Each element of an array is of same size
  • Any element of the array with given index can be accessed very quickly by using its address which can be calculated using the base address and the index.
  • Index No. - It is the special type of no. which allows us to access variables of Arrays i.e. index no. provides a method to access each element of an array in a program.

Syntax for declaring and initializing an array

  • data_type name[size];
  • data_type name[size] = {x,y,z,.....}; //size not required in this case!
  • data_type name[rows][columns]; //for 2-d arrays
  • we can also initialize the array one by one by accessing it using its index:
    • name[0]=0;
                            

                                    #include <stdio.h>

                                    int main()
                                    {
                                        int marks[4];
                                        int marks2[]={2,3,55,33,553}; //declaration with initialization
                                        // marks[0]=34;
                                        // printf("Marks of student 1 is %d\n", marks[0]);
                                        // marks[0]=454;
                                        // printf("Marks of student 1 is %d\n", marks[0]);
                                        // for (int i = 0; i < 4; i++)
                                        // {
                                        // printf("Enter the value of %d element of the array\n", i);
                                        // scanf("%d",&marks[i]);
                                        // }
                                        // for (int i = 0; i < 4; i++)
                                        // {
                                        // printf("The value of %d element of the array is %d\n",i, marks[i]);
                                    
                                        // }
                                    
                                        //2d array
                                       int student[][4]={{1,2,3,4},{3,4,5,6}}; //here we have to declare atleast with column numbers
                                       //printing the array
                                    
                                       for ( int i = 0; i < 2; i++) //double for loop in 2d array
                                        {
                                       for (int j = 0; j < 4; j++)
                                        {
                                        printf("Value of %d, %d element of the array is %d\n",i,j,student[i][j]);
                                        }
                                    
                                        }
                                       return 0;
                                    } 
                            
                        

Disadvantages of using arrays

  • Poor time complexity of insertion and deletion operation
  • Wastage of memory since arrays are fixed in size.
  • If there is enough space present in the memory but not in contiguous form, you will not be able initialize your array.
  • It is not possible to increase the size of the array, once you have declared the array.