0% found this document useful (0 votes)
365 views6 pages

Aiman Dsa Lab 3

The document contains the details of 7 tasks completed by a student. It includes the code and output for programs implementing linear and binary search on different arrays. Task 1 involves linear search on an integer array to find a given item. Task 2 uses binary search to find a roll number in a sorted array. The later tasks apply similar search techniques to arrays containing weights, names and other data.

Uploaded by

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

Aiman Dsa Lab 3

The document contains the details of 7 tasks completed by a student. It includes the code and output for programs implementing linear and binary search on different arrays. Task 1 involves linear search on an integer array to find a given item. Task 2 uses binary search to find a roll number in a sorted array. The later tasks apply similar search techniques to arrays containing weights, names and other data.

Uploaded by

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

NAME : AIMAN FAREED

ENROLLMENT: 02-235201-045

LAB 3
TASK 1

Write a program the implements the linear search on array having 15 integers provided by user.
Program prints the location of ITEM if found and print message” ITEM not found” in case of
match not found.

CODE:
#include<iostream>
using namespace std;
int main()
{
int a[15];
cout << "enter value of a";
for (int i = 0; i < 15; i++) {
cin >> a[i];
}
int item = 5;
for (int i = 0; i < 15; i++) {
if (a[i] == item)
{
cout << "location is " << i;

}
if (i == 15)
{
cout << "location not found";

}
system("pause");
}

OUTPUT:
NAME : AIMAN FAREED
ENROLLMENT: 02-235201-045

TASK 2:

Consider the following array that stores the roll # of students in sorted order. Write a program
that finds the location of roll # provided by user. If roll# not found, prints the message ”Roll
number not found”

100 110 120 122 123 124 125 126 127 128
129 130 131 132 133 134 135 136 144 149

CODE:

#include<iostream>
using namespace std;
int main()
{
int i, arr[20], num, first, last, middle;
cout << "Enter 10 Elements (in ascending order): ";
for (i = 0; i < 20; i++)
cin >> arr[i];
cout << "\nEnter Element to be Search: ";
cin >> num;
NAME : AIMAN FAREED
ENROLLMENT: 02-235201-045
first = 0;
last = 19;
middle = (first + last) / 2;
while (first <= last)
{
if (arr[middle] < num)
first = middle + 1;
else if (arr[middle] == num)
{
cout << "\nThe number, " << num << " found at Position " << middle + 1;
break;
}
else
last = middle - 1;
middle = (first + last) / 2;
}
if (first > last)
cout << "\nThe number, " << num << " is not found in given Array";
cout << endl;
return 0;
}

OUTPUT:
NAME : AIMAN FAREED
ENROLLMENT: 02-235201-045

TASK 3:

Consider an array that stores the square of 1-25 numbers in unsorted order. Write a program
that takes input from user a number and display the location of squared number that stores in
an array.

For example
Input : 5
Output : Location of 25 is 6th index

CODE:

OUTPUT:

TASK 4:

Consider the following array “Weight” that stores the weight of boxes in kilograms.

21.5 15.0 12.5 25.0 17.5 24.0 35.0 40.0 27.5 22.0
33.0 12.5 23.5 10.25 5.5 45.5 19.25 12.25 32.0 20.0

Write a program that finds the location of weight provided by user and print message ”not
found” in case of match not found

CODE:

OUTPUT:

TASK 5:
NAME : AIMAN FAREED
ENROLLMENT: 02-235201-045

Consider the array in task 4.

a. Write a program that arrange the weights in ascending order and find the location of the weight
provided by user.
b. Write a program that finds the heaviest weight.
c. Write a program that finds the lightest weight.

CODE:

OUTPUT:

TASK 6:

Consider alphabetized array “NAME”

Allen Clark Dickens Edwards Goodman Hobbs Irwin


Klein Lewis Morgan Richards Scott Tucker Walton

Write a program that prints number comparisons required to locate Hobbs, Morgan and Fisher
using linear search.

CODE:

OUTPUT:

TASK 7:

Suppose binary search algorithm is applied to the array NAME (given in task 6) to find the
location of Goodman. Write a program that finds BEG, END MID for the test segment in each
step of the algorithm. It also prints how many comparison are required to find Goodman?
NAME : AIMAN FAREED
ENROLLMENT: 02-235201-045

CODE:

OUTPUT:

You might also like