473,785 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

binary search on array based list

<code>
#include <iostream>
#include <fstream>

const int MAX_LENGTH = 10;

using namespace std;

class IntList
{
public:
IntList();
void GetList(ifstrea m &data);
void PrintList();
int LengthIs();
private:

int length;
int values[MAX_LENGTH];
};

bool BinarySearch(In tList [], int , int , int ,bool);

int main()
{
ifstream inData;
IntList info;
int item,fromLoc,to Loc,istep=0;
bool found = true;
char answer;

string msg = "Number out of range! ";
inData.open("in t.dat");
do{
cout << fixed << showpoint;
cout << "Enter value to be searched for: "<< endl;
cin >item;
cout << "Enter starting location in 0-9 range: "<< endl;
cin >fromLoc;
cout << "Enter end location in 0-9 range: "<< endl;
cin >toLoc;
try{
while(istep != MAX_LENGTH)
{
if((fromLoc <= 9) && (fromLoc >=0)&&(toLoc <= 9) &&
(toLoc >=0) && (fromLoc<toLoc) )
{ cout << "Correct range! Do the search." <<endl;
BinarySearch(in fo,item,fromLoc ,toLoc,found);
istep++;
cout << "This is the " << istep <<"th iteration,
do you want to continue? <Y/N";
cin >answer;
if(answer == 'n')
{ cout << "Search abrted!";
break;
}
}
else
throw msg;

}
}
catch(string message)
{
cout << message << "Search aborted!";

}
break;

}while(found = false);

cout << endl;
cout << "************** *************** *************** ***********"
<< endl;

info.GetList(in Data);
cout << "The list is : ";
info.PrintList( );
cout << endl;
cout << "Length is " << info.LengthIs() << endl;
cout << "************** *************** *************** ***********"
<< endl;
system("pause") ;
return 0;

}

IntList::IntLis t()
{
length =0;
}

void IntList::GetLis t(ifstream &data)
{
int value;

data >value;

while(data)
{
for(int i=0;i<MAX_LENGT H;i++)
{
data >value;
values[i] = value;
length++;
}
cout << "List has been populated." << endl;
}

}

void IntList::PrintL ist()
{
int index;
for(index=0;ind ex<MAX_LENGTH;i ndex++)
{
cout << values[index]<< " ";
}
}

int IntList::Length Is()
{
return length;
}

bool BinarySearch(In tList info[], int item, int first, int last,bool
found)
{
int mid;
found = false;
int num = 1;
while(first <= last)
{
mid = (first + last) / 2;
if(item info[mid])
first = mid +1;
else if(item < info[mid])
last = mid - 1;
else

return found = true;

}
return found = false;
}

</code>

I am passing an array based list to a function to perform a binary
search for an item being inputted. I get these compilation errors:

50 D:\ cannot convert `IntList' to `IntList*' for argument `1' to
`bool BinarySearch(In tList*, int, int, int, bool)'
D:\ In function `bool BinarySearch(In tList*, int, int, int, bool)':
137 D:\
no match for 'operator>' in 'item *((+(((unsigned int)mid) * 44u)) +
info)'

Apr 2 '07 #1
6 2710
zf*****@mail.co m wrote:
<code>
[..]
class IntList
{ [..]
};

bool BinarySearch(In tList [], int , int , int ,bool);
So, the first argument is an array of IntList? Or is it a pointer
to IntList? What is it? I believe you actually wanted to pass
your IntList by _reference_, but somehow got confused by all the
"array based" smokescreen. No matter. Your declaration here is

bool BinarySearch(In tList*, int, int, int, bool);

