DSA Lab Task 1
DSA Lab Task 1
#include <iostream>
using namespace std;
int main() {
int numbers[10];
int target, index = -1;
if (index != -1) {
cout << "Number found at index " << index << "." << endl;
} else {
cout << "Number not found." << endl;
}
return 0;
Task 02 :
Write a program to search for a specific word in a list e.g. [“Apple , Orange , Peach,
Grapes”].
#include <iostream>
#include <string>
using namespace std;
int main() {
string fruits[4] = {"Apple", "Orange", "Peach", "Grapes"};
string target;
int index = -1;
if (index != -1) {
cout << "Word found at index " << index << "." << endl;
} else {
cout << "Word not found." << endl;
}
return 0;
}
Task 03 :
Write a Program to search for smallest number in a list e.g. [
11, 55, 2 ,0 ,77 ,5 ,63 ].
#include <iostream>
using namespace std;
int main() {
int numbers[7] = {11, 55, 2, 0, 77, 5, 63};
int smallest = numbers[0];
cout << "The smallest number in the list is: " << smallest << endl;
return 0;
}
Task 04:
Write a program to implement a linear search algorithm using functions: 1. Insertion
2. deletion 3. Searching 4. Traversing
5. smallest 6. largest 7. exit
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class LinearSearch {
private:
vector<int> list;
public:
// 1. Insertion Function
void insertion() {
int element;
cout << "Enter element to insert: ";
cin >> element;
list.push_back(element);
cout << "Element inserted successfully.\n";
}
// 2. Deletion Function
void deletion() {
if (list.empty()) {
cout << "List is empty. Cannot delete.\n";
return;
}
int element;
cout << "Enter element to delete: ";
cin >> element;
// 3. Searching Function
void searching() {
if (list.empty()) {
cout << "List is empty. Cannot search.\n";
return;
}
int element;
cout << "Enter element to search: ";
cin >> element;
// 4. Traversing Function
void traversing() {
if (list.empty()) {
cout << "List is empty.\n";
return;
}
switch (choice) {
case 1: insertion(); break;
case 2: deletion(); break;
case 3: searching(); break;
case 4: traversing(); break;
case 5: smallest(); break;
case 6: largest(); break;
case 7: cout << "Exiting program.\n"; break;
default: cout << "Invalid choice. Try again.\n";
}
} while (choice != 7);
}
};
int main() {
LinearSearch search;
search.menu();
return 0;
}