© 2015UPESJuly 2015 Department. Of Civil Engineering
ARRAYS
© 2015UPESJuly 2015 Department. Of Civil Engineering
INTRODUCTION
 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:
The data type- what kind of values it can store as int, char, float.
Name- to identify the array
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
element
3rd
element
4th
element
5th
element
6th
element
7th
element
8th
element
9th
element
10th
element
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
© 2015UPESJuly 2015 Department. Of Civil Engineering
ACCESSING ELEMENTS OF THE ARRAY
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. But note that the
subscript must be an integral value or an expression that evaluates to an integral value.
int i, marks[10];
for(i=0;i<10;i++)
marks[i] = -1;
CALCULATING THE ADDRESS OF ARRAY
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.
99 67 78 56 88 90 34 85
Marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7]
1000 1002 1004 1006 1008 1010 1012 1014
Marks[4] = 1000 + 2(4 – 0)
= 1000 + 2(4) = 1008
© 2015UPESJuly 2015 Department. Of Civil Engineering
STORING VALES IN ARRAYS
Store values in the array
Initialize the elements
Input values for the elements
Assign values to the elements
Initialization of Arrays
Arrays are initialized by writing,
type array_name[size]={list of values};
int marks[5]={90, 82, 78, 95, 88};
int i, marks[10];
for(i=0;i<10;i++)
scanf(“%d”, &marks[i]);
Assigning Values
int i, arr1[10], arr2[10];
for(i=0;i<10;i++)
arr2[i] = arr1[i];
Inputting Values
CALCULATING THE LENGTH OF THE ARRAY
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
99 67 78 56 88 90 34 85
Marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6 marks[7]]
Here, lower_bound = 0, upper_bound = 7
Therefore, length = 7 – 0 + 1 = 8
© 2015UPESJuly 2015 Department. Of Civil Engineering
WRITE A PROGRAM TO READ AND DISPLAY N
NUMBERS USING AN ARRAY
#include<stdio.h>
int main()
{ int i=0, n, arr[20];
printf(“n Enter the number of elements : “);
scanf(“%d”, &n);
for(i=0;i<n;i++)
{ printf(“n Arr[%d] = “, i);
scanf(“%d”,&arr[i]);
}
printf(“n The array elements are “);
for(i=0;i<n;i++)
printf(“Arr[%d] = %dt”, i, arr[i]);
return 0;
}
© 2015UPESJuly 2015 Department. Of Civil Engineering
INSERTING AN ELEMENT IN THE ARRAY
Algorithm to insert a new element to the end of the array.
Step 1: Set upper_bound = upper_bound + 1
Step 2: Set A[upper_bound] = VAL
Step 3; EXIT
The algorithm INSERT will be declared as INSERT( A, N, POS, VAL).
Calling INSERT (Data, 6, 3, 100) will lead to the following processing in the array
45 23 34 12 56 20 20
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6]
45 23 34 12 56 56 20
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6]
45 23 34 12 12 56 20
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6]
45 23 34 100 12 56 20
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6]
© 2015UPESJuly 2015 Department. Of Civil Engineering
 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
© 2015UPESJuly 2015 Department. Of Civil Engineering
#include <stdio.h>
int main()
{
int array[100], position, c, n, value;
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d elementsn", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to insert an elementn");
scanf("%d", &position);
printf("Enter the value to insertn");
scanf("%d", &value);
Inserting element in an array
© 2015UPESJuly 2015 Department. Of Civil Engineering
Inserting element in an array (Contd.)
for (c = n - 1; c >= position - 1; c--)
{ array[c+1] = array[c];
}
array[position-1] = value;
printf("Resultant array isn");
for (c = 0; c <= n; c++)
{
printf("%dn", array[c]);}
return 0;
}
© 2015UPESJuly 2015 Department. Of Civil Engineering
OUTPUT
© 2015UPESJuly 2015 Department. Of Civil Engineering
DELETING AN ELEMENT FROM THE ARRAY
Algorithm to delete an element from the end of the array
45 23 34 12 56 20
Step 1: Set upper_bound = upper_bound - 1
Step 2: EXIT
Step 1: [INITIALIZATION] SET I = POS
Step 2: Repeat Steps 3 and 4 while I <= N - 1
Step 3: SET A[I] = A[I + 1]
Step 4: SET I = I + 1
[End of Loop]
Step 5: SET N = N - 1
Step 6: EXIT
The algorithm DELETE will be declared as DELETE( A, N, POS).
Calling DELETE (Data, 6, 2) will lead to the following processing in the array
45
23 12 12 56
20
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5]
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5]
45
23 12 56 56
20
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5]
45
23 12 56 20
20
Data[0] Data[1] Data[2] Data[3] Data[4] Data[5]
45
23 12 56 20
Data[0] Data[1] Data[2] Data[3] Data[4]
© 2015UPESJuly 2015 Department. Of Civil Engineering
Deleting an Element from an array
#include <stdio.h>
int main()
{
int array[100], position, c, n;
printf("Enter number of elements in arrayn");
scanf("%d", &n);
printf("Enter %d elementsn", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
© 2015UPESJuly 2015 Department. Of Civil Engineering
printf("Enter the location where you wish to delete elementn");
scanf("%d", &position);
if ( position >= n+1 )
printf("Deletion not possible.n");
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Resultant array isn");
for( c = 0 ; c < n - 1 ; c++ )
printf("%dn", array[c]);
} return 0;
}
Deleting an Element from an array
© 2015UPESJuly 2015 Department. Of Civil Engineering
OUTPUT
© 2015UPESJuly 2015 Department. Of Civil Engineering
Searching and Sorting
 Sorting is the process of arranging elements in the list according to their
