assignment30 - int upperBound = n; while (lowerBound <=...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <string> using namespace std; int main() { string foods[100]; int calories[100]; string searchTerm; int x = -1; bool found = false; do { x++; cout << "Enter a menu item (enter 'done' when finished): "; getline(cin, foods[x]); if (foods[x] != "done") { cout << "Enter the number of calories: "; cin >> calories[x]; cin.ignore(); } } while (foods[x] != "done"); do { cout<<"Enter a product to look up:"<<endl; getline(cin, searchTerm); for(int i = 0; i < x; i++) { if (searchTerm == foods[i]) { cout << foods[i] << " has " << calories[i] << " calories." << endl; found = true; } } if( !found ) cout << searchTerm <<" was not found."<< endl; } while (searchTerm != "done"); } void bubbleSortRange(int x[], int n) { int lowerBound = 0;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int upperBound = n; while (lowerBound <= upperBound) { int firstExchange = n; int lastExchange = -1; for (int i=lowerBound; i<upperBound; i++) { if (x[i] > x[i+1]) { int temp = x[i]; x[i] = x[i+1]; x[i+1] = temp; if (i<firstExchange) { firstExchange = i; } lastExchange = i; } } lowerBound = firstExchange-1; if (lowerBound < 0) { lowerBound = 0; } upperBound = lastExchange; } } int BinarySearch(int A, int value, int low, int high) { if (high < low) { return -1; // not found } int mid = (low + high) / 2; if (A[mid] > value) { return BinarySearch(A, value, low, mid-1); } else if (A[mid] < value) { return BinarySearch(A, value, mid+1, high); } else { return 1; // found } }...
View Full Document

This note was uploaded on 01/11/2010 for the course CMPSC 101 taught by Professor Drobish,renee during the Summer '07 term at Pennsylvania State University, University Park.

Page1 / 3

assignment30 - int upperBound = n; while (lowerBound <=...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online