Search code examples
c++vectordiscrete-mathematics

How to set up a loop that iterates over possible vector table elements


The program has to do with arithmetic sequence. I have my program arranged with these constants that are used for indexing

  • F = (First term in arithmetic series),
  • I = ( increment / common difference between terms),
  • L = (Last term in arithmetic series),
  • N = (Number of terms in series),
  • and T = (Total number by adding all terms in series).

The user should enter 3 of the five 5 characters mentioned above followed by a double value that makes sense for each character.

For example:
If the user inputs:

t 3.6 f 1.1 l 1.3

or

T 3.6 F 1.1 L 1.3

The user should receive output of

I 0.1 N 3

This is because the user has given 3 of 5 options defined as constants

I have defined functions to answer all cases required on user input. I have labeled each function name by first 3 letters being the given and last two letters being the unknown. For the example above, calling void fltin(vector<double> & v, vector<bool> & k) would output I and N. (2 cases have been exempt and I have created a function to let the user know should those cases arise below)

Another example: Calling filnt(vector<double> & v, vector<bool> & k) says the user is giving the values of F, I, and L. Output should be N and T.

My problem: I am having trouble at the final steps where given user input (not case sensitive) I can call the correct vector element in TABLE. I want to use a vector of type booleans to also control this.

All help is appreciated.

#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;
const unsigned F = 0, I = 1, L = 2, N = 3, T = 4;
bool die(const string & msg);
void filnt(vector<double> & v, vector<bool> & k) {
    v[T] = v[F];
    for (double i = (v[F] + v[I]); i <= (v[L] + v[I]); i += v[I]) {
        v[T] += i;
    }
    cout << "T:" << v[T] << " ";
    v[N] = (2 * v[T]) / (v[F] + v[L]);
    cout << "N:" << v[N] << endl;
}
void finlt(vector<double> & v, vector<bool> & k) {
    v[L] = v[F];
    for (double i = 1; i < v[N]; i++) {
        v[L] += v[I];
    }
    cout << "L:" << v[L] << " ";

    v[T] = v[F];
    for (double i = (v[F] + v[I]); i <= (v[L]); i += v[I]) {
        v[T] += i;
    }
    cout << "T:" << v[T] << endl;
}

void fitln(vector<double> & v, vector<bool> & k) {
    bool die("not checking this condition");
}

void flnit(vector<double> & v, vector<bool> & k) {
    v[I] = (v[L] - v[F]) / (v[N] - 1);
    cout << "I:" << v[I] << " ";
    v[T] = v[F];
    for (double i = (v[F] + v[I]); i <= (v[L] + v[I]); i += v[I]) {
        v[T] += i;
    }
    cout << "T:" << v[T] << endl;
}

void fltin(vector<double> & v, vector<bool> & k) {
    v[N] = (2 * v[T]) / (v[F] + v[L]);
    cout << "N:" << v[N] << " ";
    v[I] = (v[L] - v[F]) / (v[N] - 1);
    cout << "I:" << v[I] << endl;
}

void ftnil(vector<double> & v, vector<bool> & k) {
    v[L] = ((2 * v[T]) / v[N]) - v[F];
    cout << "L:" << v[L] << " ";
    v[I] = (v[L] - v[F]) / (v[N] - 1);
    cout << "I:" << v[I] << endl;
}

void iltfn(vector<double> & v, vector<bool> & k) {
    bool die("not checking this condition");
}
void ilnft(vector<double> & v, vector<bool> & k) {
    v[F] = v[L];
    for (double i = 1; i < v[N]; i++) {
        v[F] -= v[I];
    }
    cout << "F:" << v[F] << " ";
    v[T] = v[F];
    for (double i = (v[F] + v[I]); i <= (v[L]); i += v[I]) {
        v[T] += i;
    }
    cout << "T:" << v[T] << endl;
}

void itnfl(vector<double> & v, vector<bool> & k) {
    v[F] = ((v[T] * (2 / v[N])) - ((v[N] - 1)*v[I])) / 2;
    cout << "F:" << v[F] << " ";
    v[L] = ((2 * v[T]) / v[N]) - v[F];
    cout << "L:" << v[L] << endl;
}

void lntfi(vector<double> & v, vector<bool> & k) {
    v[F] = ((2 * v[T]) / v[N]) - v[L];
    cout << "F:" << v[F] << " ";
    v[I] = (v[L] - v[F]) / (v[N] - 1);
    cout << "I:" << v[I] << endl;
}

struct FD { 
    double k1; 
    double k2; 
    double k3;
    double uk1;
    double uk2;
    void(*f)(vector<double> &, vector<bool> &); 
};

const vector<FD> TABLE = {
{F,I,L,N,T,filnt},
{F,I,N,L,T,finlt},
{F,L,N,I,T,flnit},
{F,L,T,I,N,fltin},
{F,T,N,I,L,ftnil},
{I,L,N,F,T,ilnft},
{I,T,N,F,L,itnfl},
{L,N,T,F,I,lntfi}
};


int main() {
    vector<double> v(5);
    vector<bool> k = { false, false, false, false, false };
    char cone;
    char ctwo;
    char cthree;
    double one;
    double two;
    double three;
    cin >> cone;
    cin >> one;
    cin >> ctwo;
    cin >> two;
    cin >> cthree;
    cin >> three;
    // for loop I am having trouble constructing 
    /*

    */
}
bool die(const string & msg) {
    cout << "Fatal Error:" << msg << endl;
    exit(EXIT_FAILURE);
}

Solution

  • You might create a map

    void assign(std::vector<double>& v, std::vector<bool>& flags, char c, double value)
    {
        switch (c)
        {
            case 'F': case 'f': v[F] = value; flags[F] = true; break;
            case 'I': case 'i': v[I] = value; flags[I] = true; break;
            case 'L': case 'l': v[L] = value; flags[L] = true; break;
            case 'N': case 'n': v[N] = value; flags[N] = true; break;
            case 'T': case 't': v[T] = value; flags[T] = true; break;
        }
    }
    
    int main()
    {
        vector<double> v(5);
        vector<bool> flags = { false, false, false, false, false };
        for (int i = 0; i != 3; ++i) {
            char c;
            double value;
            std::cin >> c >> value;
            assign(v, flags, c, value);
        }
        const std::map<std::vector<bool>, void(*)(std::vector<double> &)> m = {
            {{true, true, true, false, false}, filnt},
            {{true, true, false, true, false}, finlt},
            {{true, false, true, true, false}, flnit},
            {{true, false, true, false, true}, fltin},
            {{true, false, false, true, true}, ftnil},
            {{false, true, true, true, false}, ilnft},
            {{false, true, false, true, true}, itnfl},
            {{false, false, true, true, true}, lntfi}
        };
        auto it = m.find(flags);
        if (it == m.end()) {
            std::cout << "Fatal Error: not checking this condition\n";
            return EXIT_FAILURE;
        }
        it->second(v);
    }
    

    Demo