Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8947779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:51:15+00:00 2026-06-15T12:51:15+00:00

I have this program which fills a vectors with float values, ensuring each vector

  • 0

I have this program which fills a vectors with float values, ensuring each vector (bin) has less than a sum of 1.0 when the float values are totaled. I discovered through debugging that the program is halting when it tries to add values of a vector to a float. The reason is because the for loop is running more times than elements that exist in the vector. I inserted a cout to check the value of the size, and am getting a very large number (95 million something)

//Bin packing algorithm
//Takes float values which are <1 and packs them into bins.

#include <iostream>
#include <vector>
#include <list>
#include <cassert>

using namespace std;
typedef unsigned int uint;

bool gt( float a, float b )
{
   return b < a;
}

int main()
{
   list< float > S; 
   while( ! cin.eof() )
   {
      float value;
      if( cin >> value && ! cin.eof() )
      {
      assert( value > 0.0 && value <= 1.0 );
         S.push_back( value );
      }
   }

   uint n = S.size();
   uint operations = 0;

   vector< vector< float > > bins;
   S.sort( gt );

   float currentitem;
   float binsum; 

   uint totalbins = 0; 
   uint currentbin; 

   vector<float> tempbin;
   while(S.size() != 0)
   {
      currentitem = S.front();
      S.pop_front();
      currentbin = 0;
      while(currentitem != 0 && totalbins > currentbin)
      {
         currentbin++;
         bintotal = 0;
         for(uint i=0; i < bins[currentbin].size(); i++) 
         {
            cout << "i=" << i <<" bins[currentbin].size()=" << bins[currentbin].size() <<  endl;
            binsum += bins[currentbin][i]; //THIS IS WHERE THE DEBUGGER POINTS. bins[currentbin].size() is producing the 95 million garbage value.
         }
         if((binsum + currentitem) <= 1)
         {
            currentitem = 0;
            bins[currentbin].push_back(currentitem);
         }
      }
      if(currentitem != 0)
      {
         totalbins++;
         tempbin.push_back(currentitem);
         bins.push_back(tempbin);
         tempbin.clear();
      }
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:51:16+00:00Added an answer on June 15, 2026 at 12:51 pm

    bins[currentbin] in the for loop access invalid bin as currentbin variable is already incremented. You need to currentbin++ after the for loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this program in Python which should save text files to a folder
Alright, I have this program to sparse code in Newick Format, which extracts both
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have a program which does a system call: latex somefile.latex This runs ok,
So I have a program which runs. This is part of the code: FileName
I have a GUI program, which would call a cmd in this GUI program.
I have a real world program that is similar to this one, which I'll
I have a program which fetches records from database (using Hibernate) and fills them
I have this following program which gets the number of files from user, then
I have a program which references a DLL. Code in this DLL gets the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.