Arrays-- Bubble Sort & Binary Search

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* 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>
using namespace 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++;
			}
			else if (key <sortedArray[mid])
			{
				totalNumberOfItems =mid-1;
			}
			else
			{
				return mid;
			}
		}
		return -1;
		cout << food[counter] << " has " << calories [counter] << " calories." <<endl;
	}
}
I've done the buble sorting for you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* 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>
using namespace 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?

Here is what I currently have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* 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>
using namespace 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");

}
Topic archived. No new replies allowed.