SHEHZAIB
S202432016
DSA LAB
#include <iostream>
using namespace std;
class Array {
private:
int arr[10]={1,2,3,4,5,6,7,8,9,10}; // Array with fixed size
int size = 10; // Current number of elements in the array
public:
// Constructor to initialize array elements and size
Array() {
// Function to traverse and display the array elements
void traverse() {
cout << endl;
for (int i = 0 ; i< 10 ; i++){
cout << arr[i] << " ";
}
// Function to search for a specific element in the array
void search() {
// Implementation for searching an element in the array
cout << endl;
cout << "Enter target = ";
int target ;
cin >> target ;
int condition = 0;
for (int i = 0 ; i < 10 ; i++){
if (arr[i]== target ){
cout << "Target found at " << i << " Index";
condition = 1 ;
break;
}
}
if (condition == 0){
cout << "Target Not Found " ;
}
// Function to insert a new element at a specified position
void insert() {
// Implementation for inserting an element in the array
int insertions;
cout << "How many values do you want to insert? ";
cin >> insertions;
for (int i = 0; i < insertions; i++) {
int value, position;
cout << "Enter the value to insert: ";
cin >> value;
cout << "Enter the position to insert (0 to " << size << "):
";
cin >> position;
for (int j = size; j > position; j--) {
arr[j] = arr[j - 1];
}
arr[position] = value;
size++;
}
cout << "Updated array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
// Function to update the value of an existing element
void update() {
// Implementation for updating an element in the array
for (int i = 0 ; i < size ; i ++){
cout << arr[ i ] << " ";
cout << "\nUpdating the array \n" ;
arr[2]= 100 ;
for (int i = 0 ; i < size ; i ++){
cout << arr[ i ] << " ";
}
}
// Function to delete an element from the array
void remove() {
// Implementation for deleting an element from the array
int elementToDelete;
cout << "Enter the element you want to delete: ";
cin >> elementToDelete;
int newArr[10];
int newSize = 0;
for (int i = 0; i < size; i++) {
if (arr[i] != elementToDelete) {
newArr[newSize] = arr[i];
newSize++;
}
}
cout << "Array after deleting " << elementToDelete << ": ";
for (int i = 0; i < newSize; i++) {
cout << newArr[i] << " ";
}
cout << endl;
}
// Function to delete an element from the array
void sort() {
// Implementation for sorting an element from the array
int i;
for( i = 0 ; i <size - 1 ; i++){
for(int j = i+1 ; j < size ; j++) { // Sorting in
DO
if (arr[j]>arr[i]){
int temp = arr[j];
arr[j]=arr[i];
arr[i]=temp ;
}
}
}
cout <<"Sorted Array\n";
for (int i =0 ; i< size ; i++){
cout <<arr[i] << " ";
}
}
};
int main() {
Array array;
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. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
array.traverse();
break;
case 2:
array.search();
break;
case 3:
array.insert();
break;
case 4:
array.update();
break;
case 5:
array.remove();
break;
case 6:
array.sort();
break;
case 7:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice! Try again." << endl;
}
} while (choice != 6);
return 0;
}
OUTPUTS:
1.
2.
3.
4.
5.
6.