Computer Science & Engineering: Department of

You might also like

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment Title 1.2

Student Name: Gulshan Singh UID: 21BCS2858


Branch: CSE Section/Group:608-A
Semester: 3rd
Date of Performance:29/08/22 Subject Name-Data Structures
Subject Code:21CSH-211

1. Aim/Overview of the practical:

Write a menu driven program to perform the following operations on a one


dimensional array:-

a) Linear Search
b) Binary Search (only Sorted)

2. Source Code:

#include <iostream>

using namespace std;

void

binarySearch (int a[], int size, int key)

int pos, chck;

int s = 0;

int e = size;

while (e > s)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

int mid = (s + e) / 2;

if (a[mid] == key)

pos = mid;

chck = 1;

break;

else if (a[mid] > key)

e = mid;

else

s = mid;

if (chck == 1)

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

}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

else

cout << "NOT FOUND\n";

void

LinearSearch (int a[], int size, int key)

int pos;

bool chck;

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

if (a[i] == key)

chck = true;

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

else

chck = false;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

if (chck = false)

cout << "not found\n";

int

main ()

int n;

cout << "Enter size of array\n";

cin >> n;

int a[n];

cout << "\nEnter an Array \n";

for (int i = 0; i < n; i++)

cin >> a[i];

} int key;

cout << "Enter element to search\n";


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

cin >> key;

ABC:cout << "Option 1 : Linear Search\n";

cout << "Option 2: Binary Search(if Array is Sorted)\n";

int ch;

cin >> ch;

switch (ch)

case 1:

LinearSearch (a, n, key);

break;

case 2:

binarySearch (a, n, key);

break;

case 3:

exit (1);

goto ABC;

return 0;

}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

3. Result/Output

Learning outcomes (What I have learnt):

1. I have learnt about the insertion and deletion on arrays

2. To perform linear search

3. To write a menu driven program using switch case

4. Iteration through the arrays


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Evaluation Grid :

Sr. No. Parameters Marks Obtained Maximum Marks

1. Student Performance 12
(Conduct of experiment)
objectives/Outcomes.

2. Viva Voce 10

3. Submission of Work 8
Sheet (Record)

Total 30

You might also like