cud any one tell me what is the problem in this program

A

ashishnh33

cud any one tell me what is the problem in this program
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int binarysearch(void)
int linearsearch(void)
class search
{
private:
int[20],beg,mid,end,,i,j,n,item;
public:
void input()
{
cout<<"\n how much element you want to enter";
cin>>n;
cout<<"\n enter the elements";
for(i=0;i<n;i++)
cin>>a;
cout<<"\n enter the element to be searched";
cin>>item;
beg=0;
end=n-1;
}
int binarysearch()
{
mid=(beg+end)/2;
while((beg<end)&&a[mid]!=item))
{
if (item>a[mid])
beg=mid+1;
mid=(beg+end)/2;
}
}
if (item==a[mid])
cout<<"item"<<item<<"is found at location"<<mid+1<<"position";
else
cout<<"item is not found";
}
int linearsearch()
{
int flag;
for(i=0;i<n;i++)
{
if(a==item)
{
flag=1;
cout<<"item"<<item<<"found at location"<<i+1;
break;
}
flag=0;
}
if(!flag)
cout<<"item not found";
}
};
void main()
{
clrscr();
search obj;
int choice;
do
{
cout<<"\n 1. linear search";
cout<<"\n 2. binary search";
cout<<"\n 3. exit";
cout<<"\n enter your choice";
cin>>choice;
switch(choice)
{
case 1:
obj.input();
obj.linearsearch();
break;
case 2:
obj.input();
obj.binarysearch();
break;
case 3:
exit(1);
}
}
while(choice!=3);
getch();
}
 
A

Alf P. Steinbach

* ashishnh33:
cud any one tell me what is the problem in this program

Please read the FAQ to learn how to post.

Short course: /describe/ the problem as best you can.

Since you don't describe any problem, I'll just list the ones I see
right off the code, without even trying the code.

#include<iostream.h>

This is a non-standard header.

#include<conio.h>

This is a non-standard header.

#include<stdlib.h>

int binarysearch(void)
int linearsearch(void)

Missing semicolons.

Also, style-wise, don't forward-declare functions. Why write more than
necessary? Why throw away good information the compiler can give you?

class search
{
private:
int[20],beg,mid,end,,i,j,n,item;

Anonymous array.

Extra comma.

public:
void input()
{
cout<<"\n how much element you want to enter";
cin>>n;
cout<<"\n enter the elements";
for(i=0;i<n;i++)
cin>>a;
cout<<"\n enter the element to be searched";
cin>>item;
beg=0;
end=n-1;
}


Indentation.

Use of global variables.

DO NOT EVER USE GLOBAL VARIABLES. It doesn't help to disguise them as
class member variables.

int binarysearch()
{
mid=(beg+end)/2;
while((beg<end)&&a[mid]!=item))
{
if (item>a[mid])
beg=mid+1;
mid=(beg+end)/2;
}
}
if (item==a[mid])
cout<<"item"<<item<<"is found at location"<<mid+1<<"position";
else
cout<<"item is not found";
}
int linearsearch()
{
int flag;
for(i=0;i<n;i++)
{
if(a==item)
{
flag=1;
cout<<"item"<<item<<"found at location"<<i+1;
break;
}
flag=0;
}
if(!flag)
cout<<"item not found";
}
};
void main()
{
clrscr();
search obj;
int choice;
do
{
cout<<"\n 1. linear search";
cout<<"\n 2. binary search";
cout<<"\n 3. exit";
cout<<"\n enter your choice";
cin>>choice;
switch(choice)
{
case 1:
obj.input();
obj.linearsearch();
break;
case 2:
obj.input();
obj.binarysearch();
break;
case 3:
exit(1);
}
}
while(choice!=3);
getch();
}


Indentation.
 
J

Jaspreet

ashishnh33 said:
cud any one tell me what is the problem in this program

What output do you want ? What exactly is coming ? Please let us know.
#include<iostream.h>
#include<conio.h>

Non-standard header. Avoid using this header unles you want your
program to be non-portable.
#include<stdlib.h>

There was some discussion on whether one should use <header_file> or
int binarysearch(void)
int linearsearch(void)

