473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
6 1658
* 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

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
Is this even C++?
You need to seriously read some good C++ tutorials.

Good luck.

Apr 17 '06 #4
"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
"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
3
by: fastwings | last post by:
mm the code //////makemenu.h//// class menu { public: int op; pmenu(int op,int sub = 0) { switch op {
16
by: Timothy Madden | last post by:
Hy I have destructors that do some functional work in the program flow. The problem is destructors should only be used for clean-up, because exceptions might rise at any time, and destructors...
29
by: Roy Gourgi | last post by:
Hi, I am new to C#. I have the same time scheduling program written in C++ and it is 5 times faster than my version in C#. Why is it so slow as I thought that C# was only a little slower than...
4
by: h974483 | last post by:
Can you tell me where to buy VB.net program/software? I am reading a abook and cannot continue without the program so that I can work on some of the exercises... Please tell me where to buy ...
20
by: Narf the Mouse | last post by:
....Without using NULL? There must be a way; people keep talking about storing pointers to objects in different locations in the program. Thanks.
15
by: E-Dot | last post by:
I am trying to write a program which asks the user to enter a number in the interval , the program then gives the natural logarithm of that number, using the series for log(x+1)... Here is what...
0
by: Christian Blackburn | last post by:
Hi Gang, Without running a program on a system without any version of .NET and seeing what error message it gives, is there an easy way to tell what version of .net a program was written in? ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.