Unit2 DSC DivA L0B
Unit2 DSC DivA L0B
Sequential Organization
1
2
Basic Array Operations
Following are the basic Array operations.
• Traverse − Print each element in the array one by one.
• Insertion − At the specified index, adds an element.
• Deletion − The element at the specified index is deleted.
• Search − Uses the provided index or the value to search for an element.
• Update − The element at the specified index is updated.
When an array in C is started with size, the elements are subsequently given
default values in the manner described below.
bool false
char 0
int 0
float 0.0
double 0.0f
void
wchar_t 0
4
• C
#include <stdio.h>
main() {
int Arr[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are:\n");
for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}
n = n + 1;
while( j >= k) {
Arr[j+1] = Arr[j];
j = j - 1;
}
Arr[k] = item;
printf("The array elements after insertion:\n");
for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}
}
The program above generates the following output when it is compiled and run:
Output
The original array elements are:
Arr[0] = 1
Arr[1] = 3
Arr[2] = 5
5
Arr[3] = 7
Arr[4] = 8
The array elements after insertion:
Arr[0] = 1
Arr[1] = 3
Arr[2] = 5
Arr[3] = 10
Arr[4] = 7
Arr[5] = 8
6
int Arr[] = {1,3,5,7,8};
int m = 3, n = 5;
int i, j;
printf("The original array elements are:\n");
for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}
j = m;
while( j < n) {
Arr[j-1] = Arr[j];
j = j + 1;
}
n = n -1;
printf("The array elements after deletion:\n");
for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}
}
The program above generates the following output when it is compiled and run:
Output
The original array elements are :
Arr[0] = 1
Arr[1] = 3
Arr[2] = 5
Arr[3] = 7
Arr[4] = 8
The array elements after deletion:
7
Arr[0] = 1
Arr[1] = 3
Arr[2] = 7
Arr[3] = 8
Search Array Operations
An array element can be found using either its value or its index.
Algorithm
Take into consideration that K is a positive integer such that K=N and Arr is a
linear array with N items. The sequential search technique to locate an element
with the value of ITEM is shown below.
• Start
• Set J = 0
• Repeat steps 4 and 5 while J < N
• IF Arr[J] is equal ITEM THEN GOTO STEP 6
• Set J = J +1
• PRINT J, ITEM
• Stop
Example
The above algorithm is implemented as follows:
• C
#include <stdio.h>
void main() {
int Arr[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
printf("The original array elements are:\n");
for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}
8
while( j < n){
if( Arr[j] == item ) {
break;
}
j = j + 1;
}
printf("Found element %d at position %d\n", item, j+1);
}
The program above generates the following output when it is compiled and run:
Output
The original array elements are:
Arr[0] = 1
Arr[1] = 3
Arr[2] = 5
Arr[3] = 7
Arr[4] = 8
Found element 5 at position 3
9
Example
The implementation of the above algorithm is shown below.
• C
#include <stdio.h>
void main() {
int Arr[] = {1,3,5,7,8};
int m = 3, n = 5, item = 10;
int i, j;
printf("The original array elements are:\n");
for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}
Arr[m-1] = item;
printf("The array elements after updation:\n");
for(i = 0; i<n; i++) {
printf("Arr[%d] = %d \n", i, Arr[i]);
}
}
The above program generates the following output when it is compiled and run:
Output
The original array elements are:
Arr[0] = 1
Arr[1] = 3
Arr[2] = 5
Arr[3] = 7
Arr[4] = 8
The array elements after updation:
10
Arr[0] = 1
Arr[1] = 3
Arr[2] = 10
Arr[3] = 7
Arr[4] = 8
11
What is the implementation of array?
Implementation of arrays performs various operations like push (adding
element), pop (deleting element) element at the end of the array, getting
the element from a particular index, and inserting and deleting an element
from a particular index.
12
multi-dimensional array
• n-dimensional array
Is an array with more than one dimension, where n refers to the number of
dimensions. Here’s a simple breakdown:
1. Basic Concept:
o A multi-dimensional array can be thought of as an array of arrays.
A 1D array stores elements in a single row, a 2D array stores
elements in rows and columns, and an n-dimensional array stores
data across multiple dimensions.
2. Declaration:
o Syntax for declaring an n-dimensional array in C:
type array_name[size1][size2][size3]...[sizeN];
o Example for a 3D array:
int arr[2][3][4]; declares a 3-dimensional array.
3. Memory Layout:
o In memory, an n-dimensional array is stored as a contiguous block.
It uses row-major order, meaning elements of the last dimension
are stored consecutively in memory.
For example:
• A 3D array is a type of multi-dimensional array with n=3n = 3n=3.
• A 5D array is a multi-dimensional array with n=5n = 5n=5.
So, every n-dimensional array is a multi-dimensional array, but not all multi-
dimensional arrays are of the same dimensionality.
13
Applications of array
Storing data in tabular form (matrices)
Implementing stacks
Implementing queues
Hash tables
Dynamic programming
Sorting algorithms
Searching algorithms
Graph representations (adjacency matrices/lists)
Storing large datasets
String manipulation
14
Difference between 1D array and 2D array
15