0% found this document useful (0 votes)
7 views13 pages

DSA Lab Task 1

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

DSA Lab Task 1

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

Task 01

Name: Kamal Shah


Class ID: CU-4320-2023
Subject: DSA Lab
Submitted to: Sikander Azam
Date: 11/ 18/ 2024
Task 01 :
Write a program to search for a specific number in a list of 10 integers using linear search.

#include <iostream>
using namespace std;

int main() {
int numbers[10];
int target, index = -1;

cout << "Enter 10 integers:" << endl;


for (int i = 0; i < 10; i++) {
cin >> numbers[i];
}

cout << "Which no. U wanna to search for: ";


cin >> target;

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


if (numbers[i] == target) {
index = i;
break;
}
}

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;

cout << "Which fruit U wanna search: ";


for (int i = 0; i < 4; i++) {
cout << fruits[i];
if (i < 4 - 1) cout << ", ";
}
cout << endl;

cout << "Enter the fruit to search for: ";


cin >> target;

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


if (fruits[i] == target) {
index = i;
break;
}
}

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

for (int i = 1; i < 7; i++) {


if (numbers[i] < smallest) {
smallest = numbers[i];
}
}

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;

auto it = find(list.begin(), list.end(), element);


if (it != list.end()) {
list.erase(it);
cout << "Element deleted successfully.\n";
} else {
cout << "Element not found.\n";
}
}

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

for (size_t i = 0; i < list.size(); ++i) {


if (list[i] == element) {
cout << "Element found at index " << i << ".\n";
return;
}
}
cout << "Element not found.\n";
}

// 4. Traversing Function
void traversing() {
if (list.empty()) {
cout << "List is empty.\n";
return;
}

cout << "List elements: ";


for (int element : list) {
cout << element << " ";
}
cout << "\n";
}

// 5. Smallest Element Function


void smallest() {
if (list.empty()) {
cout << "List is empty.\n";
return;
}

int min = list[0];


for (int element : list) {
if (element < min) {
min = element;
}
}
cout << "Smallest element: " << min << "\n";
}

// 6. Largest Element Function


void largest() {
if (list.empty()) {
cout << "List is empty.\n";
return;
}

int max = list[0];


for (int element : list) {
if (element > max) {
max = element;
}
}
cout << "Largest element: " << max << "\n";
}

// Main Menu Function


void menu() {
int choice;
do {
cout << "\n--- Linear Search Menu ---\n";
cout << "1. Insertion\n";
cout << "2. Deletion\n";
cout << "3. Searching\n";
cout << "4. Traversing\n";
cout << "5. Smallest\n";
cout << "6. Largest\n";
cout << "7. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy