Monday, July 22, 2024

ARRAYS

 ARRAY:

    An array is a collection of similar data elements. These data elements have the same data type. The elements of the array are stored in consecutive memory locations and are referenced by an index (also known as the subscript). The subscript is an ordinal number which is used to identify an element of the array.

DECLARATION OF ARRAYS:

An array must be declared before being used

  • Data type—the kind of values it can store, for example, int, char, float, double.
  • Name—to identify the array.
  • Size—the maximum number of values that the array can hold.

Arrays are declared using the following syntax:
    
 Type  name  [size];
int    marks   [10];
The type can be either int, float, double, char, or any other valid data type

ACCESSING THE ELEMENTS OF AN ARRAY:
The code accesses every individual element of the array and sets its value to –1. In the for loop, first the value of marks[0] is set to –1, then the value of the index (i) is incremented and the next value, that is, marks[1] is set to –1. The procedure continues until all the 10 elements of the array are set to –1.
Calculating the Address of Array Elements:
Since an array stores all its data elements in consecutive memory locations, storing just the
base address, that is the address of the first element in the array, is sufficient. The address of other data elements can simply be calculated using the base address. The formula to perform this calculation is,
Address of data element, A[k] = BA(A) + w(k – lower_bound)

Here, A is the array, k is the index of the element of which we have to calculate the address, BA is the base address of the array A, and w is the size of one element in memory

EXAMPLE:
Given an array int marks[ ] = {99,67,78,56,88,90,34,85} , calculate the address of marks[4] if the base address = 1000.

We know that storing an integer value requires 2 bytes, therefore, its size is 2 bytes.
marks[4]   = 1000 + 2(4 – 0)
= 1000 + 2(4) = 1008

Calculating the Length of an Array

the length of an array is 

Length = upper_bound – lower_bound + 1

 where upper_bound is the index of the last element and lower_bound is the index of the first element

in the array


STORING VALUES IN ARRAYS
There are three ways to store values in an array
Initializing Arrays during Declaration:
When an array is initialized, we need to provide a value for every element in the array. Arrays are initialized by writing,
type array_name [size] ={ list of values };


Inputting Values from the Keyboard:
An array can be initialized by inputting values from the keyboard.In this method, a while/do–while or a for loop is executed to input the value for each element of the array.
int i, marks [1 ];
for(i= ;i<1 ;i++)
scanf("%d", &marks[i]);

Assigning Values to Individual Elements:

The third way is to assign values to individual elements of the array by using the assignment operator.
A simple assignment statement can be written as
marks[3] = 100;
Here, 100 is assigned to the fourth element of the array which is specified as marks[3].
    
Note that we cannot assign one array to another array, even if the two arrays have the same type and size. To copy an array,you must copy the value of every element of the first array into the elements of the second array.

Code to copy an array at the individual element level
int i, arr1[1 ], arr2[1 ];
arr1[ 1 ] = {0,1,2,3,4,5,6,7,8,9 };
for(i= 0 ; i<1 ; i++)
arr2[i] = arr1[i];

Fill an array with even numbers:
int i, arr[1 ] ;
for(i=0; i<10; i++)
arr[i] = i*2;

OPERATIONS ON ARRAYS:
There are a number of operations that can be preformed on arrays. These operations include:
  1. Traversing an array
  2. Inserting an element in an array
  3. Searching an element in an array
  4. Deleting an element from an array
  5. Merging two arrays
  6. Sorting an array in ascending or descending order

No comments:

Post a Comment

Binary Search Trees(BST)

  Binary Search Trees (BST): A binary search tree, also known as an ordered binary tree. In a binary search tree, all the nodes in the left ...