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 8720869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:07:37+00:00 2026-06-13T07:07:37+00:00

I need to populate a vector so that it holds a sum of weights,

  • 0

I need to populate a vector so that it holds a sum of weights, where the sum must be 100. In other words, the number of items is equal to the divisor, and its values are the quotient, to ensure (force) the sum of the vector to equal 100.

Something like this: 100/3=3.333333…

vector[0]=33.33
vector[1]=33.34
vector[2]=33.33

The sum of this needs to be exactly 100 (some sort of selective rounding?)
Another example: 100/6 = 16.66666667

vector[0]=16.67
vector[1]=16.67
vector[2]=16.66
vector[3]=16.67
vector[4]=16.67
vector[5]=16.66

I’ve seen something like this done in grocery stores where something on sale might be 3 for $11, so the register displays the prices like 3.67, 3.66, and so on.

The values must add up to exactly 100 though I was thinking of doing this with an epsilon but that wouldn’t work.

const int divisor = 6;
const int dividend = 10;

std::vector<double> myVec;
myVec.resize(6);

for (int i = 0; i < divisor; ++i)
{
    ...some magic that I don't know how to do
}

EDIT: The client wants the values stored (and displayed) in values fixed at two decimal places to visually see they add to 100.

  • 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-13T07:07:37+00:00Added an answer on June 13, 2026 at 7:07 am

    You can divide into whatever is left as you go, subtracting the last value from the remaining amount.

    const int divisor = 6;
    const int dividend = 10;
    
    std::vector<double> myVec;
    myVec.reserve(6);
    double remain = 100.0;
    
    for (int i = divisor; i >= 1; --i)
    {
        double val = remain / (double)i;
        remain -= val;
        myVec.push_back(val);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of items that need to populate a set of TextBox
I have a livesearch script that i need to populate my html menu with
I need to create and populate a ListView with 3 strings that come from
I need to populate a number of UIImageView instances (about 10) with UIImage instances
vector<Widget*> vwp; // populate vwp; // Do need to specify the TYPE 'Widget' for_each(vwp.begin(),
I need to populate a couple of tables from a db that was created
I need to populate a table that will store the date ranges between 2
I need to populate a list that has 3 columns. First: Icon Image. Second:
Say I need to populate 4 or 5 dropdowns w/ items from a database.
I need to populate a JAX Bean from XML, however there is no setter

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.