By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,816 Members | 2,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,816 IT Pros & Developers. It's quick & easy.

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

P: n/a
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[i];
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[i]==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();
}

Apr 17 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* 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>
Better use <cstdlib> than <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[i];
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[i]==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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 17 '06 #2

P: n/a

ashishnh33 wrote:
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
<header_file.h>. I am not sure what was the end result. So lets let the
experts throw more sunlight onto it.
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.
{ <snip>
}; 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;
}


Apr 17 '06 #3

P: n/a
Is this even C++?
You need to seriously read some good C++ tutorials.

Good luck.

Apr 17 '06 #4

P: n/a
"ashishnh33" writes:
[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[i];
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[i]==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();
}
Apr 17 '06 #5

P: n/a
"ashishnh33" wrotes:
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.
Apr 17 '06 #6

P: n/a

"Alf P. Steinbach" <al***@start.no> wrote in message
news:4a************@individual.net...
* 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>


Better use <cstdlib> than <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[i];
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

Apr 18 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.