0% found this document useful (0 votes)
27 views1 page

Sequential Search

This document describes a sequential search algorithm to search for a value in an array. It defines a function to read user input into an array, a search function that iterates through the array using an index and returns the location of the matched value or size if not found, and a main function that calls the read and search functions to search an array for a user-input value and output the result.

Uploaded by

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

Sequential Search

This document describes a sequential search algorithm to search for a value in an array. It defines a function to read user input into an array, a search function that iterates through the array using an index and returns the location of the matched value or size if not found, and a main function that calls the read and search functions to search an array for a user-input value and output the result.

Uploaded by

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

Sequential search - searching for a certain value in an array

#include <iostream>
using namespace std;

const int size=5;

void readArray(int arr[],int size)


{
for(int i=0;i<size;i++)
{
cout<<"Enter a value to place in the array: ";
cin>>arr[i];
}
}

int search(int arr[],int value)


{
int i=0;
bool found=false;

while(!found&&i<size)
{
if (arr[i]==value)
found=true;
else
++i;
}
return i;
}

int main()
{
int x;
int arr[size];
readArray(arr,size);
cout<<"Enter a value to search for in the array: ";
cin>>x;
int loc=search(arr,x);
if(loc>=0&&loc<size)
cout<<"Found at location "<<loc+1;
else
cout<<"Not found";
cout<<endl;
system ("pause");
return 0; }

You might also like