Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Abdurahman Lab 2 Dsa

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

CS-201L

Data Structures and


Algorithms
Lab 02

Implementation of Conditional Statements, Loops


and Functions
Name: abdurahman
Roll no 21
Semester: 3rd

Presentation/Procedure Code Comments Response Total


& Conclusion
Lab Task

Write a C++ program to implement all the above-described algorithms and display
the following menu and ask the user for the desired operation.

CODE

#include <iostream>
using namespace std;

int getsize() {
int size;
cout << "Enter number of elements you want to insert: ";
cin >> size;
return size;
}

void getarray(int nums[], int size) {


for (int i = 0; i < size; i++) {
cout << "Enter element " << i + 1 << ": ";
cin >> nums[i];
}
}

void displayarray(int nums[], int size) {


cout << "\nThe element(s) inside array:\n";
for (int i = 0; i < size; i++) {
cout << nums[i] << "\t";
}
cout << endl;
}
void startinsert(int nums[], int& size, int element) {
cout << "Enter the element to add at the start: ";
cin >> element;
for (int i = size; i > 0; i--) {
nums[i] = nums[i - 1];
}
nums[0] = element;
size++;
}

void endinsert(int nums[], int& size, int element) {


cout << "Enter the element to add at the end: ";
cin >> element;
nums[size] = element;
size++;
}

void custominsert(int nums[], int& size, int element, int pos) {


cout << "Enter element for custom position: ";
cin >> element;
cout << "Enter position of " << element << " within 0 to " <<
size << ": ";
cin >> pos;

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


nums[i] = nums[i - 1];
}
nums[pos] = element;
size++;
}

void linearsearch(int nums[], int size, int element, int& loc) {


loc = -1;
cout << "Enter the element to search: ";
cin >> element;

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


if (nums[i] == element) {
loc = i;
break;
}
}

if (loc != -1)
cout << element << " found at position " << loc + 1 << endl;
else
cout << element << " not found in array.\n";
}

void delposition(int nums[], int& size, int loc) {


cout << "Enter the position of element to delete: ";
cin >> loc;
loc = loc - 1;

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


nums[i] = nums[i + 1];
}
size--;
}
void delelement(int nums[], int& size, int element, int& loc) {
loc = -1;
cout << "Enter the element to delete: ";
cin >> element;

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


if (nums[i] == element) {
loc = i;
break;
}
}

if (loc != -1) {
for (int i = loc; i < size - 1; i++) {
nums[i] = nums[i + 1];
}
size--;
cout << element << " deleted from array.\n";
} else {
cout << element << " not found in array.\n";
}
}

int main() {
int element, pos, loc;
int size = getsize();
int nums[100]; // Static array with maximum size

getarray(nums, size);

int choice;
bool exit = false;

while (!exit) {
// Displaying the menu
cout << "\n\n********* MAIN MENU *********\n";
cout << "1 --- For Insert at start\n";
cout << "2 --- For Insert at end\n";
cout << "3 --- For Insert at specific position\n";
cout << "4 --- Linear Search\n";
cout << "5 --- For Deletion at specific position\n";
cout << "6 --- For Deletion of specific number\n";
cout << "7 --- Display array\n";
cout << "8 --- Exit\n";
cout << "PLEASE ENTER YOUR CHOICE :- ";
cin >> choice;

switch (choice) {
case 1:
startinsert(nums, size, element);
displayarray(nums, size);
break;
case 2:
endinsert(nums, size, element);
displayarray(nums, size);
break;
case 3:
custominsert(nums, size, element, pos);
displayarray(nums, size);
break;
case 4:
linearsearch(nums, size, element, loc);
break;
case 5:
delposition(nums, size, pos);
displayarray(nums, size);
break;
case 6:
delelement(nums, size, element, loc);
displayarray(nums, size);
break;
case 7:
displayarray(nums, size);
break;
case 8:
exit = true;
cout << "Exiting the program.\n";
break;
default:
cout << "Invalid choice. Please enter a valid option.\n";
}
}

return 0;
}

Algorithm

1. Inserting at the Start


Step 1: Begin
Step 2: Read the element to insert
Step 3: Set i := size (current number of elements)
Step 4: Repeat step 5 while (i > 0):
Step 5: Set array[i] = array[i-1] (shift each element one position
to the right)
Step 6: Set i = i - 1
Step 7: Set array[0] = element (insert new element at index 0)
Step 8: Increment the size of the array
Step 9: End

2. Inserting at the End


Step 1: Begin
Step 2: Read the element to insert
Step 3: Set array[size] = element (insert the element at the
current last position)
Step 4: Increment the size of the array
Step 5: End

3. Inserting at a Specific Position


Step 1: Begin
Step 2: Read the element to insert and the position
Step 3: Set i := size (current number of elements)
Step 4: Repeat step 5 while (i > position):
Step 5: Set array[i] = array[i-1] (shift elements right to create
space)
Step 6: Set i = i - 1
Step 7: Set array[position] = element (insert the new element at
the specified position)
Step 8: Increment the size of the array
Step 9: End

4. Linear Search
Step 1: Begin
Step 2: Read the element to search
Step 3: Set found := false
Step 4: Set i := 0
Step 5: Repeat steps 6 to 7 while (i < size and found == false):
Step 6: If array[i] == element, set found = true and location = i
Step 7: Set i = i + 1
Step 8: If found = true, display the position of the element
Step 9: If found = false, display "element not found"
Step 10: End

5. Deleting from a Specific Position


Step 1: Begin
Step 2: Read the position to delete
Step 3: Set i := position
Step 4: Repeat step 5 while (i < size - 1):
Step 5: Set array[i] = array[i + 1] (shift elements left to fill the
gap)
Step 6: Set i = i + 1
Step 7: Decrease the size of the array by 1
Step 8: End

6. Deleting a Specific Element


Step 1: Begin
Step 2: Read the element to delete
Step 3: Set found := false
Step 4: Set i := 0
Step 5: Repeat steps 6 to 7 while (i < size and found == false):
Step 6: If array[i] == element, set found = true and position = i
Step 7: Set i = i + 1
Step 8: If found = true, repeat step 9 while (position < size - 1):
Step 9: Set array[position] = array[position + 1] (shift elements
left to remove the element)
Step 10: Decrease the size of the array by 1
Step 11: If found = false, display "element not found"
Step 12: End

Conclusion:

By writing this program, I am able to interact with arrays in a


dynamic way. I can insert elements at different positions,
delete specific elements, and search for values within the array.
This code gives me complete control over the array's contents,
while also teaching me how to efficiently manipulate arrays
through shifting elements. It helps me see how array
operations work behind the scenes, giving me a better
understanding of how data structures are managed in C++.

You might also like