Explain the characteristics and operations of arrays in C language

CServer Side ProgrammingProgramming

An array is a homogeneous sequential collection of data items over a single variable name.

For example, int student[30];

Here, student is an array name holds 30 collection of data item, with a single variable name.

Characteristics

The characteristics of arrays are as follows −

  • An array is always stored in consecutive memory location.

  • It can store multiple value of similar type, which can be referred with single name.

  • The pointer points to the first location of memory block, which is allocated to the array name.

  • An array can either be an integer, character, or float data type that can be initialised only during the declaration.

  • The particular element of an array can be modified separately without changing the other elements.

  • All elements of an array can be distinguishing with the help of index number.

Operations

The operations of an array include −

  • Searching − It is used to find whether particular element is present or not.

  • Sorting − Helps in arranging the elements in an array either in an ascending or descending order.

  • Traversing − Processing every element in an array, sequentially.

  • Inserting − Helps in inserting elements in an array.

  • Deleting − helps in deleting the element in an array.

Example Program

Following is the C program for searching an element in an array

 Live Demo

#include <stdio.h>
#define MAX 100 // Maximum array size
int main(){
   int array[MAX];
   int size, i, search, found;
   printf("Enter size of array: ");
   scanf("%d", &size);
   printf("Enter elements in array: ");
   for(i=0; i<size; i++){
      scanf("%d", &array[i]);
   }
   printf("\nEnter element to search: ");
   scanf("%d", &search);
   found = 0;
   for(i=0; i<size; i++){
      if(array[i] == search){
         found = 1;
         break;
      }
   }
   if(found == 1){
      printf("\n%d is found at position %d", search, i + 1);
   } else {
      printf("\n%d is not found in the array", search);
   }
   return 0;
}

Output

The output is as follows −

Enter size of array: 5
Enter elements in array: 11 24 13 12 45
Enter element to search: 13
13 found at position 3found
raja
Published on 10-Mar-2021 09:55:03
Advertisements