Question ?1 :0 what it means

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 ??

Can u just explan that line for me??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
using namespace 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;
}
Last edited on
zaposleni += iskoristeni[i] ? 1 : 0;

This part is easy,
 
    zaposleni += something;
is a shorter way of writing
 
    zaposleni = zaposleni + something;


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.

The statement could be re-written as:
1
2
3
4
if (iskoristeni[i] == true)
    zaposleni = zaposleni + 1;
else
    zaposleni = zaposleni + 0;

or perhaps
1
2
if (iskoristeni[i])
    zaposleni++;


Please see "Conditional operator ( ? )" http://www.cplusplus.com/doc/tutorial/operators/

Hope this helps.
Last edited on

zaposleni = zaposleni + something;// this part i do understand

so next part that u explained

if lets say "iskoristeni [100]" is true than value 1 will be added to "zaposleni"
so zaposleni will be equal to 1 and so on going in loop

this is an issue for me

lets say inputs are

A001 -s1
A101 -s2
2 - N -> number of retired agents
A100 ->first retired agent
C345 -> second retired agent


program should return 101 agent that have agent codes minus one agent thas is retired (A100) so 101-1=100 ???

zaposleni += iskoristeni[i] ? 1 : 0; // SO I THIN THIS WAS WRONG IN TEACHERS SOLUTION


zaposleni += iskoristeni[i] ? 0 : 1; // it makes sense that it is added 0 if its true, and 1 if its false
Last edited on
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;
tnx mate :)
Topic archived. No new replies allowed.