Array Illustration

array illustration

ARRAY NOTATION:

An array is a group of likely typed variables that are referred to by a common name. Every specific element in an array is accessed by its index addresses which are meant to be a convenient means of grouping related information which is consisted of contiguous memory locations.

In other words, an array is a group or a table of values generally referred by the same variable name. The values in the array are called elements (also known as variables).

Arrays of any datatype(integral, float, character) can be created and with any of the specifications: one-dimensional, 2 - dimensional, or multiple dimensional.

PROPERTIES

1.Array can store multiple values for the same data type at a time.

2. The items are stored contiguously in serialized memory locations addressed with a single group name.

3. Arrays can't be resized dynamically, now this may cause total assigned memory to be wasted or sometimes insufficient for the procedures.

4.Access time is constant for each element. i.e. O(1).

ADVANTAGES

  • Simple and easy to use
  • Faster access to the elements (constant access)

DISADVANTAGES

  • Preallocates all required memory upfront units and wastes unused space(memory) of indices in the
  • Static(Fixed) in size: The size of the array is static as we need to specify the array size before using it.
  • One-block allocation: if the array size is big, sometimes it may not be possible to get the memory for the complete array thus need to allocate the array itself at the beginning cannot be accomplished always.
  • Complex position-based insertion: If we need to insert an element at a given position, we may need to shift the existing elements to the next indices. This will create a position for us to insert the new element at the desired position. If the place at which we want to add an element is the beginning, then the shifting operation will be more expensive.

WHY USE ARRAYS OVER VARIABLES

The reason to use arrays is that every element can be accessed by its index value. This has several advantages over storing a bunch of variables for an operation.

For example

If we need to store marks of all students in a university.

So, here we can implement the concept of the array for easy implication as it can hold up the same types of information in a significant order. Also, each student's mark can be accessed using their indices.

COMPLEXITY ANALYSIS OF DIFFERENT OPERATIONS CARRIED OUT :

Operation

Complexity

access

O(1)

search

O(n)

insertion

O(n)

deletions

O(n)

Space

O(n)

LINEAR ARRAY DECLARATION:

type name[size];

  • type: int, float, char, or other data types can be used to get the array inputs with certain values.
  • name: user-defined name to be chosen for reference in future usage.
  • size: mention in these [square brackets],

EXAMPLE:  int hollow[12].

REPRESENTATION IN MEMORY:

Array-based addresses start with zero address at the starting index and the last the index corresponds to the largest addresses represented by n(consecutive elements) -1.

INITIALIZATION

The values of the elements can be assigned while declaring the array. If some of the values are not explicitly defined, they are set to 0.

  1. int marks[10] = {5, 10, 20, 30, 40, 60};

By default, an array is created whenever memory is available at any If array elements are not initialized while creation, then accessing them directly they would result in such problems.

Therefore, it is always recommended to empty the elements or assign values to it if a calculation is to be performed on the array.

    int ages[10];

    // accessing array without assigning elements first

    for(int i = 0; i < 10; i++)

    printf("\n arr[%d] = %d", i, ages[i]);

Each element of the array can be accessed using its index. The indexing in an array generally starts with 0, which means that the first element is at the 0th index. Subsequently, the last element of the array would be at the (n-1) index. This is known as

The indexing of the array may also be different by using any other base. These are known as

Accessing all the elements are possible by using a simple for-loop going through all the indices in the array.

for(int i = 0; i < arraySize; i++)

printf("\n arr[%d] = %d", i, arr[i];


Post a Comment

0 Comments