so the first argument is a pointer.
>
int main()
{
ifstream inData;
IntList info;
'info' is an _object_ of type 'IntList'.
[..]
BinarySearch(in fo,item,fromLoc ,toLoc,found);
And here you're passing your object when the function actually
expects a pointer. You probably want to do

BinarySearch( & info , ...
[..]

</code>

I am passing an array based list to a function to perform a binary
search for an item being inputted. I get these compilation errors:

50 D:\ cannot convert `IntList' to `IntList*' for argument `1' to
`bool BinarySearch(In tList*, int, int, int, bool)'
Right. See above for the correction.
D:\ In function `bool BinarySearch(In tList*, int, int, int, bool)':
137 D:\
no match for 'operator>' in 'item *((+(((unsigned int)mid) * 44u)) +
info)'
I have no idea what that is. Which like was 137? I am kinda lazy
to count to 137.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 2 '07 #2
Thank you for your response. I made the changes but I still dont
understand the called function and the errors:
Do I have to declare a pointer or just pass it to the function?
<code>
bool BinarySearch(In tList &info, int item, int first, int last,bool
found)
{
int mid;
found = false;
int num = 1;
while(first <= last)
{
mid = (first + last) / 2;
if(item info[mid]) //line producing error
first = mid +1;
else if(item < info[mid])
last = mid - 1;
else

return found = true;
}
return found = false;
}
bool BinarySearch(In tList *, int , int , int ,bool);//prototype
Please help
BinarySearch(&i nfo,item,fromLo c,toLoc,found); // function being called
in main
</code>

137
no match for 'operator[]' in 'info[mid]'

Apr 2 '07 #3
zf*****@mail.co m wrote:
Thank you for your response. I made the changes but I still dont
understand the called function and the errors:
Do I have to declare a pointer or just pass it to the function?
<code>
bool BinarySearch(In tList &info, int item, int first, int last,bool
<sigh So, you changed the first argument to be a reference?
found)
{
int mid;
found = false;
int num = 1;
while(first <= last)
{
mid = (first + last) / 2;
if(item info[mid]) //line producing error
first = mid +1;
else if(item < info[mid])
last = mid - 1;
else

return found = true;
}
return found = false;
}
bool BinarySearch(In tList *, int , int , int ,bool);//prototype
Your prototype does not match the definition.
>

Please help
BinarySearch(&i nfo,item,fromLo c,toLoc,found); // function being called
in main
</code>

137
no match for 'operator[]' in 'info[mid]'
Well, since you haven't defined (overloaded) the operator[] for the
'IntList' class, what do you want? You can't use 'info[mid]' unless
you define the operator[] in 'IntList'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 2 '07 #4
zf*****@mail.co m wrote:
class IntList
{
public:
IntList();
void GetList(ifstrea m &data);
void PrintList();
int LengthIs();
There is no method to read the i'th list member. You need

int get( int pos ) const;
or
int operator[] ( int pos ) const;
bool BinarySearch(In tList info[], int item, int first, int last,bool
found)
{
[snipped]
if(item info[mid])
D:\ In function `bool BinarySearch(In tList*, int, int, int, bool)':
137 D:\
no match for 'operator>' in 'item *((+(((unsigned int)mid) * 44u)) +
info)'
1. info[mid] has type IntList; that's probably not what you intended,
but it's legal code.

2. You try to compare an int with an IntList; the compiler has many
operator () available, but none of them matches ( int, IntList ).
That's what the compiler is trying to tell you.

3. Unfortunately, the message prints a simple "a[i]" as something like
"*( i*sizeof(a) ) + a". That's a very special, internal notation.
A better message would have been:
no match for 'operator>( int, IntList )' in 'item info[mid]'
Maybe it's time to upgrade your compiler.

More comments on your function BinarySearch():

- it needs a single IntList (no array of), and doesn't modify it. So
just write "IntList const &info" (or "const IntList &info", which is the
same). Then you can refer to its elements via "info.get(m id)" or
"info[mid]", or whatever you defined in class IntList.

- The arguments `item' and `found' are passed by value. Your assignments
have no effects to the caller.
Apr 2 '07 #5
I'm sorry guys; could anyone spell this out to me like I'm a ten year
old. I dont understand operator overloading and when you say reading
the list, do I just use a for loop to read the list and then implement
the comparisons?
<code>
#include <iostream>
#include <fstream>

const int MAX_LENGTH = 10;

using namespace std;

class IntList
{
public:
IntList();
void GetList(ifstrea m &data);
void PrintList();
int LengthIs();
int operator[](int mid);
private:

int length;
int values[MAX_LENGTH];
};

bool BinarySearch(In tList *, int , int , int ,bool);

int main()
{
ifstream inData;
IntList values;
int item,fromLoc,to Loc,istep=0;
bool found = true;
char answer;

string msg = "Number out of range! ";
inData.open("in t.dat");
do{
cout << fixed << showpoint;
cout << "Enter value to be searched for: "<< endl;
cin >item;
cout << "Enter starting location in 0-9 range: "<< endl;
cin >fromLoc;
cout << "Enter end location in 0-9 range: "<< endl;
cin >toLoc;
try{
while(istep != MAX_LENGTH)
{
if((fromLoc <= 9) && (fromLoc >=0)&&(toLoc <= 9) &&
(toLoc >=0) && (fromLoc<toLoc) )
{ cout << "Correct range! Do the search." <<endl;
BinarySearch(va lues,item,fromL oc,toLoc,found) ;
istep++;
cout << "This is the " << istep <<"th iteration,
do you want to continue? <Y/N";
cin >answer;
if(answer == 'n')
{ cout << "Search abrted!";
cout << "There were " << istep << "
iterations.";
break;
}
}
else
throw msg;

}
}
catch(string message)
{
cout << message << "Search aborted!";

}
break;

}while(found = false);

cout << endl;
cout << "************** *************** *************** ***********"
<< endl;

values.GetList( inData);
cout << "The list is : ";
values.PrintLis t();
cout << endl;
cout << "Length is " << values.LengthIs () << endl;
cout << "************** *************** *************** ***********"
<< endl;
system("pause") ;
return 0;

}

IntList::IntLis t()
{
length =0;
}

void IntList::GetLis t(ifstream &data)
{
int value;

data >value;

while(data)
{
for(int i=0;i<MAX_LENGT H;i++)
{
data >value;
values[i] = value;
length++;
}
cout << "List has been populated." << endl;
}

}

void IntList::PrintL ist()
{
int index;
for(index=0;ind ex<MAX_LENGTH;i ndex++)
{
cout << values[index]<< " ";
}
}

int IntList::Length Is()
{
return length;
}
bool BinarySearch(co nst IntList &values, int item, int first, int
last,bool found)
{

found = false;
int num = 1;
while(first <= last)
{
int mid = (first + last) / 2;

if(item values[mid])
first = mid +1;
else if(item < values[mid])
last = mid - 1;
else

return found = true;
}

return found = false;
}

</code>

139 C: passing `const IntList' as `this' argument of `int
IntList::operat or[](int)' discards qualifiers
Apr 2 '07 #6
zf*****@mail.co m wrote:
I'm sorry guys; could anyone spell this out to me like I'm a ten year
old.
Ok, after I've started, I should continue...
(But you will find more help in alt.comp.lang.l earn.c-c++)
class IntList
{
public:
IntList();
void GetList(ifstrea m &data);
void PrintList() const ;
int LengthIs() const ;
// int operator[](int mid) const ;
// For the beginning, I'd recommend a normal function
int get(int mid) const ;
I've added the `const' keyword to some functions, because they do not
modify the object. The advantage is, that you can invoke these methods
even for a const IntList.
bool BinarySearch(In tList *, int , int , int ,bool);
You forgot to change the type of the first argument to `const IntList
&', as you did in the definition below.

Don't forget the `const' keyword in the definitions:
void IntList::PrintL ist() const
Also, you must define IntList::get():

int IntList::get(in t i) const
{
return values[i];
}

bool BinarySearch(co nst IntList &values, int item, int first, int
last,bool found)
{
[...]
//later! if(item values[mid])
if (item values.get(mid) )
139 C: passing `const IntList' as `this' argument of `int
IntList::operat or[](int)' discards qualifiers
The message is clear:
- values is declared `const'
- operator[]() -- which I've replaced with get() -- was not defined for
const IntList objects.
With the modifications above, it should be fine now.
But again: BinarySearch() modifies only a local copy of `found'. Its
value in main() will not change!
Keep on!
Apr 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3653
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
3
2853
by: tsunami | last post by:
hi all; I have an array and want to insert all the elements from this array to a binary search tree.That array is an object of the class of a stringtype which includes overloaded "< > = ==" functions and every other thing it needs. void InsertElementFromArray(StringType Array,int first element,int last element);
8
2366
by: Gordon Knote | last post by:
Hi can anyone tell me what's the best way to search in binary content? Best if someone could post or link me to some source code (in C/C++). The search should be as fast as possible and it would be great if the engine (or so) would accept multiple parameters (like a search offset, a max number of bytes to search in etc.) Any ideas Thanks a lot again Gordon
1
7122
by: cyrvb | last post by:
Hello, I'm a very very newbie in C# I did start 2 days ago, I get Visual Stuido 2005 C# I try to understand how to manage the arrays I did write this
10
9783
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
11
2427
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the following. I noticed that if I set: int upper = strings.GetUpperBound(0); I never match the last element in the array (i.e. "iii")
2
2542
by: Timmy | last post by:
The bigger problem is with the Binary Search. The program crashes when it's excuted. and Visual Studio 2005 indicates stack over flow and shows a break at that function. Sequential search is working, but I am trying to have it display the number of comparisons it took to find the number. It keeps displaying "2." Thank you folks kindly,
2
2700
by: jhansi | last post by:
create a binary file and insert the records and query on those inserted record.. queries are: query based on search by name,search by age and search by designation. the problem is i have created a binary file but it is not storing records permanently. i am using binary file for the first time. plz help me to know how to store records permanently . sending my code #include<stdio.h> #include<conio.h> #include<string.h> struct record
0
6982
by: Atos | last post by:
Binary search is used to locate a value in a sorted list of values. It selects the middle element in the array of sorted values, and compares it with the target value; that is the key we are searhing for in the array. If it islower than the target value then the search is made after that middle element and till the end of the array. If it is equal then the target value is found and the middle is returned. Otherwise, search is made from the...
0
9645
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
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
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
10091
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
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6739
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();...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.