KEMBAR78
Array | PDF | Matrix (Mathematics) | Integer (Computer Science)
0% found this document useful (0 votes)
11 views28 pages

Array

Uploaded by

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

Array

Uploaded by

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

Go, change the world

RV College of
Engineering

UNIT-II
ARRAYS
AGENDA Go, change the world

• Introduction
• Declaration of Arrays
• Accessing elements of
an array,
• Storing values in
arrays, Operations on
Arrays.
• Two dimensional arrays
• Operations on two
dimensional arrays.
Introducti Go, change the world

on
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).
Declaring an array means specifying three things:
1. The data type- what kind of values it can store ex, int, char, float
2.Name- to identify the array
3.The size- the maximum number of values that the array can hold
Arrays are declared using the following syntax.
 type name[size]; 1st element 2nd 3rd 4th 5th element 6th 7th 8th element 9th 10th
element element element element element element element

marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8]


marks[9]
Introducti Go, change the world

on
Arrays can be declared by specifying the size or the number of array elements. The size of the array
specifies the maximum number of elements that the array can hold.
Eg : // declare an array by specifying size in [].
 int my_array1[20];
 char my_array2[5];
 C does not allow declaring of the array whose number of elements is not know during compile time.
 Eg : int arr[];
 int n , arr[n]; // These both will results in the error
 C array indices starts from 0, so for an array with N elements the index of the last element will be N-
1.
 C never checks the validity of array index , neither at the compile time or run time, but running such
code, output will be unpredictable.
Accessing Array Go, change the world

Elements
Array element can be accessed using array name and subscript/index written inside pair of square
brackets [].
For Example:
Suppose we have an integer array of length 5 whose name is marks. = int marks[5] = {5,2,9,1,1};
Now we can access elements of array marks using subscript followed by array name.
marks[0] = First element of array marks = 5, marks[1] = Second element of array marks = 2
To access all the elements of the array, you must use a loop. That is, we can access all the elements
of the array by varying the value of the subscript into the array.
 Note that the subscript must be an integral value or an expression that evaluates to an integral
value. For Example : int i, marks[10];
 for(i=0;i<10;i++)
 marks[i] = -1;
CALCULATING THE ADDRESS OF ARRAY Go, change the world
ELEMENTS
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.
w is the word size of one element in memory, for example, size of int is 2.

Marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6]


marks[7] marks[7] 1000 1002 1004 1006 1008 1010 1012 1014

Marks[4] = 1000 + 2(4 – 0)


= 1000 + 2(4) = 1008
CALCULATING THE Length OF ARRAY Go, change the world

The general formula to calacualte the length pf the array is


Length = upper_bound – lower_bound+1
Upper_bound is the index of last element and lower bound is the index of first
element
Usually the lower bound value is zero but that is not compulsion as we can have
array whose index may start from any other non zero value
int marks[5] = {5,2,9,1,1};
Length = 4-0+1 = 5
STORING VALES IN ARRAYS Go, change the world

There are three ways to store the values in the array and they are, as shown in below figure
Initializing Array during DeclarationGo, change the world

Array are intitialized by writing


Type array_name[size] = {list of the values}
Eg : int marks[5] = {5,2,9,1,1};
Now we can access elements of array marks using subscript followed by array name.
marks[0] = First element of array marks = 5, marks[1] = Second element of array marks = 2
 While initializing the array at the time of deceleration the programmer may omit the size of the
array
int marks[] = {5,2,9,1,1};
If the number of elements provided is less than the number of elements in the array, the unsigned
elements are filled with zeroes.
If we have more elements provided then the number of elements declared it will generate the
compile error
RV College of
Engineering
Initializing Array – Input the Values from Key
Go, change the world
Board
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” ‘”while loop” or “do-while”to initialize the array elements.
This is the most common way to initialize an array in C.
Eg :
int my_array[5];
// initialize array using a "for" loop.
int i;
for(i = 0; i < 5; i++)

{
my_array[i] = 2 * i;

}
// my_array = {0, 2, 4, 6, 8}
In the above syntax, an array of size 5 is declared first. The array is then initialized using a for loop
that iterates over the array starting from index 0 to (size - 1).
Initializing Array – Assigning values to Induvial Go, change the world
Elements

