Error on linear search

+1 Putra Adhie · November 20, 2014
Hello everybody, i'm putra... i'm from indonesia

I was making a linear search program "Looking for number in existing data"
here's the screenshot of the program

/images/forum/upload/2014-11-20/4ddcf113fbd88ef94c74d0851b8ee47b.PNG

But i have a trouble here...
if i try to run these program, and i entered the number which not exist in data...
then the result will be like this...


/images/forum/upload/2014-11-20/4fad32a8fdbf9d7e1fbc0dfbd936b06e.PNG


should, if the data does not exist there, then that will appear is this "Item not found"


can you help me out to find where is the error ?
i'm using borland c++ here, it might be looks different than any application for c++

Hope anyone read my posting and give me some answer, because my head is gonna blow up thinking about this...

I'm sorry before if my english language is so bad, because i'm using the google translate


Here's the code...


#include <iostream>
#include <conio>


int linear_search(int array[],int s,int val);


main()
{

clrscr();
const int array_size=10;
int array[array_size]={25,36,2,48,0,69,14,22,7,19};

cout<<"=======================Linear Search===================="<<endl;
cout<<"========================================================"<<endl;
gotoxy (1,24);
cout<<"========================================================";
gotoxy(1,5);

cout<<"Element of array : "<<endl;
cout<<"\n Array : "<<"\t\t Data : "<<endl;

for(int count=0;count<array_size;count++)
{
cout<<"\t"<<" array ["<<count<<"]"<<"\t\t";
cout<<array[count]<<endl; }

int searching_element=0;
int flag=0;
cout<<"\n\nEnter the number you want to search : ";
cin>>searching_element;


flag=linear_search(array,array_size,searching_element);
if (flag!=-1)
cout<<"\nItem found in position : array [ "<<flag<<" ]";
else
cout <<"\nItem not found";


getch();
return 0;
}


int linear_search(int array[],int s,int val){
for(int i=0;i<s;i++){
if(array==val)
return i;
      }
return 0;
      }

Post a Reply

Replies

Oldest  Newest  Rating
0 Homer Simpson · November 20, 2014
You're welcome. :D 
0 Putra Adhie · November 20, 2014
Hahaha LOL !!
I was input return 1;

Yeay, The programs work perfectly... thank you for your helping
:D
0 Homer Simpson · November 20, 2014
/images/forum/upload/2014-11-20/ac4599a0c7ad7ee2fd53261765acb46c.png
did you put negative 1?
0 Putra Adhie · November 20, 2014
this is the result

/images/forum/upload/2014-11-20/b5395c23fc75cb7df9c7fe914eec1aa2.PNG

I want, if the value is not there, then display the "Item not found" and if the value is there, then show

99 is not part of data... then the result must be "Item not found"
Not "Item found in position : array [ 9 ]"


Do you have another solution ?
0 Homer Simpson · November 20, 2014
Change return 0 to return -1
0 Homer Simpson · November 20, 2014
In your linear search function you're returning 0 if the value is not found in the array... 
  • 1

C++

106,961 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator