Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C++ Bubble Sort/Binary Search Help

    side note: admin, for some reason it posted it in html at first... so feel free to delete it if you want.

    So for my last assignment i have to create a program that allows the user to input 100 foods, enter calorie amount and then using bubble sort and binary search it has to be able to display them back to the user. I have all of this done except the food does not match up with the calories.


    any help is greatly appreciated!!

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
     string foods [100];
     string product;
     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,foods [counter]);
      if (foods [counter] == "done"){
    	  done1 = true;
      }
      else{
    	  cout << "Enter the number of calories: ";
    	  cin >> calories [counter];
    	  cin.ignore();
      }
     } while (done1 == false);
    int i;
    int j;
    string tmp;
    //bubble sort
    for (i=0; i<counter; i++) {
    	for (j=0; j<counter-i; j++)
    		if (foods[j+1]< foods[j]) {
    			tmp = foods[j];
    			foods[j] = foods[j+1];
    			calories[j] = calories[j+1];
    			foods[j+1] = tmp;
    		}
    		cout << foods[j] << " " << endl;
    }
    //end bubble sort, start binary search
    while (done == false){
    	int upperbound = counter;
    	 cout << "Enter a product to look up: ";
    	 getline(cin,product);
    	 if (product == "done"){
    		 done = true;
    		 break;
    	 }
    	 int lowerbound = 0;
    	 int position;
    	 position = ( lowerbound + upperbound) / 2;
    	 while((foods[position] != product) && (lowerbound <= upperbound))
    	 {
    		if (foods[position] > product)
    		{
    			upperbound = position - 1;
    		} 
    		else
    		{
    			lowerbound = position + 1;
    		}
    		position = (lowerbound + upperbound) / 2;
    	 }
    	 if (foods[position] == product){
    			 cout << foods[position] << " has " << calories[position] << " calories." << endl;
    			 cout << foods[1] << calories[1] << position;
    		 }
    	 if (lowerbound > upperbound)
    		{
                cout << product << " was not found." << endl;
    	 }}
    
    }

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Look at your bubble sort. You're only sorting the foods array.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •