[go: up one dir, main page]

0% found this document useful (0 votes)
10 views6 pages

Dsa Unit2

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

Dsa Unit2

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

UNIT 2

What is Array?

Array in C can be defined as a collection of multiple entities of similar type into a larger group. These
entities or elements can be of int, float, char, or double data type or can be of user-defined data types
too like structures. However, in order to be stored together in a single array, all the elements should be
of the same data type. The elements are stored from left to right with the left-most index being the 0th
index and the rightmost index being the (n-1) index.

Classification of Arrays:

Array are of two types:


 Single dimensional arrays.
 Multidimensional arrays.

Single Dimensional Arrays:

Single dimensional array or 1-D array is the simplest form of arrays. This type of array consists of
elements of similar types and these elements can be accessed through their indices.

Multi-dimensional Arrays:

The most common type of multi-dimensional array that is used in the C language is a 2-D array.
However, the number of dimensions can be more than 2 depending upon the compiler of the user’s
system. These arrays consist of elements that are array themselves.
Why Do We Need Arrays?

If we have a small number of elements, let us say we want 3 variables, then we can declare them
separately like var1, var2, and var3. But if we have many variables then we can use arrays to store
them.
Suppose want to make a program that prints 1-100 digits. , it can be achieved this by 2 methods. The
first one is to make 100 variables and store the numbers from 1-100 in those variables separately and
then print each digit. The second method is to create an array of size 100 and store the numbers in that
array using a loop. These digits can be printed using a single loop in linear complexity. The second
method is more optimized and desirable than the first one as it is more convenient to store these
values in a single array rather than creating 100 variables.

Properties of Array in C

The properties of the arrays depend on the programming language. Different properties of Array in
the C programming language.
1. Fixed Size Collection
2. Homogeneous Elements
3. Finite Elements
4. Contiguous Memory Allocation
5. Random Access
6. Ordered Set

Fixed Size Collection:

The size of an array is fixed at the time of declaration and cannot be changed during the program's
execution. This means we need to know the maximum number of elements beforehand. In C, an
array cannot have a size defined in terms of a variable.

Homogeneous elements:

All elements of an array must be of the same data type. This ensures consistent access and
operations on the data.

If an array is declared as follows −


int arr[] = {50, 67.55, "hello", 21};
Compiler issues a warning −
initialization of 'int' from 'char *' makes integer from pointer without a cast
[-Wint-conversion]|

Contiguous Memory Allocation:

All elements of an array are stored in contiguous memory locations, meaning they occupy a block
of memory next to each other. This allows for efficient random access and memory management.
Contiguous memory allocation allocates consecutive blocks of memory to a file/process.

Ordered Set:

Each element in an array has a unique index, starting from 0. Individual elements can be accessed
using their index within square brackets. Usually, array is traversed with a for loop running over
its length and using the loop variable as the index.

Finite Elements:
Since an array can store all the elements of same type, the total memory occupied by it depends
on the data type.

Random access of an array:

It is one of the defining properties of an Array in C. It means that we can randomly access any
element in the array without touching any other element using its index. This property is the result
of Contiguous Storage as a compiler deduces the address of the element at the given index by
using the address of the first element and the index number.

Basic Operations on Array:

o Traversal - This operation is used to print the elements of the array.


o Insertion - It is used to add an element at a particular index.
o Deletion - It is used to delete an element from a particular index.
o Search - It is used to search an element using the given index or by the value.
o Sorting - It arrange element in ascending and descending order

Calculating the Address of Array Elements:

Address of data element, A[k] = BA(A) + w(k – lower_bound)


BA—Base Address,
w-----size of individual element
k------index of element location to be found
lowerbound---------lowebound of array

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.

Solution :
marks[4] = 1000 + 2(4 – 0) = 1000 + 2(4) = 1008

Declaration of Array in C

1 D array declaration
data_type array_name [array_size]
2 D array declaration
data_type array_name[row_size][col_size];
3 D array declaration
data_type array_name[block][row_size][col_size];

Giving input/insertion to an array:


1) Initializing Arrays during Declaration
type array_name[size]={list of values};
ex= int array[5]={12,56,78,45,32};

2) Inputting Values from the Keyboard(run time):


An array can also be initialized using a loop. The loop iterates from 0 to (size - 1) for
accessing all indices of the array starting from 0. The following syntax uses a “for loop” to
initialize the array elements. This is the most common way to initialize an array in C.

int count, array[5];


for(int count=0;count<5;count++)
{
scanf(“%d”array[count]);
}
3) Assigning Values to individual Element:
int i, array[5];
int array[0]=12;
int array[1]=56;
int array[2]=78;
int array[3]=45;
int array[4]=32;

2D Array:

2D array can be defined as an array of arrays. The 2D array is organized as matrices which can be
represented as the collection of rows and columns.
2D arrays are created to implement a relational database look alike data structure. It provides ease of
holding bulk of data at once which can be passed to any number of functions wherever required.
The syntax of declaring two dimensional array is very much similar to that of a one dimensional
array, given as follows:

Syntax
datatype arrayname[rows_size][col-size]
Three-Dimensional Array in C:

A three Dimensional Array or 3D array in C is a collection of two-dimensional arrays. It can be


visualized as multiple 2D arrays stacked on top of each other.

Syntax
datatype arrayname[block_size][rows_size][col-size]
Advantages of Arrays in Data Structures:

1. Efficient access: Arrays offer fast and efficient access to elements because each element can
be accessed directly through its index. This makes array traversal quick and straightforward.
2. Versatility: Arrays can be used to store any type of data like integers, characters, and strings.
They can also be used to store user-defined data types, such as structures and classes.
3. Flexibility: Arrays are used to implement other data structures like stacks, queues, trees,
graphs, etc.
4. Easy to remember: Arrays represent multiple data items of the same type using a single name.
5. Optimized code.
6. Any array element can be accessed in any order either from the front or rear

Disadvantages of Array:

1. Array is homogenous. It means that the elements with similar data type can be stored in it.
2. In array, there is static memory allocation that is size of an array cannot be altered.
3.There will be wastage of memory if we store less number of elements than the declared
size.

You might also like