Lab assignment 4
Lab assignment 4
SESSION: Fall-2024
SEMESTER: 3rd
DSA – Arrays | Basic Operations
DSA – Arrays | Basic Operations
Outlines
Lab - 3
Data Structures & algorithm Introduction
Types
Basic Operations
Array Data Structures
Lab - 4
DSA – Arrays | Basic Operations
Data Structures and Algorithms (DSA) form the foundation of computer science, focusing on
how data is stored, organized, and processed efficiently. A data structure is a specialized
format for organizing and managing data, such as arrays, linked lists, trees, stacks, and
graphs, each designed for specific tasks like efficient access, insertion, and deletion. An
algorithm is a step-by-step procedure or set of rules designed to perform operations like
searching, sorting, and merging data. Together, DSA helps optimize resource usage, improve
computational speed, and solve complex problems effectively in software development and
systems design.
Types
DSA – Arrays | Basic Operations
Basic Operations
Traversing
Accessing each element of a data structure exactly once to process or inspect them.
Searching
Finding the location of a specific element in a data structure.
Insertion
Adding a new element to a data structure at a specified position.
Updation
An update operation replaces the existing element at a specified position in a data structure
with a new value.
Deletion
Removing an existing element from a data structure.
Sorting
Arranging the elements of a data structure in a specific order (e.g., ascending or descending).
Merging
Combining two or more data structures into one, maintaining a specific order or structure.
DSA – Arrays | Basic Operations
Traversing
Traversing an array refers to the process of accessing each element in the array, one by one,
in order to either perform some operation on them (like printing or modifying the values) or
to search for a specific value.
Imagine you have a shopping list, and you want to review each item to make sure you've
purchased everything. In this case, the shopping list can be thought of as an array, and
traversing it means going through each item in the list.
Code :
DSA – Arrays | Basic Operations
Searching
Searching an array involves looking for a specific value within a collection (array) of elements.
If the value is found, its position (index) is returned; if not, the search concludes that the
element doesn't exist in the array.
Code:
DSA – Arrays | Basic Operations
Insertion
Insertion in an Array refers to the process of adding a new element at a specific position in an
array, shifting subsequent elements to the right to make space. Insertion may require
rearranging elements depending on where the new element is placed.
Real-life Example:
Imagine a bookshelf where books are arranged in a specific order (e.g., by size or topic). If you
want to add a new book between two others, you'd need to shift the books to the right to
make space for the new one.
Code:
DSA – Arrays | Basic Operations
Updation
Updation in an Array refers to the process of modifying the value of an existing element in an
array at a specific index. Essentially, you're replacing the old value at that index with a new one.
Real-Life Example:
Imagine you are managing a student grades list for a class, where each student's grade is
stored in an array. Initially, you record the grades as they are given after the first exam. Later,
one student asks for a re-evaluation, and their grade gets updated. This change would require
you to update the grade in the array.
Code:
DSA – Arrays | Basic Operations
Deletion
Deletion in an array refers to the process of removing an element from the array, which can
involve shifting the remaining elements to maintain the structure of the array. Since arrays
have a fixed size, when an element is deleted, the overall size of the array does not change, but
the positions of the elements may need to be adjusted.
Real-Life Example:
Imagine an array represents a lineup of students waiting to enter a classroom, where each
number corresponds to a student's ID. If one student (ID 92) decides to leave the lineup, we
need to remove them and have the remaining students shift forward.
DSA – Arrays | Basic Operations
Code:
DSA – Arrays | Basic Operations
Sorting
Sorting in an array refers to the process of arranging the elements in a particular order, typically
in ascending or descending order. This operation is fundamental in computer science and is
used in various applications, from searching data to organizing information for better
readability
Code:
DSA – Arrays | Basic Operations
Task
Objective:
Write a program that performs the following operations on an array of size 10:
Requirements:
• The array should be initialized with 10 elements (either user input or default values).
• Create a menu to allow the user to select the desired operation.
• Implement the following functionalities in the program:
DSA – Arrays | Basic Operations
#include <iostream>
using namespace std;
void traverse() {
cout << "Current elements in the array: ";
for (int i = 0; i < SIZE; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
void search() {
int target;
cout << "Enter the element to search for: ";
cin >> target;
void insert() {
int position, element;
cout << "Enter the position to insert (0 to " << SIZE - 1 << "): ";
cin >> position;
void update() {
int position, newValue;
cout << "Enter the position to update (0 to " << SIZE - 1 << "): ";
cin >> position;
cout << "Enter the new value for the element: ";
cin >> newValue;
arr[position] = newValue;
cout << "Element updated successfully!" << endl;
traverse();
}
void deletion() {
int position;
cout << "Enter the position to delete (0 to " << SIZE - 1 << "): ";
cin >> position;
void sortArray() {
DSA – Arrays | Basic Operations
void mergeArrays() {
int arr2[SIZE], mergedArray[2 * SIZE];
cout << "Enter elements for the second array (" << SIZE << " elements): ";
for (int i = 0; i < SIZE; i++) {
cin >> arr2[i];
}
int main() {
int choice;
do {
cout << "\nOperations:\n";
cout << "1. Traverse\n";
cout << "2. Search\n";
cout << "3. Insert\n";
cout << "4. Update\n";
cout << "5. Delete\n";
cout << "6. Sort\n";
cout << "7. Merge\n";
cout << "8. Exit\n";
cout << "Enter your choice: ";
DSA – Arrays | Basic Operations
switch (choice) {
case 1:
traverse();
break;
case 2:
search();
break;
case 3:
insert();
break;
case 4:
update();
break;
case 5:
deletion();
break;
case 6:
sortArray();
break;
case 7:
mergeArrays();
break;
case 8:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice! Try again." << endl;
}
} while (choice != 8);
return 0;
}