values, in ascending or descending order. A sorted list is called as ordered
list. Sorted lists are especially important in list searching because they
facilitate the rapid search operations. Many sorting techniques are available.
The three simple and most important among them are:
Bubble Sort
Selection Sort and
Insertion Sort
 Searching is the process of finding the location of specified element in a
list. The specified element is called the search key. If the process of
searching finds a match of search key with the list element value, the search
is said to be successful; otherwise, it is unsuccessful. The two most
commonly used search techniques are:
Sequential Search
Binary Search
© 2015UPESJuly 2015 Department. Of Civil Engineering
Bubble Sort
© 2015UPESJuly 2015 Department. Of Civil Engineering
Program for Bubble Sort
#include <stdio.h>
int main()
{
int myarray[50];
int i, n, temp, swapflag;
/* Get the number of items in array */
printf("How many items are in the array? n");
scanf("%d", &n);
/* Read array items*/
printf("Enter the array items n");
for (i = 0; i < n; i++)
scanf("%d", &myarray[i]);
/* Sort array using bubble sort method */
do {
swapflag = 0;
© 2015UPESJuly 2015 Department. Of Civil Engineering
for (i = 0; i < n-1; i++) {
if (myarray[i] > myarray[i+1])
{
temp = myarray[i+1]
myarray[i+1] = myarray[i];
myarray[i] = temp;
swapflag = 1;
}
}
} while (swapflag == 1);
/* Print the sorted array */
printf("Array items sorted in ascending order n");
for (i = 0; i < n; i++){
printf("%d n", myarray[i]);}
return 0; }
© 2015UPESJuly 2015 Department. Of Civil Engineering
Selection Sort
 The selection Sort starts from first element and
