Arrays

What is an array?

The array is a basic data structure which is provided by every programming language. It is a group of related data items that has the same name or share a same name. For example: let us declare an array with the name marks to represent the set of marks of some certain students. Each data item within an array is represented with the index number in brackets after the array name. For example:

marks[5] , this represents the marks of the fifth student whereas the complete set of values known as an array and the individual value are known as element. The array can be of any variable type. There are two types of array:

  • One-Dimensional Array
  • Two-Dimensional Array

One-Dimensional Array

In this type of array the data is stored and represented in the linear form. The one-dimensional array can be represented as:

datatype array name[size];

 In the above syntax the data type is the base type of the array for each element in the array. The array name is constant means it cannot be changed during the execution of the program.

[ ] is subscript operator which is used to mention the size of the array and the size of the array should be a positive value. For example:

int marks[5];

Here the data type is integer and the array name is marks of size 5. The array can hold 5 values of marks. Now we will represent the set of marks like (45, 67, 78, 34, 98) by an array variable marks and the marks in the memory will be stored like this:

     

marks[0]               marks[1]           marks[2]             marks[3]            marks[4]

The values in array can be assigned as:

                                         marks[0]= 45

                                         marks[1]= 67

                                         marks[2]= 78

                                         marks[3]= 34

                                         marks[4]= 98

In memory the marks will be represented as follows:

4567783498

marks[0]                  marks[1]                marks[2]                marks[3]               marks[4]

 

Let us have a look on the program:

#include <stdio.h>

int main(){
     int marks[5],i,n,sum=0;
     printf("Enter the number of students: ");
     scanf("%d",&n);
     for(i=0;i<n;++i){
          printf("Enter the marks of student%d: ",i+1);
          scanf("%d",&marks[i]);
          sum+=marks[i];
     }
     printf("The sum is: %d",sum);
return 0;
}

Output

Enter the number of students: 3

Enter the marks of student1: 34

Enter the marks of student2: 23

Enter the marks of student3: 45

The sum is: 102

Two-Dimensional Array

In two-dimensional array it is a collection of elements that are placed in rows and columns. The syntax for 2-D array is: data type array name[m] [n], where m represents rows and n and represents column. The 2-D array is also known as matrix. A matrix is said to be of order m*n. Some of the operations which can be performed on a matrix are addition, multiplication, transposition and finding determinant of the matrix. The 2-D array can be represented as:

datatype array name[rows]
; int a[3][3] = {3,4,5,6,7,8,9,10,13};

Let us have a look on the example:

 

#include<stdio.h>

 int main() {
   int i, j;
   int a[3][3] = { { 3, 4, 5 },
                 {6, 7, 8},
                 {9, 10, 13}};
   for (i = 0; i < 3; i++) {
      for (j = 0; j < 3; j++) {
         printf("%d ", a[i][j]);
      }
      printf("\n");
   }
   return 0;
 }

Output:

 3   4   5

 6   7   8          

 9   10 13

Operations

Some of the basic operations supported by an array are:

  • Insertion- It will add an element at the given index.
  • Deletion- It will delete the element from a given index.
  • Update- It will update the element at the given index.
  • Traverse- It will print all the array elements one by one.
  • Search- It will search an element from an array by using the index.

Insert Operation:

In the insert operation we insert one or more data elements in an array. The elements can be inserted at the beginning or at the end or at the given index. Insertion of an element at the end of the array is easy to perform but the condition is that the memory space is available to allot the additional elements. If we want to add any element in the middle of an array then it is required that half of the elements must be moved to rightwards to new locations to allot the new element and keep the order of the other elements. Let us have a look on the example:

#include<stdio.h>
#define n 10
#define p n*2

/*Inserting the element at kth position of an array, where k<=n*/
main()
{  
int linear[p], k, item;
int i; //loop index
/*read element in an array*/

for(i=0;i<n;i++)
scanf("%d",&linear[i]);
printf("\nEnter the position and the item:");
scanf("%d %d", &k, &item);

/*insert an element in an array*/
i = n-1;
while(i>=k)
{
   linear[i+1] = linear[i];
    i++;
}

linear[k] = item;
n++;

/*print the array*/
for(i=0;i<=n;i++)
  printf("\nlinear[%d] =", linear[i]);
}

Delete Operation:

The delete operation is a removing one of the existing element from an array and reorganize all elements in an array. If we want to delete the element from the middle of an array then each subsequent element must be moved one location forward of the previous position of the element. The deletion operation is simple as if we want to delete the last element of an array. Let us have a look on the example:

#include<stdio.h>
#define n 10
#define p n*2

main()
{  

int linear[p], k, item, i;
/*read element in an array*/
for(i=0; i<n; i++)
 scanf("%d", &linear[i]);
printf("\nEnter the position:");
scanf("%d", &k);
/*delete the element from an array*/
item= linear[k];
for(i=k; i<n-1;i++)
  linear[i]= linear[i+1];
   n--;
for(i=0;i<n;i++)
  printf("\nlinear[%d]=", linear[i]);
return;
}

 

Search Operation:

We can perform the search operation for the array of elements to search an element based on its value or its index. Let us have a look on the example:

#include <stdio.h>

main() {
   int linear[] = {1,2,3,4,7};
   int item = 5, n = 5;
   int i = 0, j = 0;
   printf("The elements are :\n");

   for(i = 0; i<n; i++) {
      printf("linear[%d] = %d \n", i, linear[i]);
   }

   while( j < n){
      if( linear[j] == item ){
         break;
      }
      j = j + 1;
   }
   printf("The element found %d at position %d\n", item, j+1);
}

 

Update Operation:

The update operation is where we perform updating on the existing element on a given array at a given index. Let us have a look on the example:

#include <stdio.h>

main() {
   int linear[] = {1,2,3,4,8};
   int k = 3, n = 5, item = 10;
   int i, j;
   printf("The array elements are :\n");

   for(i = 0; i<n; i++) {
      printf("linear[%d] = %d \n", i, linear[i]);
   }
   linear[k-1] = item;
   printf("The array elements after updation :\n");

   for(i = 0; i<n; i++) {
      printf("linear[%d] = %d \n", i, linear[i]);
   }
}

 

 

1 Comment Arrays

Leave A Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.