Hi I found this piece in another "solved" threat.
The person that wrote this however had a different problem with it.
I changed the bar size value from 12 to 6500.
For me this works but has a major flaw :

When :
lengths to cut < size of a bar
then :
no new bar is created and the cycle is finished.

for example : if i have 7x1000 to cut it will show :
bar 1 : 1000 1000 1000 1000 1000 1000
...
not making a second bar for the last 1000.

I'm looking at this for 2 hrs now, I can't see why it does not create a new bar.
Any help pls?

#include <iostream>
#include <iomanip>
#include <vector>
#include <iterator>
#include <algorithm>
#include <numeric>
/* Get the number of different lengths from the user */
 
struct Cut{
unsigned number;
double length;
};
 
 
int main () {
 
std::vector< Cut > allCuts;
Cut currentCut;
unsigned numberOfSizes;
std::cout << "Enter number of different sizes: ";
std::cin >> numberOfSizes;
 
/* Now get the actual numbers and lengths from the user */
for(unsigned i = 0; i < numberOfSizes; ++i){
//currentCut = new Cut;
std::cout << "Enter new cut: <number> <length>" << std::endl;
std::cin >> currentCut.number >> currentCut.length;
/* Check that the length is sensible */
if((currentCut.length <= 0) || (currentCut.length > 6500)){
std::cerr << "Error length must be in the range (0,6500]" << std::endl;
--i;
}
else
allCuts.push_back(currentCut);
}
 
/* Now put all the cuts into a single large vector */
std::vector< double > lengths;
for(unsigned i = 0; i < allCuts.size(); ++i){
for(unsigned j = 0; j < allCuts[i].number; ++j)
lengths.push_back(allCuts[i].length);
}
 
/* Sort the vector */
std::sort(lengths.begin(), lengths.end());
 
/* Make a place to put the lengths to cut from each bar */
const unsigned NEW_BAR_LENGTH = 6500;
std::vector< std::vector< double > >bars;
std::vector< double > currentBar;
double remainingLength = NEW_BAR_LENGTH;
 
/* Now keep going through the lengths vector until all the cuts are accounted for */
while(lengths.size() > 0){
/* start at the longest length that needs to be cut ... */
std::vector< double >::iterator it = lengths.end() - 1;
/* Go until the smallest length that's needed is reached */
while(it >= lengths.begin()){
/* Check if the current length will fit */
if(*it < remainingLength){ /* If it will... */
/* ... Add it to the current bar and remove it from the list to be fitted */
currentBar.push_back(*it);
/* Update the amount of the bar remaining */
remainingLength -= *it;
/* Remove the current element from the lengths vector */
lengths.erase(it);
}
/* Check that we didn't just remove the last element */
if(lengths.size() > 0){
/* length[0] is the shortest length cut that we still need to get
  if this is more than the remaining amount of the current bar,
  then we should finish this bar and start a new one, check for
  this now */
if(lengths[0] > remainingLength){
/* Reset the remaining length */
remainingLength = NEW_BAR_LENGTH;
/* Add this completed bar to the vector of bars */
bars.push_back(currentBar);
/* Clear the current bar */
currentBar.clear();
/* Exit this loop and start on the new bar */
break;
}
}
--it;
}
}
 
double totalWaste = 0.0;
for(unsigned i = 0; i < bars.size(); ++i){
std::cout << "Bar " << std::setw(2) << i + 1 << ":\t";
for(unsigned j = 0; j < bars[i].size(); ++j)
std::cout << std::setprecision(3) << std::fixed << bars[i][j] << " ";
std::cout << std::endl;
std::partial_sum(bars[i].begin(), bars[i].end(), bars[i].begin());
totalWaste += NEW_BAR_LENGTH - bars[i].back();
}
 
/* Print a summary */
std::cout << "===============================" << std::endl;
std::cout << "Bars used:\t" << bars.size() << std::endl;
std::cout << "Total waste:\t" << totalWaste << " m" << std::endl;
std::cout << "Waste/Bar:\t" << totalWaste/bars.size() << " m" << std::endl;
std::cout << "===============================" << std::endl;
system("pause");
return 0;
}

Please post the complete problem statement .... I have no clue what was written in the "other guys" post and have no desire to go hunting for it

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.