searches the entire list until it finds the minimum
value. The sort places the minimum value in first
place, selects the second element and searches for
the second smallest element. The process continues
until the complete list is sorted.
© 2015UPESJuly 2015 Department. Of Civil Engineering
Selection Sort
© 2015UPESJuly 2015 Department. Of Civil Engineering
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for ( c = 0 ; c < n ; c++ )
{
scanf("%d", &array[c]);
}
© 2015UPESJuly 2015 Department. Of Civil Engineering
for ( c = 0 ; c < ( n - 1 ) ; c++ )
{ position = c;
for ( d = c + 1 ; d < n ; d++ )
{ if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{ swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
© 2015UPESJuly 2015 Department. Of Civil Engineering
 printf("Sorted list in ascending order:n");
for ( c = 0 ; c < n ; c++ )
{
printf("%dn", array[c]);
}
return 0;
}
© 2015UPESJuly 2015 Department. Of Civil Engineering
Insertion Sort
© 2015UPESJuly 2015 Department. Of Civil Engineering
/* insertion sort ascending order */
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
© 2015UPESJuly 2015 Department. Of Civil Engineering
for (c = 1 ; c <= n - 1; c++)
{
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("Sorted list in ascending order:n");
for (c = 0; c <= n - 1; c++) {
printf("%dn", array[c]); }
© 2015UPESJuly 2015 Department. Of Civil Engineering
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
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
© 2015UPESJuly 2015 Department. Of Civil Engineering
Program (Linear Search)
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in arrayn");
scanf("%d",&n);
printf("Enter %d integer(s)n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to searchn");
scanf("%d", &search);
© 2015UPESJuly 2015 Department. Of Civil Engineering
for (c = 0; c < n; c++)
{ if (array[c] == search) /* if required element found */
{ printf("%d is present at location %d.n", search, c+1);
break;
}
} if (c == n)
printf("%d is not present in array.n", search);
return 0; }
© 2015UPESJuly 2015 Department. Of Civil Engineering
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to findn");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
Program (Binary Search)
© 2015UPESJuly 2015 Department. Of Civil Engineering
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d is not present in the list.n", search);
return 0;
}
Program (Binary Search)
© 2015UPESJuly 2015 Department. Of Civil Engineering
TWO DIMENSIONAL ARRAYS
 A two dimensional array is specified using two subscripts where one subscript denotes row
and the other denotes column.
 C looks a two dimensional array as an array of a one dimensional array.
Second Dimension
A two dimensional array is declared as:
data_type array_name[row_size][column_size];
Therefore, a two dimensional mXn array is an
array that contains m*n data elements and each
element is accessed using two subscripts, i and j
where i<=m and j<=n
int marks[3][5]
Rows/Columns
Col 0 Col 1 Col2 Col 3 Col 4
Row 0 Marks[0][0] Marks[0][1] Marks[0][2] Marks[0][3] Marks[0][4]
Row 1 Marks[1][0] Marks[1][1] Marks[1][2] Marks[1][3] Marks[1][4]
Row 2 Marks[2][0] Marks[2][1] Marks[2][2] Marks[2][3] Marks[2][4]
Two Dimensional Array
© 2015UPESJuly 2015 Department. Of Civil Engineering
MEMORY REPRESENTATION OF A TWO
DIMENSIONAL ARRAY
There are two ways of storing a 2-D array can be stored in memory. The first way is row major
order and the second is column major order.
In the row major order the elements of the first row are stored before the elements of the second
and third row. That is, the elements of the array are stored row by row where n elements of the
first row will occupy the first nth locations.
(0,0) (0, 1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3)
However, when we store the elements in a column major order, the elements of the first column
are stored before the elements of the second and third column. That is, the elements of the
array are stored column by column where n elements of the first column will occupy the first nth
locations.
(0,0) (1,0) (2,0) (3,0) (0,1) (1,1) (2,1 (3,1) (0,2) (1,2) (2,2) (3,2)
Address(A[I][J] = Base_Address + w{M ( J - 1) + (I - 1)}, if the array elements are stored in column
major order.
And, Address(A[I][J] = Base_Address + w{N ( I - 1) + (J - 1)}, if the array elements are stored in row
major order.
Where, w is the number of words stored per memory location
m, is the number of columns
n, is the number of rows
I and J are the subscripts of the array element
© 2015UPESJuly 2015 Department. Of Civil Engineering
TWO DIMENSIONAL ARRAYS CONTD..
 A two dimensional array is initialized in the same was as a single dimensional array is initialized. For example,
int marks[2][3]={90, 87, 78, 68, 62, 71};
int marks[2][3]={{90,87,78},{68, 62, 71}};
Write a program to print the elements of a 2D array
#include<stdio.h>
#include<conio.h>
int main()
{ int arr[2][2] = {12, 34, 56,32};
int i, j;
for(i=0;i<2;i++)
{ printf("n");
for(j=0;j<2;j++)
printf("%dt", arr[i][j]);
} return 0;
}
© 2015UPESJuly 2015 Department. Of Civil Engineering
MULTI DIMENSIONAL ARRAYS
 A multi dimensional array is an array of arrays.
 Like we have one index in a single dimensional array, two indices in a two dimensional array, in
the same way we have n indices in a n-dimensional array or multi dimensional array.
 Conversely, an n dimensional array is specified using n indices.
 An n dimensional m1 x m2 x m3 x ….. mn array is a collection m1*m2*m3* ….. *mn elements.
 In a multi dimensional array, a particular element is specified by using n subscripts as
A[I1][I2][I3]…[In], where,
I1<=M1 I2<=M2 I3 <= M3 ……… In <= Mn
© 2015UPESJuly 2015 Department. Of Civil Engineering
PROGRAM TO READ AND DISPLAY A 2X2X2
ARRAY
#include<stdio.h>
int main()
{ int array1[3][3][3], i, j, k;
printf(“n Enter the elements of the matrix”);
printf(“n ******************************”);
for(i=0;i<2;i++)
{ for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
printf(“n array[%d][ %d][ %d] = ”, i, j, k);
scanf(“%d”, &array1[i][j][k]);
}
}
}
printf(“n The matrix is : “);
printf(“n *********************************”)l
for(i=0;i<2;i++)
{ printf(“nn”);
for(j=0;j<2;j++)
{
printf(“n”);
for(k=0;k<2;k++)
printf(“t array[%d][ %d][ %d] = %d”, i, j, k, array1[i][j][k]); } }
© 2015UPESJuly 2015 Department. Of Civil Engineering
How 2D array represented in memory?
Explain with example.
 There are two way of representing 2D array in memory:
 Row Major: elements of array store in contiguous memory locations
row by row. As shown in figure.
 Column Major: elements of array store in contiguous memory
locations column by column. As shown in figure.
 Numerical Example:
© 2015UPESJuly 2015 Department. Of Civil Engineering
 In figure there is 3x3 array and memory location start from 200 and each
element take 2 address. Calculate element at Array [3][1] for both row
and column major?
 Ans. here m=n=3
I =3, j=1, w=2, base address=200
 Formula for row major
Array[i][j] = Base+w( n(i-1)+(j-1) )
= 200+2(3(3-1)+(1-1))
= 212
 Formula for column major
Array[i][j] = Base+w( m(j-1)+(i-1) )
= 200+2(3(1-1)+(3-1))
= 204

Arrays

  • 1.
    © 2015UPESJuly 2015Department. Of Civil Engineering ARRAYS
  • 2.
    © 2015UPESJuly 2015Department. Of Civil Engineering INTRODUCTION  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: The data type- what kind of values it can store as int, char, float. Name- to identify the array 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 element 3rd element 4th element 5th element 6th element 7th element 8th element 9th element 10th element marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] marks[8] marks[9]
  • 3.
    © 2015UPESJuly 2015Department. Of Civil Engineering ACCESSING ELEMENTS OF THE ARRAY 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. But note that the subscript must be an integral value or an expression that evaluates to an integral value. int i, marks[10]; for(i=0;i<10;i++) marks[i] = -1; CALCULATING THE ADDRESS OF ARRAY 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. 99 67 78 56 88 90 34 85 Marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7] 1000 1002 1004 1006 1008 1010 1012 1014 Marks[4] = 1000 + 2(4 – 0) = 1000 + 2(4) = 1008
  • 4.
    © 2015UPESJuly 2015Department. Of Civil Engineering STORING VALES IN ARRAYS Store values in the array Initialize the elements Input values for the elements Assign values to the elements Initialization of Arrays Arrays are initialized by writing, type array_name[size]={list of values}; int marks[5]={90, 82, 78, 95, 88}; int i, marks[10]; for(i=0;i<10;i++) scanf(“%d”, &marks[i]); Assigning Values int i, arr1[10], arr2[10]; for(i=0;i<10;i++) arr2[i] = arr1[i]; Inputting Values CALCULATING THE LENGTH OF THE ARRAY 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 99 67 78 56 88 90 34 85 Marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6 marks[7]] Here, lower_bound = 0, upper_bound = 7 Therefore, length = 7 – 0 + 1 = 8
  • 5.
    © 2015UPESJuly 2015Department. Of Civil Engineering WRITE A PROGRAM TO READ AND DISPLAY N NUMBERS USING AN ARRAY #include<stdio.h> int main() { int i=0, n, arr[20]; printf(“n Enter the number of elements : “); scanf(“%d”, &n); for(i=0;i<n;i++) { printf(“n Arr[%d] = “, i); scanf(“%d”,&arr[i]); } printf(“n The array elements are “); for(i=0;i<n;i++) printf(“Arr[%d] = %dt”, i, arr[i]); return 0; }
  • 6.
    © 2015UPESJuly 2015Department. Of Civil Engineering INSERTING AN ELEMENT IN THE ARRAY Algorithm to insert a new element to the end of the array. Step 1: Set upper_bound = upper_bound + 1 Step 2: Set A[upper_bound] = VAL Step 3; EXIT The algorithm INSERT will be declared as INSERT( A, N, POS, VAL). Calling INSERT (Data, 6, 3, 100) will lead to the following processing in the array 45 23 34 12 56 20 20 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6] 45 23 34 12 56 56 20 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6] 45 23 34 12 12 56 20 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6] 45 23 34 100 12 56 20 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[6]
  • 7.
    © 2015UPESJuly 2015Department. Of Civil Engineering  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
  • 8.
    © 2015UPESJuly 2015Department. Of Civil Engineering #include <stdio.h> int main() { int array[100], position, c, n, value; printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d elementsn", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the location where you wish to insert an elementn"); scanf("%d", &position); printf("Enter the value to insertn"); scanf("%d", &value); Inserting element in an array
  • 9.
    © 2015UPESJuly 2015Department. Of Civil Engineering Inserting element in an array (Contd.) for (c = n - 1; c >= position - 1; c--) { array[c+1] = array[c]; } array[position-1] = value; printf("Resultant array isn"); for (c = 0; c <= n; c++) { printf("%dn", array[c]);} return 0; }
  • 10.
    © 2015UPESJuly 2015Department. Of Civil Engineering OUTPUT
  • 11.
    © 2015UPESJuly 2015Department. Of Civil Engineering DELETING AN ELEMENT FROM THE ARRAY Algorithm to delete an element from the end of the array 45 23 34 12 56 20 Step 1: Set upper_bound = upper_bound - 1 Step 2: EXIT Step 1: [INITIALIZATION] SET I = POS Step 2: Repeat Steps 3 and 4 while I <= N - 1 Step 3: SET A[I] = A[I + 1] Step 4: SET I = I + 1 [End of Loop] Step 5: SET N = N - 1 Step 6: EXIT The algorithm DELETE will be declared as DELETE( A, N, POS). Calling DELETE (Data, 6, 2) will lead to the following processing in the array 45 23 12 12 56 20 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] 45 23 12 56 56 20 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] 45 23 12 56 20 20 Data[0] Data[1] Data[2] Data[3] Data[4] Data[5] 45 23 12 56 20 Data[0] Data[1] Data[2] Data[3] Data[4]
  • 12.
    © 2015UPESJuly 2015Department. Of Civil Engineering Deleting an Element from an array #include <stdio.h> int main() { int array[100], position, c, n; printf("Enter number of elements in arrayn"); scanf("%d", &n); printf("Enter %d elementsn", n); for ( c = 0 ; c < n ; c++ ) scanf("%d", &array[c]);
  • 13.
    © 2015UPESJuly 2015Department. Of Civil Engineering printf("Enter the location where you wish to delete elementn"); scanf("%d", &position); if ( position >= n+1 ) printf("Deletion not possible.n"); else { for ( c = position - 1 ; c < n - 1 ; c++ ) array[c] = array[c+1]; printf("Resultant array isn"); for( c = 0 ; c < n - 1 ; c++ ) printf("%dn", array[c]); } return 0; } Deleting an Element from an array
  • 14.
    © 2015UPESJuly 2015Department. Of Civil Engineering OUTPUT
  • 15.
    © 2015UPESJuly 2015Department. Of Civil Engineering Searching and Sorting  Sorting is the process of arranging elements in the list according to their values, in ascending or descending order. A sorted list is called as ordered list. Sorted lists are especially important in list searching because they facilitate the rapid search operations. Many sorting techniques are available. The three simple and most important among them are: Bubble Sort Selection Sort and Insertion Sort  Searching is the process of finding the location of specified element in a list. The specified element is called the search key. If the process of searching finds a match of search key with the list element value, the search is said to be successful; otherwise, it is unsuccessful. The two most commonly used search techniques are: Sequential Search Binary Search
  • 16.
    © 2015UPESJuly 2015Department. Of Civil Engineering Bubble Sort
  • 17.
    © 2015UPESJuly 2015Department. Of Civil Engineering Program for Bubble Sort #include <stdio.h> int main() { int myarray[50]; int i, n, temp, swapflag; /* Get the number of items in array */ printf("How many items are in the array? n"); scanf("%d", &n); /* Read array items*/ printf("Enter the array items n"); for (i = 0; i < n; i++) scanf("%d", &myarray[i]); /* Sort array using bubble sort method */ do { swapflag = 0;
  • 18.
    © 2015UPESJuly 2015Department. Of Civil Engineering for (i = 0; i < n-1; i++) { if (myarray[i] > myarray[i+1]) { temp = myarray[i+1] myarray[i+1] = myarray[i]; myarray[i] = temp; swapflag = 1; } } } while (swapflag == 1); /* Print the sorted array */ printf("Array items sorted in ascending order n"); for (i = 0; i < n; i++){ printf("%d n", myarray[i]);} return 0; }
  • 19.
    © 2015UPESJuly 2015Department. Of Civil Engineering Selection Sort  The selection Sort starts from first element and searches the entire list until it finds the minimum value. The sort places the minimum value in first place, selects the second element and searches for the second smallest element. The process continues until the complete list is sorted.
  • 20.
    © 2015UPESJuly 2015Department. Of Civil Engineering Selection Sort
  • 21.
    © 2015UPESJuly 2015Department. Of Civil Engineering #include <stdio.h> int main() { int array[100], n, c, d, position, swap; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for ( c = 0 ; c < n ; c++ ) { scanf("%d", &array[c]); }
  • 22.
    © 2015UPESJuly 2015Department. Of Civil Engineering for ( c = 0 ; c < ( n - 1 ) ; c++ ) { position = c; for ( d = c + 1 ; d < n ; d++ ) { if ( array[position] > array[d] ) position = d; } if ( position != c ) { swap = array[c]; array[c] = array[position]; array[position] = swap; } }
  • 23.
    © 2015UPESJuly 2015Department. Of Civil Engineering  printf("Sorted list in ascending order:n"); for ( c = 0 ; c < n ; c++ ) { printf("%dn", array[c]); } return 0; }
  • 24.
    © 2015UPESJuly 2015Department. Of Civil Engineering Insertion Sort
  • 25.
    © 2015UPESJuly 2015Department. Of Civil Engineering /* insertion sort ascending order */ #include <stdio.h> int main() { int n, array[1000], c, d, t; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for (c = 0; c < n; c++) { scanf("%d", &array[c]); }
  • 26.
    © 2015UPESJuly 2015Department. Of Civil Engineering for (c = 1 ; c <= n - 1; c++) { d = c; while ( d > 0 && array[d] < array[d-1]) { t = array[d]; array[d] = array[d-1]; array[d-1] = t; d--; } } printf("Sorted list in ascending order:n"); for (c = 0; c <= n - 1; c++) { printf("%dn", array[c]); }
  • 27.
    © 2015UPESJuly 2015Department. Of Civil Engineering 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 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
  • 28.
    © 2015UPESJuly 2015Department. Of Civil Engineering Program (Linear Search) #include <stdio.h> int main() { int array[100], search, c, n; printf("Enter the number of elements in arrayn"); scanf("%d",&n); printf("Enter %d integer(s)n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the number to searchn"); scanf("%d", &search);
  • 29.
    © 2015UPESJuly 2015Department. Of Civil Engineering for (c = 0; c < n; c++) { if (array[c] == search) /* if required element found */ { printf("%d is present at location %d.n", search, c+1); break; } } if (c == n) printf("%d is not present in array.n", search); return 0; }
  • 30.
    © 2015UPESJuly 2015Department. Of Civil Engineering #include <stdio.h> int main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elementsn"); scanf("%d",&n); printf("Enter %d integersn", n); for (c = 0; c < n; c++) scanf("%d",&array[c]); printf("Enter value to findn"); scanf("%d", &search); first = 0; last = n - 1; middle = (first+last)/2; Program (Binary Search)
  • 31.
    © 2015UPESJuly 2015Department. Of Civil Engineering while (first <= last) { if (array[middle] < search) first = middle + 1; else if (array[middle] == search) { printf("%d found at location %d.n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if (first > last) printf("Not found! %d is not present in the list.n", search); return 0; } Program (Binary Search)
  • 32.
    © 2015UPESJuly 2015Department. Of Civil Engineering TWO DIMENSIONAL ARRAYS  A two dimensional array is specified using two subscripts where one subscript denotes row and the other denotes column.  C looks a two dimensional array as an array of a one dimensional array. Second Dimension A two dimensional array is declared as: data_type array_name[row_size][column_size]; Therefore, a two dimensional mXn array is an array that contains m*n data elements and each element is accessed using two subscripts, i and j where i<=m and j<=n int marks[3][5] Rows/Columns Col 0 Col 1 Col2 Col 3 Col 4 Row 0 Marks[0][0] Marks[0][1] Marks[0][2] Marks[0][3] Marks[0][4] Row 1 Marks[1][0] Marks[1][1] Marks[1][2] Marks[1][3] Marks[1][4] Row 2 Marks[2][0] Marks[2][1] Marks[2][2] Marks[2][3] Marks[2][4] Two Dimensional Array
  • 33.
    © 2015UPESJuly 2015Department. Of Civil Engineering MEMORY REPRESENTATION OF A TWO DIMENSIONAL ARRAY There are two ways of storing a 2-D array can be stored in memory. The first way is row major order and the second is column major order. In the row major order the elements of the first row are stored before the elements of the second and third row. That is, the elements of the array are stored row by row where n elements of the first row will occupy the first nth locations. (0,0) (0, 1) (0,2) (0,3) (1,0) (1,1) (1,2) (1,3) (2,0) (2,1) (2,2) (2,3) However, when we store the elements in a column major order, the elements of the first column are stored before the elements of the second and third column. That is, the elements of the array are stored column by column where n elements of the first column will occupy the first nth locations. (0,0) (1,0) (2,0) (3,0) (0,1) (1,1) (2,1 (3,1) (0,2) (1,2) (2,2) (3,2) Address(A[I][J] = Base_Address + w{M ( J - 1) + (I - 1)}, if the array elements are stored in column major order. And, Address(A[I][J] = Base_Address + w{N ( I - 1) + (J - 1)}, if the array elements are stored in row major order. Where, w is the number of words stored per memory location m, is the number of columns n, is the number of rows I and J are the subscripts of the array element
  • 34.
    © 2015UPESJuly 2015Department. Of Civil Engineering TWO DIMENSIONAL ARRAYS CONTD..  A two dimensional array is initialized in the same was as a single dimensional array is initialized. For example, int marks[2][3]={90, 87, 78, 68, 62, 71}; int marks[2][3]={{90,87,78},{68, 62, 71}}; Write a program to print the elements of a 2D array #include<stdio.h> #include<conio.h> int main() { int arr[2][2] = {12, 34, 56,32}; int i, j; for(i=0;i<2;i++) { printf("n"); for(j=0;j<2;j++) printf("%dt", arr[i][j]); } return 0; }
  • 35.
    © 2015UPESJuly 2015Department. Of Civil Engineering MULTI DIMENSIONAL ARRAYS  A multi dimensional array is an array of arrays.  Like we have one index in a single dimensional array, two indices in a two dimensional array, in the same way we have n indices in a n-dimensional array or multi dimensional array.  Conversely, an n dimensional array is specified using n indices.  An n dimensional m1 x m2 x m3 x ….. mn array is a collection m1*m2*m3* ….. *mn elements.  In a multi dimensional array, a particular element is specified by using n subscripts as A[I1][I2][I3]…[In], where, I1<=M1 I2<=M2 I3 <= M3 ……… In <= Mn
  • 36.
    © 2015UPESJuly 2015Department. Of Civil Engineering PROGRAM TO READ AND DISPLAY A 2X2X2 ARRAY #include<stdio.h> int main() { int array1[3][3][3], i, j, k; printf(“n Enter the elements of the matrix”); printf(“n ******************************”); for(i=0;i<2;i++) { for(j=0;j<2;j++) { for(k=0;k<2;k++) { printf(“n array[%d][ %d][ %d] = ”, i, j, k); scanf(“%d”, &array1[i][j][k]); } } } printf(“n The matrix is : “); printf(“n *********************************”)l for(i=0;i<2;i++) { printf(“nn”); for(j=0;j<2;j++) { printf(“n”); for(k=0;k<2;k++) printf(“t array[%d][ %d][ %d] = %d”, i, j, k, array1[i][j][k]); } }
  • 37.
    © 2015UPESJuly 2015Department. Of Civil Engineering How 2D array represented in memory? Explain with example.  There are two way of representing 2D array in memory:  Row Major: elements of array store in contiguous memory locations row by row. As shown in figure.  Column Major: elements of array store in contiguous memory locations column by column. As shown in figure.  Numerical Example:
  • 38.
    © 2015UPESJuly 2015Department. Of Civil Engineering  In figure there is 3x3 array and memory location start from 200 and each element take 2 address. Calculate element at Array [3][1] for both row and column major?  Ans. here m=n=3 I =3, j=1, w=2, base address=200  Formula for row major Array[i][j] = Base+w( n(i-1)+(j-1) ) = 200+2(3(3-1)+(1-1)) = 212  Formula for column major Array[i][j] = Base+w( m(j-1)+(i-1) ) = 200+2(3(1-1)+(3-1)) = 204