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

Dsa Assignment 1 Anees

The document contains a C++ program for a Data Structures and Algorithms assignment, implementing four key operations: inserting an element into an array, deleting an element from an array, performing a linear search, and performing a binary search. Each operation includes user prompts for input and outputs the results accordingly. The program runs in a loop until the user chooses to exit.

Uploaded by

40096
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Dsa Assignment 1 Anees

The document contains a C++ program for a Data Structures and Algorithms assignment, implementing four key operations: inserting an element into an array, deleting an element from an array, performing a linear search, and performing a binary search. Each operation includes user prompts for input and outputs the results accordingly. The program runs in a loop until the user chooses to exit.

Uploaded by

40096
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 11

SUBMITTED TO:

Mr.Sheikh Abdul Wahab

SUBMITTED BY:
Anees Yameen
40096
BSSE(Spring) 2025
PROGRAM:
Data Structure and Algorithm
Assignment : 01

Tasks 1 to 4 :

Code Implementation :
#include <iostream>

#include <vector>
using namespace std;

// --- Question No 1: Insert Element ---

void insertElement() {

int size;

cout << "Enter the initial size of the array: ";

cin >> size;

vector<int> arr(size);

cout << "Enter the elements of the array: ";

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

cin >> arr[i];

int position, element;

cout << "Enter the position to insert (0 to " << size << "): ";

cin >> position;

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

cin >> element;

if (position < 0 || position > size) {

cout << "Invalid position for insertion." << endl;

return;

}
arr.push_back(0); // Temporarily increase size

for (int i = size; i > position; --i) {

arr[i] = arr[i - 1]; // Shift elements to the right

arr[position] = element; // Insert the element

cout << "Array after insertion: ";

for (int val : arr) {

cout << val << " ";

cout << endl;

// --- Question No 2: Delete Element ---

void deleteElement() {

int size;

cout << "Enter the initial size of the array: ";

cin >> size;

vector<int> arr(size);

cout << "Enter the elements of the array: ";

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

cin >> arr[i];

}
int position;

cout << "Enter the position to delete (0 to " << size - 1 << "): ";

cin >> position;

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

cout << "Invalid position for deletion." << endl;

return;

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

arr[i] = arr[i + 1]; // Shift elements to the left

arr.pop_back(); // Decrease the size

cout << "Array after deletion: ";

for (int val : arr) {

cout << val << " ";

cout << endl;

// --- Question No 3: Linear Search ---

void linearSearch() {

int size;

cout << "Enter the size of the unsorted array: ";


cin >> size;

vector<int> arr(size);

cout << "Enter the elements of the array: ";

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

cin >> arr[i];

int elementToSearch;

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

cin >> elementToSearch;

int index = -1; // Initialize index to -1 (not found)

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

if (arr[i] == elementToSearch) {

index = i; // Element found at index i

break; // Stop searching once found

if (index != -1) {

cout << "Element " << elementToSearch << " found at index " << index << endl;

} else {

cout << "Element " << elementToSearch << " not found in the array." << endl;

}
}

// --- Question No 4: Binary Search ---

void binarySearch() {

int size;

cout << "Enter the size of the sorted array: ";

cin >> size;

vector<int> arr(size);

cout << "Enter the elements of the sorted array (in increasing order): ";

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

cin >> arr[i];

int elementToSearch;

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

cin >> elementToSearch;

int low = 0;

int high = size - 1;

int index = -1; // Initialize index to -1 (not found)

while (low <= high) {

int mid = low + (high - low) / 2; // Calculate middle index


if (arr[mid] == elementToSearch) {

index = mid; // Element found at middle

break;

} else if (arr[mid] < elementToSearch) {

low = mid + 1; // Search in the right half

} else {

high = mid - 1; // Search in the left half

if (index != -1) {

cout << "Element " << elementToSearch << " found at index " << index << endl;

} else {

cout << "Element " << elementToSearch << " not found in the array." << endl;

int main() {

int choice;

do {

cout << "\nChoose an operation:" << endl;

cout << "1. Insert element into array" << endl;

cout << "2. Delete element from array" << endl;

cout << "3. Linear search" << endl;


cout << "4. Binary search" << endl;

cout << "0. Exit" << endl;

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

insertElement();

break;

case 2:

deleteElement();

break;

case 3:

linearSearch();

break;

case 4:

binarySearch();

break;

case 0:

cout << "Exiting program." << endl;

break;

default:

cout << "Invalid choice. Please try again." << endl;

} while (choice != 0);


return 0;

Screenshot :

You might also like