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


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...


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);


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

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

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 : ";

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

return 0;

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

Post a Reply


Oldest  Newest  Rating
0 Homer Simpson · November 20, 2014
In your linear search function you're returning 0 if the value is not found in the array... 
0 Homer Simpson · November 20, 2014
Change return 0 to return -1
0 Putra Adhie · November 20, 2014
this is the result


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
did you put negative 1?
0 Putra Adhie · November 20, 2014
Hahaha LOL !!
I was input return 1;

Yeay, The programs work perfectly... thank you for your helping
0 Homer Simpson · November 20, 2014
You're welcome. :D 
  • 1



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

Bucky Roberts Administrator