usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Code for Selection with Limits to Repetitions in C++



Conditional Selection with Limits on Repetition.

So it's Christmas, and your mum delegates you, for some mysterious reason, to go buy nine (9) balloons - of any of the colours red, orange, yellow, green, blue, indigo, violet, pink, milk, white,- for home decoration.

But here's the catch. You are to buy:
  1. a minimum of 1 red balloon and a maximum of 4 red balloons;
  2. a minimum of 1 orange balloon and a maximum of 3 orange balloons;
  3. a minimum of 1 yellow balloon and a maximum of 2 yellow balloons;
  4. a minimum of 1 green balloon and a maximum of 4 green balloons;
  5. a minimum of 1 blue balloon and a maximum of 3 blue balloons;
  6. a minimum of 1 indigo balloon and a maximum of 2 indigo balloons;
  7. a minimum of 1 violet balloon and a maximum of 4 violet balloons;
  8. a minimum of 1 pink balloon and a maximum of 3 pink balloons;
  9. a minimum of 1 gray balloon and a maximum of 2 gray balloons;
  10. a minimum of 1 white balloon and a maximum of 4 white balloons.

With these conditions, every family member's favourite colour is covered for, and the decoration mix is also kept lively.

This is quite a handful for you to handle...



Code for Limited Repetitive Selection in C++

The code for Selection with Conditioned Repetition will be based on that for Selection with boundless Repetition.

All that is needed after Selection with limitless Repetition is a Productive, as opposed to Summative, check of the results from the Selection with unconditioned Repetition for those options that meet our conditions.

This is how our Limited Repetitive Selection algorithm in C++ will work.

Create a new C++ class file;
Call it ConditionalSelection.
Type out the adjoining C++ code for Selection with Conditioned Repetition.








ConditionalSelection Header File

#pragma once

#include "Selection.h"
#include <array>

class ConditionalSelection : public Selection
{
public:
    ConditionalSelection();
    virtual ~ConditionalSelection();
    vector<vector<string>> limitedSelection(vector<string>, unsigned shortunsigned short[], unsigned short[]);
private:
    vector<vector<string>> final_elements;
};


C++ Code for ConditionalSelection Class File

#include "stdafx.h"
#include "ConditionalSelection.h"
#include <iostream>

ConditionalSelection::ConditionalSelection() : Selection()
{
}

vector<vector<string>> ConditionalSelection::limitedSelection(vector<stringcandidatesunsigned short dimensionunsigned short minimum[], unsigned short maximum[]) {
    final_elements = {};
    groupSelection(candidatesdimension);
    for (int i = 0; i < complete_group.size(); i++) {
        bool state = false;
        for (int j = 0; j < words.size(); j++) {
            // get 'words[j]' frequency/count in group
            int frequency = count(complete_group[i].begin(), complete_group[i].end(), words[j]);
            if (frequency >= minimum[j] && frequency <= maximum[j]) {
                state = true;
            }
            else {
                state = false;
                break;
            }
        }
        // skip if already in net
        if (state) {
            final_elements.push_back(complete_group[i]);
        }
    }
    return final_elements;
}

ConditionalSelection::~ConditionalSelection()
{
}

Main Class

#include "stdafx.h"
#include "ConditionalSelection.h"
#include <vector>

#include <iostream>

using namespace std;

int main()
{
    vector<string> goods = { "0""1""2""3""4""5""6""7""8""9" };
    unsigned short min_occurrence[]{ 0, 0, 1, 0, 0, 1, 0, 0, 1, 0 };
    unsigned short max_occurrence[]{ 4, 3, 2, 4, 3, 2, 4, 3, 2, 4 };
    ConditionalSelection choose;
    vector<vector<string>> result = choose.limitedSelection(goods, 4, min_occurrence, max_occurrence);
    // print choices and operation
    cout << "\n[ ";
    for (string choice : choose.words) {
        cout << choice << "; ";
    }
    cout << "] conditioned selection " << choose.r << ":\n\n";

    // print out selections nicely
    int i = 0;
    for (vector<stringset : result) {
        i++;
        cout << i << ":    ";
        for (string member : set) {
            cout << member << "; ";
        }
        cout << "\n";
    }
    cout << "\nNumber of ways is " << result.size() << ".";

    return 0;
}




<< PreviousNext >>