-
December 7th, 2008, 01:58 AM
#1
Please check Bubble Sort and Binary Search
I was required to insert a bubble sort and binary search into my program. Here is the code I added:
Code:
# include <iostream>
# include <string>
using namespace std;
int main()
{
string menuitem[100];
int counter=-1, calories[100];
do
{
counter++;
//asking for information to put into the menuitem array
cout<<"Enter a menu item (enter 'done' when finished): ";
getline (cin,menuitem[counter]);
if (menuitem[counter]!="done")
{
//asking for information to put into the corresponding calories array
cout<<"Enter the number of calories: ";
cin>>calories[counter];
cin.ignore();
}
}
while (menuitem[counter] !="done");
//bubble sort.......this sort puts all of my menuitems in alphabetical order
for (int a=0; a<counter;a++)
for(int b=0; b<counter-1;b++)
if (menuitem[b]>menuitem[a])
{
swap(menuitem[b],menuitem[a]);
//if the menuitem is swapped then the corresponding calories are swapped as well
swap(calories[b],calories[a]);
}
if(counter!=0)
{
string ans;
do
{
//Asking for what menuitem you want to search for
cout<<"Enter a product to look up: ";
getline (cin,ans);
if(ans!="done")
{
bool found=false;
int high=counter-1, low=0, med,cycles=0;
do
{
//binary search code.....until the first "while" loop.
med=(high+low)/2;
if( ans > menuitem[med])
{
low = med + 1;
}
else if(ans < menuitem[med])
{
high = med - 1;
}
else
{
//if "ans" was found then it sets the found bool to true, couts, and breaks
found=true;
cout<<menuitem[med]<<" has "<<calories[med]<<" calories."<<endl;
break;
}
}
while (low<=high);
//uses the bool to determine if the "ans" was not found
if (!found)
{
cout<<ans<<" was not found."<<endl;
}
}
}
while (ans!="done");
}
}
Does it look like I did the binary search and the bubble sort correctly? Any advice would be appreciated. I am new at programming, so any advice would be appreciated. Thanks
Last edited by syntaxerror; December 7th, 2008 at 04:11 PM.
Reason: Add Code Tags
-
December 7th, 2008, 02:15 PM
#2
Re: Bubble Sort and Binary Search check
Please, edit your post adding Code tags around the code snippets (see Announcement: Before you post....), otherwise your code is unreadable.
Victor Nijegorodov
-
December 7th, 2008, 03:56 PM
#3
Re: Bubble Sort and Binary Search check
-
December 7th, 2008, 04:19 PM
#4
Re: Bubble Sort and Binary Search check
Get out of the habit of "inline programming" right from the beginning.
Organize your design into proper objects and methods.
"Co-Syinchronized" arrays are ALWAYS a bad idea.
Start with:
Code:
class Item
{
public:
Item(string const &name, double calories) {...}
string Name() const {}
double Calories() const {}
};
One you have this Implemented, you create:
Code:
void Sort(Item[] items, int itemCount) {}
and
Item const &Search(Item[] const & items, int itemCount, string name) {}
Then your main() becomes implemented in terms of very simple calls.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|