0% found this document useful (0 votes)
5 views

What is Array

An array is a collection of similar data items stored at contiguous memory locations, allowing random access via index numbers. In C programming, arrays are derived data types that can hold primitive data types, with fixed sizes and zero-based indexing for element access. Basic operations on arrays include insertion, deletion, searching, updating, and displaying elements.

Uploaded by

deena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

What is Array

An array is a collection of similar data items stored at contiguous memory locations, allowing random access via index numbers. In C programming, arrays are derived data types that can hold primitive data types, with fixed sizes and zero-based indexing for element access. Basic operations on arrays include insertion, deletion, searching, updating, and displaying elements.

Uploaded by

deena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

What is Array?

Explain the storage structure for


arrays in detail

Arrays are defined as the collection of similar types of data items stored at
contiguous memory locations. It is one of the simplest data structures where
each data element can be randomly accessed by using its index number.

In C programming, they are the derived data types that can store the primitive
type of data such as int, char, double, float, etc. For example, if we want to
store the marks of a student in 6 subjects, then we don't need to define a
different variable for the marks in different subjects. Instead, we can define an
array that can store the marks in each subject at the contiguous memory
locations.

Let's make this concrete with an example. Say we have an array of 5 integers
declared like this: int numbers[5] = {10, 25, 8, 33, 19};

The elements are all ints (4 bytes each on a typical machine) and live together
in a continuous chunk of 20 bytes of memory. We can access any element
directly using its index - numbers[0] is 10, numbers[3] is 33, etc.

There are some of the properties of an array that are listed as follows -

o Each element in an array is of the same data type and carries the same
size of 4 bytes.
o Elements in the array are stored at contiguous memory locations, and the
first element is stored at the smallest memory location.
o 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
the data element's size.
o Fixed Size - The size of an array is fixed and cannot be changed after it
is created. You decide how many elements it can hold during the
declaration.
For example, Declaring an array of 10 integers will always hold exactly 10
integers. No more, no less.
o Index-based Access - Each element in the array is accessed using a
numerical index. The indices usually start from 0.
Like this: myArray[0], myArray[1], myArray[2] and so on.

Representation of an array

We can represent an array in various ways in different programming languages.


As an illustration, let's see the declaration of array in C language -

o The first thing to note is those square brackets that come after the name.
That's how you indicate an array in code. The number inside indicates
the capacity of this array. In this case, it's a 10-element array.
o Here's a catch. Each element in the array is assigned an index value. This
is how you identify and access elements. Here's the catch. The indices
start from 0, not 1, as you might expect.
o So the initial element is at index 0, the second at index 1, and so forth
until you reach the element at index 9 for this array, with 10 elements. It
may seem a bit odd. Just go along with these zero-based indices.
o When it comes to accessing elements, the syntax is quite simple. Just use
the array name followed by the index number of the element you want
within those brackets. For example, arrayName[indexNumber].
o Another intriguing aspect to highlight. All its elements are arranged
sequentially in a block when you allocate memory for an array.
o It feels akin to a group of soldiers standing in formation. This condensed
setup contributes to the efficiency of arrays when performing tasks.
Array Representation

Arrays are represented as a collection of buckets where each bucket stores one
element. These buckets are indexed from '0' to 'n-1', where n is the size of that
particular array. For example, an array with size 10 will have buckets indexed
from 0 to 9.

This indexing will be similar for the multidimensional arrays as well. If it is a


2-dimensional array, it will have sub-buckets in each bucket. Then it will be
indexed as array_name[m][n], where m and n are the sizes of each level in the
array.

As per the above illustration, following are the important points to be


considered.

 Index starts with 0.


 Array length is 9 which means it can store 9 elements.
 Each element can be accessed via its index. For example, we can fetch an
element at index 6 as 23.
Basic Operations in Arrays

The basic operations in the Arrays are insertion, deletion, searching, display,
traverse, and update. These operations are usually performed to either modify
the data in the array or to report the status of the array.

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.
 Display − Displays the contents of the array.

You might also like