0% found this document useful (0 votes)
184 views2 pages

To Perform Linear Search: Practical - 1

This document describes the linear search algorithm to find a target value in an array. It contains the pseudocode which includes initializing an index i to 1, checking if i is greater than the array length n to end the search, comparing the element at index i to the target x and incrementing i if they are not equal, printing the index if they are equal, and otherwise printing that the element was not found. It also includes C++ code to implement the linear search algorithm on an integer array, taking the array and target number as input and printing the index if the number is found or a not found message.

Uploaded by

Basit Afzal
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)
184 views2 pages

To Perform Linear Search: Practical - 1

This document describes the linear search algorithm to find a target value in an array. It contains the pseudocode which includes initializing an index i to 1, checking if i is greater than the array length n to end the search, comparing the element at index i to the target x and incrementing i if they are not equal, printing the index if they are equal, and otherwise printing that the element was not found. It also includes C++ code to implement the linear search algorithm on an integer array, taking the array and target number as input and printing the index if the number is found or a not found message.

Uploaded by

Basit Afzal
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/ 2

DAA LAB Roll no.

1702678

Practical -1
To perform linear search

Algorithm:
Linear Search ( Array A, Value x)

Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

Program:
# include<iostream>
using namespace std;
int main()
{
int i, num, n, c=0, pos;
cout<<"Enter the array size : ";
cin>>n;
int arr[n];
cout<<"Enter Array Elements : ";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter the number to be search : ";
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{
cout<<"Number not found..!!";
}
else
{
cout<<num<<" found at position "<<pos;
}
}

Page no. 1
DAA LAB Roll no.1702678

Output:

Page no. 2

You might also like