Quantcast

Jump to content


Photo

Bubble sort and binary search for C++


  • Please log in to reply
8 replies to this topic

#1 Supernature

Supernature
  • 87 posts

Posted 03 August 2010 - 01:13 PM

Back again.. class is almost over. This is our last assignment before the final, and I have no idea how to do this >__>



Anyone? I can't wait til this class is over >____>

Here's my code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string item[100];
	string search;
	int calories[100];
	int i = -1;

	do
	{
		i++;
		cout << "Enter a menu item (enter 'done' when finished): ";
		getline(cin,item[i]);
		if (item[i] != "done")
	{
		cout << "Enter the number of calories: ";
		cin >> calories[i];
		cin.ignore();
		}
	}while (item[i] != "done");
	
	cout << "HERE IS THE SORTED DATA:" << endl;
		// display all inputs here, sorted using a bubble sort.

	cout << "NOW YOU CAN SEARCH FOR DATA:" << endl;
	
	}

Edited by Supernature, 03 August 2010 - 02:42 PM.


#2 Junsu

Junsu
  • 1566 posts

Posted 03 August 2010 - 01:34 PM

Here I am again.

I dont get how its sorted.

bbb bbb bbb should be greater than ccc ccc?



Edit:

And you should get code for a bubble sort and a binary sort cuz its a pain in the ass to type up.
You shouldn't need to memorize the code for either, but you have to know how they work

Edited by Junsu, 03 August 2010 - 01:36 PM.


#3 Supernature

Supernature
  • 87 posts

Posted 03 August 2010 - 01:35 PM

What do you mean?

Oh, it's sorting it alphabetically, not by calories.

Anybody who does this for me will get something sweet on Neo. :)

And you should get code for a bubble sort and a binary sort cuz its a pain in the ass to type up.
You shouldn't need to memorize the code for either, but you have to know how they work


I'm kind of to the point where I don't care. I just want a C to pass this class
(:

Edited by Supernature, 03 August 2010 - 01:38 PM.


#4 Junsu

Junsu
  • 1566 posts

Posted 03 August 2010 - 01:43 PM

Are you good with java?
I'll do your C++ if you do my java lol :D

#5 Supernature

Supernature
  • 87 posts

Posted 03 August 2010 - 01:44 PM

I'm only good at web design programming languages, lol. I can't program for shit. I have to take Java in the Spring..

#6 Junsu

Junsu
  • 1566 posts

Posted 03 August 2010 - 01:50 PM

Good luck O___O

Cant help you this time cuz I dont have time :p



#7 Supernature

Supernature
  • 87 posts

Posted 03 August 2010 - 01:53 PM

Heh, thanks anyways :p

#8 aweroij

aweroij
  • 44 posts

Posted 03 August 2010 - 02:03 PM

#include <iostream>

#include <string>



using namespace std;



int main()

{

	string item[100];

	string search;

	int calories[100];

	int i = -1;



	do

	{

		i++;

		cout << "Enter a menu item (enter 'done' when finished): ";

		getline(cin,item[i]);

		if (item[i] != "done")

		{

			cout << "Enter the number of calories: ";

			cin >> calories[i];

			cin.ignore();

		}

	} while (item[i] != "done");



	bool sorted;

	do {

		sorted = true;



		for (int j = 0; j < i - 1; j++) {

			if (item[j] > item[j+1]) {

				string tempStr = item[j];

				int tempCal = calories[j];

				item[j] = item[j+1];

				item[j+1] = tempStr;

				calories[j] = calories[j+1];

				calories[j+1] = tempCal;

				sorted = false;

			}

		}

	} while (!sorted);



	cout << "HERE IS THE SORTED DATA:" << endl;

	for (int j = 0; j < i; j++)

		cout << item[j] << " has " << calories[j] << " calories." << endl;



	cout << "NOW YOU CAN SEARCH FOR DATA:" << endl;

	do {

		cout << "Enter a product to look up: ";

		getline(cin, search);

	if (search == "done") break;



		int low = 0;

		int high = i;

		while (low < high) {

			int mid = low + ((high - low) / 2);

			if (item[mid] < search)

				low = mid + 1;

			else

				high = mid;

		}



		if ((low < i) && (item[low] == search))

			cout << item[low] << " has " << calories[low] << " calories." << endl;

		else

			cout << search << " was not found." << endl;

	} while (true);



}


#9 Supernature

Supernature
  • 87 posts

Posted 03 August 2010 - 02:31 PM

Woo thank yah! PMing you :)


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users