468,134 Members | 1,268 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,134 developers. It's quick & easy.

Searching within an abstract data type (ADT)

I have an ADT and I'd like to search for certain items in it. How can I
accomplish this?
Oct 10 '05 #1
9 2341
deanfamily11 wrote:
I have an ADT and I'd like to search for certain items in it. How can I
accomplish this?


Be more specific. Does the object have a container you need to search
through? Or are you talking about using reflection to find/invoke a
particular function? Or are you trying to use assumptions about the
object layout to find certain data members?

Jacques.
Oct 10 '05 #2
Ok, here's an example and please note this is supposed to be an array of
class objects or variables, also note that >> indicates that those are
variable of that item in the array:
book[1]
title
author
price book[2]title
author
price
book[3]
....

In this case, I'd like to be able to search through all of the items for a
given title supplied by the user.

"Jacques Labuschagne" <ja*****@clawshrimp.com> wrote in message
news:Mt*********************@news.xtra.co.nz... deanfamily11 wrote:
I have an ADT and I'd like to search for certain items in it. How can I
accomplish this?


Be more specific. Does the object have a container you need to search
through? Or are you talking about using reflection to find/invoke a
particular function? Or are you trying to use assumptions about the object
layout to find certain data members?

Jacques.

Oct 10 '05 #3
deanfamily11 wrote:
Ok, here's an example and please note this is supposed to be an array of
class objects or variables, also note that >> indicates that those are
variable of that item in the array:
book[1]
title
author
price
book[2]
title
author
price


book[3]
...

In this case, I'd like to be able to search through all of the items for a
given title supplied by the user.


OK, sounds simple so far. So what's the public interface of the Book
object? Something like this?

class Book{
private:
...
public:
const string& getTitle() const;
...
};

If so, then the solution is to step through the array and check the
value of Book::getTitle() for every element:

for (int i = 0; i < array_size; ++i){
if (array[i].getTitle() == user_title){
cout << "Found it!\n";
break;
}
}

Is there something wrong with this solution?

Jacques.
Oct 10 '05 #4
Your suggestion I think works, but I need to know how to define getBook in
the area to define the class.

"Jacques Labuschagne" <ja*****@clawshrimp.com> wrote in message
news:BP*********************@news.xtra.co.nz...
deanfamily11 wrote:
Ok, here's an example and please note this is supposed to be an array of
class objects or variables, also note that >> indicates that those are
variable of that item in the array:
book[1]
title
author
price


book[2]
title
author
price


book[3]
...

In this case, I'd like to be able to search through all of the items for
a given title supplied by the user.


OK, sounds simple so far. So what's the public interface of the Book
object? Something like this?

class Book{
private:
...
public:
const string& getTitle() const;
...
};

If so, then the solution is to step through the array and check the value
of Book::getTitle() for every element:

for (int i = 0; i < array_size; ++i){
if (array[i].getTitle() == user_title){
cout << "Found it!\n";
break;
}
}

Is there something wrong with this solution?

Jacques.

Oct 10 '05 #5
deanfamily wrote:
Your suggestion I think works, but I need to know how to define getBook in
the area to define the class.


Why don't you post the code of your ADT so we have some context?

Jacques.
Oct 10 '05 #6
Ok, here it is for your reference, and also I need to do the search in ISBN
number:

class bookType
{
public:
//this part is for the book title
string printTitle() const;
void setTitle(string);
void checkTitle(string);

//this part is for the number of copies
int showNumCopies() const;
void setCopies(int);
void updateCopies(int);
int printNumCopies() const;

//this part is for the publisher
string showPublisher() const;
void setPublisher(string);
void updatePublisher(string);
string printPublisher() const;

//this part is for the ISBN
string showISBN() const;
void setISBN(string);
void updateISBN(string);
string printISBN() const;

//this part is for the price
double showPrice() const;
void setPrice(double);
void updatePrice(double);
double printPrice() const;

//this part is for the authors
string showAuthors() const;
void setAuthors(string, int);
void updateAuthors(string, int);
string printAuthors() const;

private:
string title;
string publisher;
string isbn;
double price;
int numInStock;
string authors[4];
};

string bookType::printTitle() const
{
return title;
}

void bookType::setTitle(string titleName)
{
title = titleName;
}

void bookType::checkTitle(string newTitle)
{
if(newTitle == title)
cout << "That title already exists." << endl;
}

int bookType::showNumCopies() const
{
return numInStock;
}

void bookType::setCopies(int copiesIn)
{
numInStock = copiesIn;
}

void bookType::updateCopies(int newNum)
{
numInStock = newNum;
}

int bookType::printNumCopies() const
{
return numInStock;
}

string bookType::showPublisher() const
{
return publisher;
}

void bookType::setPublisher(string publish)
{
publisher = publish;
}

void bookType::updatePublisher(string newPub)
{
publisher = newPub;
}

string bookType::printPublisher() const
{
return publisher;
}

string bookType::showISBN() const
{
return isbn;
}

void bookType::setISBN(string isbnIn)
{
isbn = isbnIn;
}

void bookType::updateISBN(string newIsbn)
{
isbn = newIsbn;
}

