473,698 Members | 2,943 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am.h>
#include<conio. h>
#include<stdlib .h>
int binarysearch(vo id)
int linearsearch(vo id)
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"<<i tem<<"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"<<i tem<<"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.linearsearc h();
break;
case 2:
obj.input();
obj.binarysearc h();
break;
case 3:
exit(1);
}
}
while(choice!=3 );
getch();
}

Apr 17 '06 #1
6 1672
* 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<iostre am.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(vo id)
int linearsearch(vo id)
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"<<i tem<<"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"<<i tem<<"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.linearsearc h();
break;
case 2:
obj.input();
obj.binarysearc h();
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<iostre am.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(vo id)
int linearsearch(vo id)
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.linearsearc h();
break;
case 2:
obj.input();
obj.binarysearc h();
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<iostre am>
#include<conio. h>
#include<cstdli b>

using namespace std;
//int binarysearch(vo id);
// the *first* thing you should have done is provide that missing semicolon
//int linearsearch(vo id);
/* 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::binarys earch()
{
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"<<i tem<<"is found at location"<<mid+ 1<<"position";
else
cout<<"item is not found";
}
#endif
//............... .........
int Search::linears earch()
{
int flag;
for(i=0;i<n;i++ )
{
if(a[i]==item)
{
flag=1;
cout<<"item"<<i tem<<"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.linearsearc h();
break;
case 2:
obj.input();
//obj.binarysearc h();
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<iostre am.h>
#include<conio. h>
#include<stdlib .h>
int binarysearch(vo id)
int linearsearch(vo id)
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<iostre am.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(vo id)
int linearsearch(vo id)


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"<<i tem<<"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
17640
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. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
3
2963
by: fastwings | last post by:
mm the code //////makemenu.h//// class menu { public: int op; pmenu(int op,int sub = 0) { switch op {
16
1856
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 will be called for clean-up only. So how can I tell, from within the destructor, if the call has been made as part of normal flow of control and the destructor can play its functional role, or if the call has been made as a result of an...
29
2257
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 C++? What am I doing wrong? Here is my code: using System;
4
1268
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 Thanks
20
2136
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
2681
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 I have so far and can't figure out what i'm doing wrong. any help would be greatly appreciated, thanks guys... #include <stdio.h> #include <math.h>
0
1205
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? Thanks, Christian Blackburn
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.