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

UNIT2 ARRAY

array in Data structure in c

Uploaded by

pk1225j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

UNIT2 ARRAY

array in Data structure in c

Uploaded by

pk1225j
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction To Array

What is Array?
An array is a group of similar elements or data items of the same type collected
at contiguous memory locations. In simple words, we can say that in computer
programming, arrays are generally used to organize the same type of data.
Array for Integral value:

Array for Character value:

Representation of an Array:
Arrays always store the same type of values. In the above example:
• int is a type of data value.
• Data items stored in an array are known as elements.
• The location or placing of each element has an index value.
Important: Array can store only the same type of data items. From the below
example we can see how it works:

• In the array a, we have stored all integral values (same type)


• In the array b, we have stored all char values (same type)
• In the array c, there is integral, float, char all types of values and this is
not something an array can store so, option 3 is wrong because an array
cannot store different types of values.
Declaration Syntax of Array:
VariableType VariableName[Sequence of Elements];
Example 1: For integral value
int A[10];
Here 10 means, this array A can have 10 integer elements.

2 5 8 44 21 11 7 9 3 1

Example 2: For character value


char B[10];
This array B can have 10 character elements.
f d a b n j l s e y

Initialization of an Array:
If an array is described inside a function, the elements will have garbage value.
And in case an array is static or global, its elements will be initialized
automatically to 0.
We can say that we can simply initialize elements of an array at the time of
declaration and for that, we have to use the proper syntax:
Syntax: datatype Array_Name[size] = { value1, value2, value3, ….. valueN };
Types of Arrays:
There are two types of arrays:
• One-Dimensional Arrays
• Multi-Dimensional Arrays
One -Dimensional Arrays
A one-dimensional array is a kind of linear array. It involves single sub-scripting.
The [] (brackets) is used for the subscript of the array and to declare and access
the elements from the array.
Syntax: DataType ArrayName [size];
For example: int a[10];
Multi-Dimensional Arrays
In multi-dimensional arrays, we have two categories:
• Two-Dimensional Arrays
• Three-Dimensional Arrays
1. Two-Dimensional Arrays
An array involving two subscripts [] [] is known as a two-dimensional array.
They are also known as the array of the array. Two-dimensional arrays are
divided into rows and columns and are able to handle the data of the table.
Syntax: DataType ArrayName[row_size][column_size];
For Example: int arr[5][5];

2. Three-Dimensional Arrays
When we require to create two or more tables of the elements to declare the
array elements, then in such a situation we use three-dimensional arrays.
Syntax: DataType ArrayName[size1][size2][size3];
For Example: int a[5][5][5];
Visit and explore, Difference Between One-Dimensional and Two-Dimensional
Array
Advantages of Array
• It is a better version of storing the data of the same size and same type.
• It enables us to collect the number of elements in it.
• Arrays have a safer cache positioning that improves performance.
• Arrays can represent multiple data items of the same type using a single
name.
Disadvantages Of Array:
• In an array, it is essential to identify the number of elements to be stored.
• It is a static structure. It means that in an array, the memory size is fixed.
• When it comes to insertion and deletion, it is a bit difficult because the
elements are stored sequentially and the shifting operation is expensive.

You might also like