Hello,
I have been working on this code for days now. I am supposed to collect data from the user about different foods and their calories, up to 100 entries. Then, the program is supposed to use a bubble sort to sort and display the entries. After that, the user is able to enter a food and a binary search will display the food and number of calories.
I am having a very difficult time with the bubble sort and binary search. I have spent a lot of time researching both of these but haven't been able to apply them properly to this program. Any help would be greatly appreciated!
Thanks,
Sarah
/* Write a program that allows the user to enter foods and their calories, up to 100 entries.
When the user enters "done" then stop asking for new entries. Prompt the user for
a food item and when entered, display the number of calories. If the item is not found,
say so.
*/
#include <iostream>
#include <string>
usingnamespace std;
int main()
{//Get information from user-- food type and calorie count.
string food[100];
string selection;
double calories[100];
int counter=-1;
bool done1=false;
bool done=false;
do
{
counter++;
cout<< "Enter a menu item (enter 'done' when finished): ";
getline(cin,food[counter]);
if (food[counter] == "done")
{
done1=true;
}
else
{
cout << "Enter the number of calories: ";
cin >> calories[counter];
cin.ignore();
}
} while (done1==false);
cout << "HERE IS THE SORTED DATA:" << endl;
// Do a bubble sort here.
int i, j;
string tmp;
{
for (i=0; i<counter; i++)
{
for (j=0; j<counter-i; j++)
if (food[j-1], food[j])
{
tmp=food[j];
food[j] = food[j+1];
food[j+1] = tmp;
}
cout << food[j] << " has " << calories[j] << " calories." <<endl;
}
}
cout << "NOW YOU CAN SEARCH FOR DATA:" <<endl;
//Use a binary search here.
{
cout <<"Enter a product to look up: ";
getline(cin,selection);
int first =0;
int totalNumberOfItems;
int binarySearch(string sortedArray[], int totalNumberOfItems, string key);
totalNumberOfItems--;
while(first<=totalNumberOfItems)
{
int mid= (first + totalNumberOfItems)/2;
if (key > sortedArray[mid])
{
mid++;
}
elseif (key <sortedArray[mid])
{
totalNumberOfItems =mid-1;
}
else
{
return mid;
}
}
return -1;
cout << food[counter] << " has " << calories [counter] << " calories." <<endl;
}
}
/* Write a program that allows the user to enter foods and their calories, up to 100 entries.
When the user enters "done" then stop asking for new entries. Prompt the user for
a food item and when entered, display the number of calories. If the item is not found,
say so.
*/
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
//Get information from user-- food type and calorie count.
string food[100];
string selection;
double calories[100];
int counter=-1;
bool done1=false;
bool done=false;
do
{
counter++;
cout<< "Enter a menu item (enter 'done' when finished): ";
getline(cin,food[counter]);
if (food[counter] == "done")
{
done1=true;
}
else
{
cout << "Enter the number of calories: ";
cin >> calories[counter];
cin.ignore();
}
} while (done1==false);
cout << "HERE IS THE SORTED DATA:" << endl;
// Do a bubble sort here.
int i, j;
string tmp;
{
for (i=0; i<counter; i++)
{
for (j=0; j<counter-i; j++)
if (food[j] > food[j+1]) //you condition correction
{
tmp=food[j];
food[j] = food[j+1];
food[j+1] = tmp;
//sort the calories too
swap(calories[j], calories[j+1]);
}
}
}
//print after sorting.. now while sorting..
for(i=0; i<counter; i++)
{
cout << food[i] << " has " << calories[i] << " calories." <<endl;
}
return 0;
}
Thanks for your help with this.
I now have the code mostly working. I only have a couple little issues. When the bubble sort displays its data, the first entry is not included. Do I need to change something in the "for" specifications to make this work?
Secondly, also in the bubble sort, "done" is included as a type of food. How can I make sure that "done" isn't displayed in this bubble sort?
/* Write a program that allows the user to enter foods and their calories, up to 100 entries.
When the user enters "done" then stop asking for new entries. Prompt the user for
a food item and when entered, display the number of calories. If the item is not found,
say so.
*/
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string food[100];
string selection;
int calories[100];
int counter=-1;
bool done1=false;
bool done=false;
do
{
counter++;
cout<< "Enter a menu item (enter 'done' when finished): ";
getline(cin,food[counter]);
if (food[counter] != "done")
{
cout << "Enter the number of calories: ";
cin >> calories[counter];
cin.ignore();
}
} while (food[counter] != "done");
cout << "HERE IS THE SORTED DATA:" << endl;
/*Output for this does not display the first entry and displays
the loop-terminating "done" as a food. */
int i, j;
string tmp;
{
for (i=0; i<counter; i++)
{
for (j=0; j<counter-i; j++)
if (food[j] > food[j+1])
{
tmp=food[j];
food[j] = food[j+1];
food[j+1] = tmp;
swap(calories[j], calories[j+1]);
}
}
}
for(i=0; i<counter; i++)
{
cout << food[i] << " has " << calories[i] << " calories." <<endl;
}
cout << "NOW YOU CAN SEARCH FOR DATA:" <<endl;
do
{
cout <<"Enter a product to look up: ";
getline(cin,selection);
bool found = false;
for (int y=0; y<counter; y++)
if (selection == food[y])
{
cout << food[y] << " has " << calories [y] << " calories." <<endl;
found = true;
break;
}
if ((!found) && (selection != "done"))
{
cout << selection << " was not found." << endl;
}
if (selection == "done")
break;
} while (selection != "done");
}