linear search (c++)

2
LINEAR SEARCH OUTPUT SOURCE CODE physics@SGTK045:~$ cd Desktop/ Tashmeet #include <iostream>//include for header file physics@SGTK045:~$/Desktop/Tashmeet c++linearsearch.cpp –o linearsearchout using namespace std; physics@SGTK045:~$ /Desktop/ Tashmeet $ ./linearsearch.out void search(int[],int,int);//function declaration Enter the elements of array int main()//main body 6 { Enter the elements of array int A[20],size,num,i,pos,flag;//variable of program 1 cout<<"Enter the number of elements in array"<<endl; 2 cin>>size;//input size 5 cout<<"Enter the elements of array"<<endl; 8 for(i=0;i<size;i++)//iteration statement 10 cin>>A[i]; 6 cout<<"Enter the number of elements Enter the element to be searched for to be searched for"<<endl; 10 cin>>num;//input the number of elements Element found at position 5 search (A,size,num);//function calling physics@SGTK045:~$ cd Desktop/Tashmeet } physics@SGTK045:~$/Desktop/Tashmeet c++linearsearch.cpp –o linearsearchout void search ( int A[100], int size,int num) physics@SGTK045:~$ /Desktop/Tashmeet$ ./linearsearch.out { Enter the elements of array int flag=0,pos; 4 for ( int i=0;i<size;i++) Enter the elements of array if (A[i]==num) 66 { 78 flag=1; 10 pos=i; 55 break; Enter the element to be searched for } 78 if (flag==0) Element found at postion 4

Upload: joe-hardy

Post on 09-Apr-2016

7 views

Category:

Documents


0 download

DESCRIPTION

A C++ Program to search a given element in an array using functions and return the address of the given element in the array.

TRANSCRIPT

Page 1: Linear Search (C++)

LINEAR SEARCH OUTPUT

SOURCE CODE physics@SGTK045:~$ cd Desktop/ Tashmeet

#include <iostream>//include for header file physics@SGTK045:~$/Desktop/Tashmeet c++linearsearch.cpp –o linearsearchout

using namespace std; physics@SGTK045:~$ /Desktop/ Tashmeet $ ./linearsearch.out

void search(int[],int,int);//function declaration Enter the elements of array

int main()//main body 6

{ Enter the elements of array

int A[20],size,num,i,pos,flag;//variable of program 1

cout<<"Enter the number of elements in array"<<endl; 2

cin>>size;//input size 5

cout<<"Enter the elements of array"<<endl; 8

for(i=0;i<size;i++)//iteration statement 10

cin>>A[i]; 6

cout<<"Enter the number of elements Enter the element to be searched for

to be searched for"<<endl; 10

cin>>num;//input the number of elements Element found at position 5

search (A,size,num);//function calling physics@SGTK045:~$ cd Desktop/Tashmeet

} physics@SGTK045:~$/Desktop/Tashmeet c++linearsearch.cpp –o linearsearchout

void search ( int A[100], int size,int num) physics@SGTK045:~$ /Desktop/Tashmeet$ ./linearsearch.out

{ Enter the elements of array

int flag=0,pos; 4

for ( int i=0;i<size;i++) Enter the elements of array

if (A[i]==num) 66

{ 78

flag=1; 10

pos=i; 55

break; Enter the element to be searched for

} 78

if (flag==0) Element found at postion 4

cout<<"Element not found"<<endl;

else

cout <<"Element found at position"<<pos+1<<endl;

//getch();

}