0% found this document useful (0 votes)
36 views15 pages

Unit2 DSC DivA L0B

Uploaded by

thebigbull405
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)
36 views15 pages

Unit2 DSC DivA L0B

Uploaded by

thebigbull405
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/ 15

UNIT II Linear Data Structure Using

Sequential Organization

Introduction to sequential organization


Sequential organization is a method of structuring information in a linear, step-
by-step manner. This approach is often used in various fields, such as writing,
programming, and project management, to ensure clarity and logical
progression.
Key Features:
Chronological Order: Events or steps are presented in the order they occur,
making it easy to follow the timeline.
Cause and Effect: Each step or point can build upon the previous one,
establishing clear relationships and outcomes.
Clarity and Understanding: This structure helps the audience easily grasp
complex information by breaking it down into manageable parts.
Applications:
Writing: Used in narratives and instructional texts to guide readers through a
process.
Programming: In coding, algorithms often follow sequential logic to execute
tasks effectively.
Project Management: Tasks are organized in a sequence to streamline
workflows and enhance productivity.
Conclusion:
Sequential organization is an effective way to present information logically and
coherently, making it easier for the audience to follow along and comprehend
the material.

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.

Data Type Default Value

bool false

char 0

int 0

float 0.0

double 0.0f

void

wchar_t 0

Traverse Array Operations


The elements of an array are traversed during this procedure.
Example
The following program iterates through an array and outputs each element:
3
• 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]);
}
}
Output
The original array elements are:
Arr[0] = 1
Arr[1] = 3
Arr[2] = 5
Arr[3] = 7
Arr[4] = 8

Insertion Array Operations


One or more data elements are added to an array using the insert array
operations. An array can have a new element inserted at any index, including
the start, end, or middle, depending on the need.
In this example, the insertion action is implemented practically by adding data
to the end of the array.
Example
The implementation of the above algorithm is shown below.

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

Deletion Array Operations


Deletion is the process of eliminating an existing element from an array and
rearranging all of the array’s elements.
Algorithm
Take into consideration that K is a positive integer such that K=N and Arr is a
linear array with N items. The algorithm to remove one element from the Kth
position of Arr is shown below.
• Start
• Set J = K
• Repeat steps 4 and 5 while J < N
• Set Arr[J] = Arr[J + 1]
• Set J = J+1
• Set N = N-1
• Stop
Example
The implementation of the above algorithm is shown below.
• C
#include <stdio.h>
void main() {

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

Update Array Operations


Update array operations involve changing an existing array element at a certain
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 technique to update an element that is accessible
at the Kth position of Arr is shown below.
• Start
• Set Arr[K-1] = ITEM
• Stop

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

ADDRESS CALCULATION ON ARRAY

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.

Two Dimensional Array


The two-dimensional 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. However, 2D arrays are created to implement a
relational database lookalike data structure. It provides ease of holding
the bulk of data at once which can be passed to any number of functions
wherever required.

Declaration of two dimensional Array in C


The syntax to declare the 2D array is given below.
1. data_type array_name[rows][columns];

Consider the following example.


1. int twodimen[4][3];

Here, 4 is the number of rows, and 3 is the number of columns.

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

You might also like