jeudi 30 avril 2020

How to scale values according to a counter

I have five integer values

std::vector<int> vec(5, 0);

the value of each int will be based on a counter.

if counter is 0 than all the ints would be 0

if the counter is 100 than all the ints will have value of 20

so when the counter is 20 the first int will have a value of 20 and the rest will be 0.

when the counter is 30 the first int will be 20 and the second int will be 10

This is my code for this i am interested to see other logics to attain this,

std::vector<int> vec(5, 0);
    int counter = 50;
    for (int i = 0; i < 5; i++)
    {
        int startValue = (i) * 20;
        int finalValue = counter - startValue;

        if (finalValue > 20)
            finalValue = 20;

        if (finalValue < 0)
            finalValue = 0;

        vec[i] = finalValue;
    }


    for (auto &v : vec)
    {
        std::cout << v << std::endl;
    }

Aucun commentaire:

Enregistrer un commentaire