Gaboros
Gaboros

Reputation: 191

"Dynamic" if statement c++

I want to make a program which can generate football matches depending on the team. The conditions:

For example: we have 4 team: a,b,c and d.

So an example solution:

Round 1:
a-b
c-d

Round 2:
a-c
b-d

Round 3:
a-d
c-b

I have function which can generate an array when everyone play with everyone.

For example:

a-b
a-c
a-d
b-c
b-d
c-d

But I have to sort this for rounds. I sort the function on that way I put the first on in each round:

Round 1:
a-b
x-x

Round 2:
a-c
x-x

Round 3:
a-d
x-x

I can arrange the missing ones in this case with a simple if statement. I check in each round that the two x can't be a and b. And in this way it can sort. But the problem is when I got at least 3 team in a round.

Round 1:
a-b
x-x
y-y

I have 3 loop in each other so it could be sort with any team if I had a "dynamic" if statement.

I mean in this way I don't need only check the two x that can't be a and b because I got two y-y which can't be a,b,c and d.

So is there any way to do this if statement dynamical?

Because this:

if (iTR[x][0] != iT[i][0] && iTR[x][0] != iT[i][1])

only can't check one place but I need to check always more and more places.

Sorry for write this so complicated but I couldn't write it easier. And thank you in advance!

Upvotes: 1

Views: 2152

Answers (3)

It's not entirely clear from your phrasing. But it sounds (...maybe?) like you have working code that does not make you happy because you are running an if statement inside of a loop, instead of having a "more powerful" variation of if. This "dynamical if" would run an arbitrary number of comparisons at once, making a loop unnecessary.

So perhaps the kind of thing you don't care for is:

vector<int> v;
...
bool noOnes = true;
for (vector<int>::iterator i = v.begin(); i != v.end(); i++) {
    if (*i == 1) {
        noOnes = false;
        break;
    }
}
if (noOnes) {
    cout << "No values in the vector were 1";
}

And you are imagining something which would behave the same way but be more like:

vector<int> v;
...
// pseudocode, I'm making this "ifall" syntax up
ifall(v, != 1) {
    cout << "No values in the vector were 1";
}

If I've guessed the spirit of your question correctly, it's a reasonable thing to ask about. But the first thing to point out is that the word "dynamic" in C++ (and other software contexts) has a specific meaning that doesn't really have anything to do with this. It's generally the opposite of "static", and you can read up on the terminology with some searching:

https://stackoverflow.com/search?q=difference+between+static+and+dynamic+%5Bc%2B%2B%5D

As for a technique that is "built-in" to C++ which lets you express such things without your code containing loops...it exists. There is a header in the standard library called <algorithm> which "defines a collection of functions especially designed to be used on ranges of elements".

http://www.cplusplus.com/reference/algorithm/

Using this, you could test to see if any elements in a vector weren't 1 with something like...say...find_if and passing it a function that tests to see if a specific element isn't one:

bool IsOne(int i) {
    return (i == 1);
}
...
vector<int> v;
...
if (find_if(v.begin(), v.end(), IsOne) == v.end())) {
    cout << "No values in the vector were 1";
}

The looping is all taken care of inside of the find_if algorithm. For better or worse, it is rare that I see people using <algorithm> in "real world" codebases. I'd imagine most early C++ courses will tell you to not worry about it, and get the answer right and correct using loops. Whether you want to go down this particular rabbit hole is up to you.

Upvotes: 3

I'm not sure to understand your question. In a trivial sense, almost every if is dynamic. If it is completely static, the compiler would optimize it out.

I believe you didn't understand what your homework is about. It is more a question of understanding the algorithm than of coding it in a particular language like C++.

However, as a hint, you could consider coding a utility function which computes your complex (or dynamic) condition.

Hope this will help.

Upvotes: 0

Grammin
Grammin

Reputation: 12205

Why can't you do something like:

for(int ww =0; ww < numRounds; ww++)
  for(int zz =0; zz < numRounds; zz++)
    if(iTR[x][zz] != iT[i][ww])

Upvotes: 0

Related Questions