string bookType::printISBN() const
{
return isbn;
}

double bookType::showPrice() const
{
return price;
}

void bookType::setPrice(double priceIn)
{
price = priceIn;
}

void bookType::updatePrice(double newPrice)
{
price = newPrice;
}

double bookType::printPrice() const
{
return price;
}

string bookType::showAuthors() const
{
return authors[0];
return authors[1];
return authors[2];
return authors[3];
}

void bookType::setAuthors(string authorIn, int authNum)
{
authors[authNum] = authorIn;
}

void bookType::updateAuthors(string setAuthor, int authNum)
{
authors[authNum] = setAuthor;
}

string bookType::printAuthors() const
{
return authors[0];
return authors[1];
return authors[2];
return authors[3];
}
"Jacques Labuschagne" <ja*****@clawshrimp.com> wrote in message
news:OG*********************@news.xtra.co.nz...
deanfamily wrote:
Your suggestion I think works, but I need to know how to define getBook
in the area to define the class.


Why don't you post the code of your ADT so we have some context?

Jacques.

Oct 10 '05 #7
deanfamily wrote:
Ok, here it is for your reference, and also I need to do the search in ISBN
number:
OK that seems easy enough. See below

class bookType
{
public:
//this part is for the book title
string printTitle() const;
See the function you have called printTitle? It doesn't print anything
so it is badly named. In fact it is almost exactly the function getTitle
that Jacques imagined.

You could rewrite Jacques code like following

for (int i = 0; i < array_size; ++i){
if (array[i].printTitle() == user_title){
cout << "Found it!\n";
break;
}
}

No do you see how sily the name printTitle is? The above code works but
the name is confusing. So use Jacques' solution but rename the function
to getTitle, you will have to rename it everywhere that you are using it.

You have similar confusion all over the place. Look at this
void setCopies(int);
void updateCopies(int);
void bookType::setCopies(int copiesIn)
{
numInStock = copiesIn;
}

void bookType::updateCopies(int newNum)
{
numInStock = newNum;
}
The functions setCopies and updateCopies are *exactly* the same. Why did
you feel the need to have two functions. Just get rid of one of them.

The same problems with many others, they are either duplicates or have
inappropriate names.

string bookType::printAuthors() const
{
return authors[0];
return authors[1];
return authors[2];
return authors[3];
}


This just doesn't work, it will only return the first author (and again
it won't print anything) the three following return statements will be
ignored.

When I see these sort of naming problem I know the programmer is
confused. You shoul name a function after what it does, not how you use
it. getTitle because you the function get the title from the book, not
printTitle because you happed to use the function to print then title.

Assuming this is homework you will definitely lose marks for
inappropriate names for your functions, and also for having duplicate
functions.

john
Oct 10 '05 #8
The problem that I am mainly having with the code:
for (int i = 0; i < array_size; ++i){
if (array[i].printTitle() == user_title){
cout << "Found it!\n";
break;
is that == is for integers and it won't compile as a result. The
"user_title" and "printTitle" parts are strings. How do you change the code
so that it can be used for strings?

"John Harrison" <jo*************@hotmail.com> wrote in message
news:wf*****************@newsfe6-win.ntli.net... deanfamily wrote:
Ok, here it is for your reference, and also I need to do the search in
ISBN number:


OK that seems easy enough. See below

class bookType
{
public:
//this part is for the book title
string printTitle() const;


See the function you have called printTitle? It doesn't print anything so
it is badly named. In fact it is almost exactly the function getTitle that
Jacques imagined.

You could rewrite Jacques code like following

for (int i = 0; i < array_size; ++i){
if (array[i].printTitle() == user_title){
cout << "Found it!\n";
break;
}
}

No do you see how sily the name printTitle is? The above code works but
the name is confusing. So use Jacques' solution but rename the function to
getTitle, you will have to rename it everywhere that you are using it.

You have similar confusion all over the place. Look at this
void setCopies(int);
void updateCopies(int);


void bookType::setCopies(int copiesIn)
{
numInStock = copiesIn;
}

void bookType::updateCopies(int newNum)
{
numInStock = newNum;
}


The functions setCopies and updateCopies are *exactly* the same. Why did
you feel the need to have two functions. Just get rid of one of them.

The same problems with many others, they are either duplicates or have
inappropriate names.

string bookType::printAuthors() const
{
return authors[0];
return authors[1];
return authors[2];
return authors[3];
}


This just doesn't work, it will only return the first author (and again it
won't print anything) the three following return statements will be
ignored.

When I see these sort of naming problem I know the programmer is confused.
You shoul name a function after what it does, not how you use it. getTitle
because you the function get the title from the book, not printTitle
because you happed to use the function to print then title.

Assuming this is homework you will definitely lose marks for inappropriate
names for your functions, and also for having duplicate functions.

john

Oct 11 '05 #9
* deanfamily:
[top- and middle-posting]


Please stop top-posting and middle-posting in this group.

Thanks.

- Alf
PS: Please also try the suggestions you've been given before protesting that
they won't work.

--
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?
Oct 11 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by SunScreen | last post: by
7 posts views Thread by jason | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.