The values can be assigned to the induvial elements of the array by using the
assignment operator.

Eg : marks[3] = 100;

Here the 100 is assigned to the fourth element of the array.

We can not assign the one array to the other array, though they may have same size and
type.

To copy the array, we must copy the every element of the first array to the element of
the second array
Operations on array in C Go, change the world

There are a number of operations that can be performed on an array which are:
Traversal
Insertion
Deletion
Searching
Merging
Sorting
Operations on array in C- Traversal Go, change the world

 Traversal means accessing each array element for a specific purpose, either to perform an operation
on them , counting the total number of elements or else using those values to calculate some other
result.
 Since array elements is a linear data structure meaning that all elements are placed in consecutive
blocks of memory it is easy to traverse them.
Algorithm: Consider A[]is the array:
Step 1: Initialise counter c = lower_bound_index

Step 2: Repeat steps 3 to 4 while c < upper_bound

Step 3: Apply the specified operation on A[c]

Step 4: Increment counter : C = C + 1

[Loop Ends]

Step 5: Exit
Operations on array in C-Inserting Go, change the world

 Inserting an element in array means adding a new data element to already existing array.

 If the array has to be added at the end of existing array - Just add 1 to the upper_bound and assign
the value

 Alogorithim

 Step 1: Set upper_bound = upper_bound+1


 Step2: Set A[upper_bound] =VAL
 Step 3 :Exit

 Assume :Memory Space is available


Operations on array in C-Inserting Go, change the world

 Inserting an element in array in the middle of the array


 The algorithm INSERT will be declared as INSERT( A, N, POS, VAL).

Step 1: [INITIALIZATION] SET I = N


Step 2: Repeat Steps 3 and 4 while I >= POS
Step 3: SET A[I + 1] = A[I]
Step 4: SET I = I – 1
[End of Loop]
Step 5: SET N = N + 1
Step 6: SET A[POS] = VAL
Step 7: EXIT

Calling INSERT (Data, 6, 3, 100) will lead to the following processing in the
array
Operations on array in C-Inserting Go, change the world

Data[0] Data[1] Data[2] Data[3] Data[4] Data[5]


Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6]
Data[6]

56
56

45 23 34 100 12 20
45 23 34 12 56 20

Data[0] Data[1] Data[2] Data[3] Data[4]


[0] Data[1]
Data Data[2] Data[3] Data[4] Data[5] Data[5] Data[6]
Data[6]
Operations on array in C-Deleting Go, change the world
Operations on array in C-Merging Go, change the world

 Merging two arrays means combining two separate arrays into one single array.
 For instance, if the first array consists of 3 elements and the second array consists of 5 elements then the resulting array
consists of 8 elements.
 This resulting array is known as a merged array.

 We are given two sorted arrays and our task is to merge these two sorted arrays.

 Input: First Array: 5 4 3 2 1

 Second Array: 9 8 7 6 5

 Output: Merged sorted Array: 9 8 7 6 5 5 4 3 2 1


Searching of Value in an Array Go, change the world

 LINEAR SEARCH

LINEAR_SEARCH(A, N, VAL, POS)

Step 1: [INITIALIZE] SET POS = -1


Step 2: [INITIALIZE] SET I = 0
Step 3:Repeat Step 4 while I<N
Step 4: IF A[I] = VAL, then
SET POS = I
PRINT POS
Go to Step 6
[END OF IF]
[END OF LOOP]
Step 5: PRINT “Value Not Present In The Array”
Step 6: EXIT
Searching of Value in an Array Go, change the world

 BINARY SEARCH
BEG = lower_bound and END = upper_bound
MID = (BEG + END) / 2
If VAL < A[MID], then VAL will be present in the left segment of the array. So,
the value of END will be changed as, END = MID – 1
If VAL > A[MID], then VAL will be present in the right segment of the array. So,
the value of BEG will be changed as, BEG = MID + 1

 BINARY_SEARCH(A, lower_bound, upper_bound, VAL, POS)

