0% found this document useful (0 votes)
5 views

Lab assignment 4

Basic DSA codes

Uploaded by

unknowngaming727
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab assignment 4

Basic DSA codes

Uploaded by

unknowngaming727
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

DSA – Arrays | Basic Operations

Data Structures & Algorithms


LAB 04

Lab Instructor: Jawad Hassan


Department of Artificial Intelligence
Email: [email protected]

Name: M. Hasnain Fareed


ID: F2023376449

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 & algorithm Introduction

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

Array Data Structures

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.

Real-Life Example: Checking Items in a Shopping List

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.

Real-Life Example of Array Traversal


Imagine you have a bookshelf with 10 books. You want to find out if a particular book, say
"The Great Gatsby," is on your shelf. To do that, you'll start at the first book, check the title,
then move to the next book, and so on, until you find "The Great Gatsby" or reach the end of
the shelf. This is an example of traversing the array of books on your shelf.

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

Example: Organizing a Family Gathering by Age


Imagine you're organizing a family gathering and you have a list of family members along with
their ages. You want to sort them by age so that you can plan activities suited to different age
groups.
DSA – Arrays | Basic Operations

Code:
DSA – Arrays | Basic Operations

Task

Task: Data Structure : Array Operations with Menu

Objective:

Write a program that performs the following operations on an array of size 10:

1. Traversing – Display the current elements of the array.


2. Searching – Find a specific element in the array.
3. Inserting – Insert a new element into the array at a specified position.
4. Updating – Update the value of an existing element.
5. Deleting – Remove an element from the array.
6. Sorting – Arrange elements in the Array the array.

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;

const int SIZE = 10;


int arr[SIZE] = {20, 10, 40, 30, 60, 50, 80, 70, 100, 90};

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;

for (int i = 0; i < SIZE; i++) {


if (arr[i] == target) {
cout << "Element " << target << " found at index " << i << "." << endl;
return;
}
}
cout << "Element " << target << " not found in the array." << endl;
}

void insert() {
int position, element;

cout << "Current array: ";


traverse();

cout << "Enter the position to insert (0 to " << SIZE - 1 << "): ";
cin >> position;

if (position < 0 || position >= SIZE) {


cout << "Invalid position! Please enter a valid position between 0 and " << SIZE - 1 << "." << endl;
return;
}

cout << "Enter the element to insert: ";


cin >> element;
DSA – Arrays | Basic Operations

for (int i = SIZE - 1; i > position; i--) {


arr[i] = arr[i - 1];
}
arr[position] = element;

cout << "Element inserted successfully!" << endl;


traverse();
}

void update() {
int position, newValue;
cout << "Enter the position to update (0 to " << SIZE - 1 << "): ";
cin >> position;

if (position < 0 || position >= SIZE) {


cout << "Invalid position! Please enter a valid position between 0 and " << SIZE - 1 << "." << endl;
return;
}

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;

if (position < 0 || position >= SIZE) {


cout << "Invalid position! Please enter a valid position between 0 and " << SIZE - 1 << "." << endl;
return;
}

for (int i = position; i < SIZE - 1; i++) {


arr[i] = arr[i + 1];
}
arr[SIZE - 1] = 0; // Optional: Reset the last element to 0

cout << "Element deleted successfully!" << endl;


traverse();
}

void sortArray() {
DSA – Arrays | Basic Operations

for (int i = 0; i < SIZE - 1; i++) {


for (int j = i + 1; j < SIZE; j++) {
if (arr[i] > arr[j]) {
swap(arr[i], arr[j]);
}
}
}
cout << "Array sorted successfully!" << endl;
traverse();
}

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];
}

for (int i = 0; i < SIZE; i++) {


mergedArray[i] = arr[i];
}
for (int i = 0; i < SIZE; i++) {
mergedArray[SIZE + i] = arr2[i];
}

cout << "Arrays merged successfully! Merged array: ";


for (int i = 0; i < 2 * SIZE; i++) {
cout << mergedArray[i] << " ";
}
cout << endl;
}

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

cin >> choice;

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;
}

You might also like