Array

Array

Array is a collection of data elements, all of same data type, accessed using a common name. Array elements are placed in contiguous memory location. Array is one type of data structure that can store a fixed-size sequential collection of items of the same type. An array is a variable that can store multiple values. Array is the collection of homogeneous data elements.

Array structure
Declaration:

To declare array in C, a programmer specifies the data type of items, array name and numbers of items required by an array as follows -

One dimensional array:
type arrayname[arraysize];

This is single-dimentional array. Array size must be positive integer.

Two dimensional array:
type arrayname[i][j];

here: i= numbers of rows and j= numbers of columns
This is called 2-Dimentional array. 2D array represent matrices.
Example:
Types of array:

In C programming language, there is two type of array-

  1. One dimentional(1-D) Array
  2. Two dimentional(2-D) Array or Multi-Dimentional array

Advantages of array:

  1. Array represent multiple data elements of the same type using a single name.
  2. In arrays, the elements can be accessed randomly by using the index number.
  3. Array consist of contiguous memory locations therefore there is no chance of extra memory being allocated in case of arrays
  4. 2D Array is used to represent matrices.
  5. Using arrays, we can implement other data structure like stack, queues, linked list, trees, graphs etc.

Disadvantages of Array:

  1. Using arrays, we can implement other data structure like stack, queues, linked list, trees, graphs etc.
  2. Allocating more memory than the requirement leads to wastage of memory and less allocation of memory leads problem.
  3. Insertion and deletion quit difficult in an arrays.
  4. The array is homogeneous(means only one type of value can be store in the array.

Applications:

Sample Programs

1. Initialization of 1-D array at compile time
                // Print the elements stored in the array
                
                 
                #include<stdio.h> 
                #include<conio.h> 
            
            
            
                void main()
                 {
                    int a[5]={1,2,3,4,5};
                    clrscr();
                 
                    // printing elements of an array
    
                    for(int i = 0; i < 5; i++) 
                    {
                        printf("%d\n", a[i]);
                    }
                    getch();
                }
output:
                1
                2
                3
                4
                5
            
2. Initialization of 1-D array at run time
            // Print the elements stored in the array
            
            
                 
                #include<stdio.h> 
                #include<conio.h> 
            
                 void main()
                  {
                       int a[5];
                       clrscr();
                 
                       printf("Enter 5 elements in the array/n");
                       scanf("&d", &a[i]);
                 
                   // taking input and storing it in an array
              
                       for( i = 0; i < 5; i++) {
                       scanf("%d", &a[i]);
                       }
                
                       printf("Displaying array elements/n");
            
                       // printing elements of an array
                
                       for(int i = 0; i < 5; i++) 
                       {
                             printf("%d\n", a[i]);
                       }
                       getch();
                   }
            
Output:
                Enter 5 elements in the array
                10
                20
                30
                40
                50
                Displaying array elements
                10
                20
                30
                40
                50
            
3.Sum of array 1-D elements
//Print sum of array elements
               
    #include<stdio.h> 
  
    int main()
        {
           int array[5];
           int sum, i;
           sum = 0;
           printf("Enter values in 1-D array/n")
           for(i = 0; i < 6; i++); 
              {
                 scanf("%d",array[i]);
              }
           for(i = 0; i < 6; i++) 
              {
                 sum = sum + array[i];      
              }
            
           printf("Sum of array is %d", sum);   
            
           return 0;
        }
     
Output:
        Enter values in 1-D array
        1
        2
        3
        4
        5
        Sum of array is 15
    
4. Initialization of 2-D array at compile time
            //program for compile time array initialization

             
            #include<stdio.h> 


            void main()
            {
              int a[2][2]={{1,2},{3,4}};
              int i,j;

              printf(“Array elements are\n”);

              for(i=0;i<2;i++)
                {
                   for(j=0;j<2;j++)
                     {
                       printf(“%d\n”,a[i][j]);
                     }
                }
               return 0;
 
            }
          
Output:
        Array elements are
               1   2
               3   4
5. Initialization of 2-D array at run time
        //  Print the elements stored in the array

         
        #include<stdio.h> 


         void main()
         {
          int a[2][2];
          int i,j;

          printf(“Enter the array elements \n”);

          for(i=0;i<2;i++)
            {
            for(j=0;j<2;j++)
              {
                scanf(“%d”,&a[i][j]);
              }
            }
          printf(“Array elements are \n”);

           for(i=0;i<2;i++)
              {

                for(j=0;j<2;j++)
                  {
                    printf(“%d\n”,a[i][j]);
                  }
               }

            return 0;  
          }

Output:
       Enter the array elements
       5
       6
       7
       8
       Array elements are
       5   6   
       7   8
Examples For Practice
  1. Implement C program to find smallest element in a 1-D array
  2. Implement C program to find the average of N elements of 1-D array. (Read N from user).
  3. Implement C program to find the sum of all elements in a 2-D array.
  4. Implement C program to display even and odd numbers from given 2-D array