Step 1: [INITIALIZE] SET BEG = lower_bound, END = upper_bound, POS = -1


Step 2: Repeat Step 3 and Step 4 while BEG <= END
Step 3: SET MID = (BEG + END)/2
Step 4: IF A[MID] = VAL, then
POS = MID
PRINT POS
Go to Step 6
IF A[MID] > VAL then;
SET END = MID - 1
Searching of Value in an Array-Binary Go, change the world
Search

BINARY_SEARCH(A, lower_bound, upper_bound, VAL, POS)

Step 1: [INITIALIZE] SET BEG = lower_bound, END = upper_bound, POS = -1


Step 2: Repeat Step 3 and Step 4 while BEG <= END
Step 3: SET MID = (BEG + END)/2
Step 4: IF A[MID] = VAL, then
POS = MID
PRINT POS
Go to Step 6
IF A[MID] > VAL then;
SET END = MID - 1
ELSE
SET BEG = MID + 1
[END OF IF]
[END OF LOOP]
Step 5: IF POS = -1, then
PRINTF “VAL IS NOT PRESENT IN THE ARRAY”
[END OF IF]
Step 6: EXIT
Searching of Value in an Array-Binary Go, change the world
Search
int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
and VAL = 9, the algorithm will proceed in the following manner.
BEG = 0, END = 10, MID = (0 + 10)/2 = 5
Now, VAL = 9 and A[MID] = A[5] = 5
A[5] is less than VAL, therefore, we will now search for the value in the later half of the array.
So, we change the values of BEG and MID.
Now, BEG = MID + 1 = 6, END = 10, MID = (6 + 10)/2 =16/2 = 8
Now, VAL = 9 and A[MID] = A[8] = 8
A[8] is less than VAL, therefore, we will now search for the value in the later half of the array.
So, again we change the values of BEG and MID.
Now, BEG = MID + 1 = 9, END = 10, MID = (9 + 10)/2 = 9
Now VAL = 9 and A[MID] = 9.
Searching of Value in an Array-Binary Go, change the world
Search
int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
and VAL = 9, the algorithm will proceed in the following manner.
BEG = 0, END = 10, MID = (0 + 10)/2 = 5
Now, VAL = 9 and A[MID] = A[5] = 5
A[5] is less than VAL, therefore, we will now search for the value in the later half of the array.
So, we change the values of BEG and MID.
Now, BEG = MID + 1 = 6, END = 10, MID = (6 + 10)/2 =16/2 = 8
Now, VAL = 9 and A[MID] = A[8] = 8
A[8] is less than VAL, therefore, we will now search for the value in the later half of the array.
So, again we change the values of BEG and MID.
Now, BEG = MID + 1 = 9, END = 10, MID = (9 + 10)/2 = 9
Now VAL = 9 and A[MID] = 9.
Two Dimensional Arrays Go, change the world

Two Dimensional Arrays can be thought of as an array of arrays or as a matrix consisting of rows and
columns.
Declaration of two dimensional Array in
Go, change the world
C
Two Dimensional Arrays can be thought of as an array of arrays or as a matrix consisting of rows and
columns.
The syntax to declare the 2D array is given below.

data_type array_name[rows][columns];
Consider the following example.

int twodimen[4][3];
Here, 4 is the number of rows, and 3 is the number of columns
Initializing of two dimensional Array in CGo, change the world

There are two ways to initialize a two Dimensional arrays during declaration.

int disp[2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};
OR
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

To access the values we need to use the two branching loops


Operations of two dimensional Array in Go, change the world
C
Two dimensional arrays can be used to implement concept of matrices
1. Transpose : Transpose of m×n matrix A is given as n×m matrix B , where
Bi,j = Ai,j
2. Sum = Ci,j = Ai,j + Bi,j = Two matrix which are compatible can be added and the
result will be stored in other matrix
3. Difference = Ci,j = Ai,j - Bi,j =Two matrix which are compatible can be subtracted
and the result will be stored in other matrix
4. Product = Two matrix can be multiplied with each other if the number of columns
in first matrix is equal to number of rows in second matrix

You might also like