Array in data structures and algorithm



Array in data structures and algorithm :
  1. Arrays area unit outlined because the assortment of comparable kind of information things hold on at contiguous memory locations.
  2. Arrays area unit the derived information kind in C artificial language which might store the primitive kind of information like int, char, double, float, etc.
  3. Array is that the simplest arrangement wherever every information component are often willy-nilly accessed by victimization its index.
  4. For example, if we wish to store the marks of a student in half-dozen subjects, then we do not got to outline totally different variable for the marks in several subject. rather than that, we will outline AN array which might store the marks in every subject at a the contiguous memory locations.
Properties of array :
  1. Each and every element is of same data type and carries a same data size i.e int = 4 bytes.
  2. Elements of the array are stored at contiguous memory locations where first element is stored at smallest memory locations.
  3. Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and size of the data element.
Need of using Array :

In creating by mental acts, the foremost of the cases needs to store the big variety of knowledge of comparable kind. To store such quantity of knowledge, we want to outline an outsized variety of variables. it'd be terribly tough to recollect names of all the variables whereas writing the programs. rather than naming all the variables with a special name, it's higher to outline AN array and store all the weather into it.

Following example illustrates, however array is helpful in writing code for a specific downside.

In the following example, we've marks of a student in six completely different subjects. the matter intends to calculate the typical of all the marks of the coed.

In order as an example the importance of array, we've created 2 programs, one is while not victimization array and different involves the utilization of array to store marks.

Program without Array :
 #include <stdio.h>   
 void main ()   
 {   
   int marks_1 = 56, marks_2 = 78, marks_3 = 88, marks_4 = 76, marks_5 = 56, marks_6 = 89;    
   float avg = (marks_1 + marks_2 + marks_3 + marks_4 + marks_5 +marks_6) / 6 ;    
   printf(avg);    
 }   

Program with Array :

 #include <stdio.h>   
 void main ()   
 {   
   int marks[6] = {56,78,88,76,56,89);   
   int i;    
   float avg;   
   for (i=0; i<6; i++ )    
   {   
     avg = avg + marks[i];    
   }    
   printf(avg);    
 }    

Advantages of Array :

  1. Array provides the only name for the cluster of variables of a similar sort so, it's simple to recollect the name of all the weather of associate array.
  2. Traversing associate array may be a terribly easy method, we tend to simply got to increment the bottom address of the array so as to go to every part one by one.
  3. Any part within the array will be directly accessed by exploitation the index.


For Videos Join Our Youtube Channel: Join Now