i have to write a program in c++ that reads numbers(weight of an item) from a file until now i got it:)
and then place this items in boxes.the goal is to use as less boxes as possible.Each box can weight no more than 10.
place each new item in the most-filled box.
use a priority queue to determine the box that an items goes in.


pls someone help me
thanks!!!!

Recommended Answers

All 7 Replies

Post what code you have so far and I'll see if I can help out.

hi,

int main()
{
 using namespace std;
ifstream in_stream;
ofstream out_stream;

instream.open("infile.dat");
outstream.open("outfile.dat");

double first,second,third,forth;
in_stream >> first >> second >> third >>forth;
.
.
.
.
.
.
out_stream << "i need " <<       << "boxes to back this items" <<endl;

in_stream.close();
out_stream.close();
return 0; 
}

its an example of 4th items

use code tags next time.

This is one mistake you made (missing the "_"'s in the variable names)

in_stream.open("infile.dat");
out_stream.open("outfile.dat");

For the rest of it I can help you out a bit more but I need to know what kind of containers you can use (array, vector) if any.
I'll post a bit of code after you tell me that but I will still leave you work to do with it because I'm not doing your whole assignment for you.

^ Just reread first post about queues

Now after looking at these queues I decided to just write the whole thing out but there is a flaw with this code and I'll leave it to you to fix it.
If you enter the numbers 10 9 6 3 2 2 it would say you need 4 boxes and the last box would have a 2 in it when if you were to work that out on paper the best way to pack that would be to have the 3 in the last box. This would return 4 either way but for examples with more numbers this could cause problems.

#include <iostream>
#include <fstream>
#include <queue>

using namespace std;

int main()
{
	priority_queue<double> items, pile;
	double in, weight = 0;
	int boxes = 0;
	
	ifstream in_stream;
	ofstream out_stream;
	
	in_stream.open("infile.dat");
	
	while(in_stream >> in) //gets all the data out of the file and puts it into the items container
	{
		items.push(in);
	}
	
	while( items.size() != 0 ) //while there are still objects to be placed into boxes
	{
		while( weight < 10 && items.size() > 0 ) //while the weight is not 10 or and you are not out of numbers in the main container
		{
			if( weight + items.top() <= 10 ) //if the item can fix in the box
			{
				weight += items.top(); //add it to the weight
				items.pop(); //remove it from the list
			}
			else //otherwise
			{
				pile.push(items.top()); //add to the not able to be used list
				items.pop(); //remove it from the list
			}			
		}
		boxes++; //add a box
		weight = 0; //reset weight
		while( pile.size() > 0 ) //dump all items from the pile list into the item list
		{
			items.push(pile.top());
			pile.pop();
		}
	}
	out_stream.open("outfile.dat");
	
	if( boxes == 1 )
		out_stream << "I need " << boxes <<  " box to pack these items." <<endl;
	else
		out_stream << "I need " << boxes <<  " boxes to pack these items." <<endl;
	
	out_stream.close();
	in_stream.close();
	
	return 0;
}

very thank u. i will work on it and find it out

a little bit more help??? until now he had this:
Scan the items in the order given; place each new item in the most-filled box that can accept it without overflowing.
Use a priority queue to determine the box that an item goes in.
now:we have also a second question
Sort the items, placing the heaviest item first; then use the strategy in part (a).
plsss

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.