Data Structures and Algorithms - Arrays Prof. Neeraj Bhargava Kapil Chauhan Department of Computer Science School of Engineering & Systems Sciences MDS University, Ajmer
Introduction  Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms.  Following are the important terms to understand the concept of Array.  Element − Each item stored in an array is called an element.  Index − Each location of an element in an array has a numerical index, which is used to identify the element.
Array Representation  Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.
Cont..  Arrays can be declared in various ways in different languages. For illustration, let's take C array declaration.
Properties of the Array  Each element is of same data type and carries a same size i.e. int = 4 bytes.  Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location.
Advantages of Array  Array provides the single name for the group of variables of the same type therefore, it is easy to remember the name of all the elements of an array.  Traversing an array is a very simple process, we just need to increment the base address of the array in order to visit each element one by one.  Any element in the array can be directly accessed by using the index.
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.  Update − Updates an element at the given index.
Program  #include <windows.h>  #include <stdio.h>  #include <stdlib.h>  #define NUM_EMPLOYEE 10   int main(int argc, char *argv[]){  int Salary[NUM_EMPLOYEE], lCount=0,gCount=0,i=0;  printf("Enter employee salary (Max 10)n ");  for (i=0; i<NUM_EMPLOYEE; i++){  printf("nEnter employee salary: %d - ",i+1);  scanf("%d",&Salary[i]);  }   for(i=0; i<NUM_EMPLOYEE; i++){  if(Salary[i]<3000)  lCount++;  else  gCount++;  }   printf("nThere are {%d} employee with salary more than 3000n",gCount);  printf("There are {%d} employee with salary less than 3000n",lCount);  printf("Press ENTER to continue...n");  getchar();  return 0;  }
Assignment  Explain Array in data structure with suitable example and discuss basic operation.

Data structures and algorithms arrays

  • 1.
    Data Structures andAlgorithms - Arrays Prof. Neeraj Bhargava Kapil Chauhan Department of Computer Science School of Engineering & Systems Sciences MDS University, Ajmer
  • 2.
    Introduction  Array isa container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms.  Following are the important terms to understand the concept of Array.  Element − Each item stored in an array is called an element.  Index − Each location of an element in an array has a numerical index, which is used to identify the element.
  • 3.
    Array Representation  Arrayscan be declared in various ways in different languages. For illustration, let's take C array declaration.
  • 4.
    Cont..  Arrays canbe declared in various ways in different languages. For illustration, let's take C array declaration.
  • 5.
    Properties of theArray  Each element is of same data type and carries a same size i.e. int = 4 bytes.  Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location.
  • 6.
    Advantages of Array Array provides the single name for the group of variables of the same type therefore, it is easy to remember the name of all the elements of an array.  Traversing an array is a very simple process, we just need to increment the base address of the array in order to visit each element one by one.  Any element in the array can be directly accessed by using the index.
  • 7.
    Basic Operations Following arethe 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.  Update − Updates an element at the given index.
  • 8.
    Program  #include <windows.h> #include <stdio.h>  #include <stdlib.h>  #define NUM_EMPLOYEE 10   int main(int argc, char *argv[]){  int Salary[NUM_EMPLOYEE], lCount=0,gCount=0,i=0;  printf("Enter employee salary (Max 10)n ");  for (i=0; i<NUM_EMPLOYEE; i++){  printf("nEnter employee salary: %d - ",i+1);  scanf("%d",&Salary[i]);  }   for(i=0; i<NUM_EMPLOYEE; i++){  if(Salary[i]<3000)  lCount++;  else  gCount++;  }   printf("nThere are {%d} employee with salary more than 3000n",gCount);  printf("There are {%d} employee with salary less than 3000n",lCount);  printf("Press ENTER to continue...n");  getchar();  return 0;  }
  • 9.
    Assignment  Explain Arrayin data structure with suitable example and discuss basic operation.