Breaking Posts

6/trending/recent

Hot Widget

Type Here to Get Search Results !

Arrays

  • Arrays are collections of data elements of the same type that are stored in contiguous memory regions.
  • Arrays are a derived data type in the C programming language that may hold primitive data types like int, char, double, float, and so on.
  • The simplest data structure is an array, which allows each data element to be accessed at random using its index number.
  • For example, if we want to record a student's grades in six subjects, we don't need to create separate variables for each subject's grades. 
  • Instead, we can create an array that can be used to hold the marks in each subject in contiguous memory locations.
Arrays in Data Structure

The array markings[20] contains the student's marks in twenty separate subjects, each of which is represented by a different subscript in the array, such as marks[0] for the first subject, marks[1] for the second subject, and so on.

The array's properties.

  • Each element has the same data type and size, namely int = 2 bytes.
  • The array's elements are kept in contiguous memory regions, with the first element kept in the smallest memory place.
  • Because we can calculate the address of each element of the array with the supplied base address and data element size, we can access elements of the array at random.
For example, in the C programming language, declaring an array looks like this:

int arr[20]; 
char arr[20]; 
float arr[5] 

Why do we need to use an array?

  • In most circumstances, computer programming necessitates the storage of a huge number of data of comparable type. 
  • We need to define a big number of variables to store such a large amount of data. While writing the programmes, it would be extremely tough to remember the names of all the variables. 
  • It is preferable to build an array and store all the elements in it rather than naming all the variables with various names.
The following example shows how arrays can be useful when developing code to solve a specific problem.
  • In the example below, we have an exam scores in six different subjects. The goal of the challenge is to determine the average of all of the student's grades.
  • To demonstrate the importance of arrays, we constructed two programmes, one without utilizing arrays and the other using arrays used 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 by using 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);   
}   

The Benefits of Array

  • Because an array gives a single name for a set of variables of the same type, it is simple to remember the names of all the array's elements.
  • Traversing an array is a simple task; all we have to do is increase the array's base address to visit each element one by one.
  • The index can be used to directly access any element in the array.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.