Arrays

 What is an Array?

Whenever we want to work with large number of data values, we need to use that much number of different variables. As the number of variables are increasing, complexity of the program also increases and programmers get confused with the variable names. There may be situations in which we need to work with large number of similar data values. To make this work more easy, C programming language provides a concept called "Array".

 An array is a variable which can store multiple values of same data type at a time.

"Collection of similar data items stored in continuous memory locations with single name".

To understand the concept of arrays, consider the following example declaration.

int a, b, c;

Here, the compiler allocates 2 bytes of memory with name 'a', another 2 bytes of memory with name 'b' and more 2 bytes with name 'c'. These three memory locations are may be in sequence or may not be in sequence. Here these individual variables store only one value at a time.

 


 

 

Now consider the following declaration...

int a[3];

Here, the compiler allocates total 6 bytes of continuous memory locations with single name 'a'. But allows to store three different integer values (each in 2 bytes of memory) at a time. And memory is organized as follows...




That means all these three memory locations are named as 'a'. But "how can we refer individual elements?" is the big question. Answer for this question is, compiler not only allocates memory, but also assigns a numerical value to each individual element of an array. This numerical value is called as "Index". Index values for the above example are as follows...




The individual elements of an array are identified using the combination of 'name' and 'index' as follows...

arrayName[indexValue]

For the above example, the individual elements can be referred as follows...




If I want to assign a value to any of these memory locations (array elements), we can assign as follows...

a[1] = 100;

The result will be as follows...

 


 

Basic Operations

Following are the basic operations supported by an array.

  • Traverse − print all the array elements one by one.

  • Insertion − Adds an element at the given index.

  • Deletion − Deletes an element at the given index.

  • Search − Searches an element using the given index or by the value.

 

Traverse Operation

This operation is to traverse through the elements of an array.

Example
 
    #include <stdio.h>
    #include<conio.h>
    void main ()  
    {  
        int arr[10],i;   
        for (i=0;i<5;i++)  
        {  
              
                printf("Enter a[%d]: ",i);              
                scanf("%d",&arr[i]);  
            
        }  
        printf("\n printing the elements ....\n");   
        for(i=0;i<5;i++)  
        {  
            printf("\n");  
             
                printf("Array[%d]=%d \n",i,arr[i]);  
             
        }
         getch();
    }
  

Output




Insertion Operation
  
Insertion operation is to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array.
  • Input the array elements, the position of the new element to be inserted and the new element.
  • Insert the new element at that position and shift the rest of the elements to right by one position.

Example

    #include <stdio.h>
    #include<conio.h>
    void main ()  
    {  
    int arr[100], position, c, n, value;
    printf("Enter number of elements in array\n");
   scanf("%d", &n);
    printf("Enter %d elements\n", n);
 
   for (c = 0; c < n; c++)
      scanf("%d", &arr[c]);
   printf("Enter the location where you wish to insert an element\n");
   scanf("%d", &position);
  if (position >= n+1)
      printf("Insertion not possible.\n");
   else
   {
       printf("Enter the value to insert\n");
   scanf("%d", &value);
   for (c = n - 1; c >= position - 1; c--)
      arr[c+1] = arr[c];
 
   arr[position-1] = value;
 
   printf("Resultant array is\n");
 
   for (c = 0; c <= n; c++)
      printf("%d\n", arr[c]);
    }  
     getch();
    } 
 
Output 
 
 

 
 
Deletion Operation

Deletion refers to removing an existing element from the array and re-organizing all elements of an array.

  • Input the array elements, the position of the new element to be inserted and the new element.
  • Delete the element and shift the rest of the elements to left by one position.

 

Example 

     #include <stdio.h>
    #include<conio.h>
    void main ()  
    {  
      int arr[100], position, c, n;

   printf("Enter number of elements in array\n");
   scanf("%d", &n);

   printf("Enter %d elements\n", n);

   for (c = 0; c < n; c++)
      scanf("%d", &arr[c]);

   printf("Enter the location where you wish to delete element\n");
   scanf("%d", &position);

   if (position >= n+1)
      printf("Deletion not possible.\n");
   else
   {
      for (c = position - 1; c < n - 1; c++)
         arr[c] = arr[c+1];

      printf("Resultant array:\n");

      for (c = 0; c < n - 1; c++)
         printf("%d\n", arr[c]);
   }
    getch();
    } 
 
 
Output 
 

 

 
 
 
Search Operation

We can perform a search for an array element based on its value or its index.

  • Input the array elements, the element to be searched.
  • Traverse the array and check if the element is present in the array.
  • Display "Yes" if it is present in the array, else display "No".

 

Example
 
    #include <stdio.h>
    #include<conio.h>
    void main ()  
    {  
      int arr[100], ele, c, n,found;

   printf("Enter number of elements in array\n");
   scanf("%d", &n);

   printf("Enter %d elements\n", n);

   for (c = 0; c < n; c++)
      scanf("%d", &arr[c]);

   printf("Enter the element which you want to search \n");
   scanf("%d", &ele);

   found=0;
    for (c = 0; c < n; c++)
    {
        if(arr[c]==ele)
        {
            found=1;
            break;
        
        }
    }
    if(found==1)
    {
      printf("\n %d  is present at position  %d",ele,c+1);    
    }
        
        else
        {
            printf("\n %d  is not Present in the array",ele);
        }
    

    getch();
    }  

Output 


 

 

 

 

 


 

 

 
 






 

 

 

 

 


No comments:

Post a Comment