So the teacher gave us solution for one problem and when i asked him why and how it works he didnt know to explain, now. Problem is user inputs special agent codes like A100, or B007, now s1 i s code for first agent and s2 last ganet that is now in secret agency, problem is that some agent codes are already in use, so if user inputs A001 , and for last A100, output should be 100 new agents, and if he types A004 for used agent code, then output should be 99 agents, as that code is used in range from A001-A100
zaposleni is my most recent holder value, iskoristeni (used on english) is bool array ??
#include <iostream>
#include <string>
usingnamespace std;
int rac(string s)
{
int ret = (s[0] - 'A' ) * 1000;
ret += (s[1] - '0') * 100;
ret += (s[2] - '0') * 10;
ret += (s[3] - '0') * 1; // ZA A/100 VRATI 100, ZA B200 VRATI 1200
return ret;
}
int main()
{
string s1, s2;
int N;
cout << "Unesite oznaku prvog agenta:" << endl;
cin >> s1;
cout << "Unesite oznaku zadnjeg agenta:" << endl;
cin >> s2;
cout << "Unesite broj agenata koji su umirovljeni:" << endl;
cin >> N;
bool iskoristeni[30000] = { 0 }; // flag je spušten tj. false za sve
for (int i = 0; i < N; ++i) // unos umirovljenih oznaka
{
string s;
cin >> s;
iskoristeni[rac(s)] = true; // odmah mijenjam tim oznakama da su potrosene
}
int zaposleni = 0;
for (int i = rac(s1); i <= rac(s2); ++i) // izracun zaposlenih od s1 do s2
zaposleni += iskoristeni[i] ? 1 : 0; // why on earth is this working ??
cout << zaposleni << endl;
return 0;
}
So what is the "something"? iskoristeni[i] ? 1 : 0
This is an example of the conditional (or ternary) operator.
First the expression before the '?' is evaluated as true or false
If the expression iskoristeni[i] is true, the first value is taken, otherwise the second (after the colon ':') is taken as the result.
I think you are correct. To begin with I did not understand the original problem, but according to the required result, yes this makes more sense. zaposleni += iskoristeni[i] ? 0 : 1;