Why are these functions / methods declared outside the class ? I did
see their definition inside the class also.
class search
{
private:
int[20],beg,mid,end,,i,j,n,item;
public:
void input()

Try having all method declarations inside the class and their
definition outside the class unless you have short body methods which
can be inlined.
void main()

main() returns int.
{
clrscr();
Non-standard.

search obj;
int choice;
do
{
cout<<"\n 1. linear search";
cout<<"\n 2. binary search";
cout<<"\n 3. exit";
cout<<"\n enter your choice";
cin>>choice;
switch(choice)
{
case 1:
obj.input();
obj.linearsearch();
break;
case 2:
obj.input();
obj.binarysearch();
break;
case 3:
exit(1);

Why exit with a status 1 ? 0 represents success.
}
}
while(choice!=3);
getch();

return 0;
 
O

osmium

ashishnh33 said:
[Could] any one tell me what is the problem in this program[?]

It was riddled with syntax errors. This compiles and passes some
preliminary tests on the linear search.Do a line by line comparison between
this and what you posted.



#include<iostream>
#include<conio.h>
#include<cstdlib>

using namespace std;
//int binarysearch(void);
// the *first* thing you should have done is provide that missing semicolon
//int linearsearch(void);
/* prototypres such as this are not intended for member functions.
that purpose is served by the class definiton.
*/
class Search
{
private:
int a[20],beg,mid,end,i,j,n,item;
public:
void input();
int binarysearch();
int linearsearch();
};
//.......................
void Search::input()
{
cout<<"\n how much element you want to enter";
cin>>n;
cout<<"\n enter the elements";
for(i=0;i<n;i++)
cin>>a;
cout<<"\n enter the element to be searched";
cin>>item;
beg=0;
end=n-1;
}
#if 0
// do one thing at a time, easiest first.
// defer this until the other search works
//........................
int Search::binarysearch()
{
mid=(beg+end)/2;
while((beg<end)&&a[mid]!=item))
{
if (item>a[mid])
beg=mid+1;
mid=(beg+end)/2;
}
}
if (item==a[mid])
cout<<"item"<<item<<"is found at location"<<mid+1<<"position";
else
cout<<"item is not found";
}
#endif
//........................
int Search::linearsearch()
{
int flag;
for(i=0;i<n;i++)
{
if(a==item)
{
flag=1;
cout<<"item"<<item<<"found at location"<<i+1;
break;
}
flag=0;
}
if(!flag)
cout<<"item not found";
}
//==========================
int main()
{
//clrscr(); // my compiler won't accept this
Search obj;
int choice;
do
{
cout<<"\n 1. linear search";
cout<<"\n 2. binary search";
cout<<"\n 3. exit";
cout<<"\n enter your choice";
cin>>choice;
switch(choice)
{
case 1:
obj.input();
obj.linearsearch();
break;
case 2:
obj.input();
//obj.binarysearch();
break;
case 3:
exit(1);
}
}
while(choice!=3);
getch();
}
 
O

osmium

s:
cud any one tell me what is the problem in this program
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int binarysearch(void)
int linearsearch(void)
class search
{
private:
int[20],beg,mid,end,,i,j,n,item;

There are things there that should be local (automatic) variables in the
member functions themselves instead of here. This list should be limited to
things that truly belong to the object. I think the array and n belong
here. Almost certainly beg, mid and end belong to the binary search and
should be declared there. i and j probably belong to the linear search. I
don't know about item, I didn't try to figure out the logic. Rule of thumb:
things that have long term state belong here, other things do not.
 
N

Norm Mann

Alf P. Steinbach said:
* ashishnh33:
cud any one tell me what is the problem in this program

Please read the FAQ to learn how to post.

Short course: /describe/ the problem as best you can.

Since you don't describe any problem, I'll just list the ones I see right
off the code, without even trying the code.

#include<iostream.h>

This is a non-standard header.

#include<conio.h>

This is a non-standard header.

#include<stdlib.h>

int binarysearch(void)
int linearsearch(void)

Missing semicolons.

Also, style-wise, don't forward-declare functions. Why write more than
necessary? Why throw away good information the compiler can give you?

class search
{
private:
int[20],beg,mid,end,,i,j,n,item;

Anonymous array.

Extra comma.

Also: j isn't used at all.
public:
void input()
{
cout<<"\n how much element you want to enter";
cin>>n;
cout<<"\n enter the elements";
for(i=0;i<n;i++)
cin>>a;
cout<<"\n enter the element to be searched";
cin>>item;
beg=0;
end=n-1;
}


Indentation.

Use of global variables.

DO NOT EVER USE GLOBAL VARIABLES. It doesn't help to disguise them as
class member variables.

int binarysearch()
{
mid=(beg+end)/2;
while((beg<end)&&a[mid]!=item))
{
if (item>a[mid])
beg=mid+1;
mid=(beg+end)/2;


Also need to check for item < a[mid]. Otherwise, this could become an
infinite loop.
}
}
if (item==a[mid])
cout<<"item"<<item<<"is found at location"<<mid+1<<"position";
else
cout<<"item is not found";
}
int linearsearch()

.... snip ...

HTH

-NM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top