Click here to Skip to main content
15,877,051 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Input: S= set of cameras; T = set of targets; P= set of discrete
pans; K= No. of set covers
Output: C=Collection of Set Covers C1, C2....CK
2: /*SENSORS keeps track of the list of unused sensors */
3: k = 0
4: while SENSORS 6= ∅ do
5: /* a new set cover Ck will be formed */
6: k= k + 1
7: Ck=∅
9: /*TARGETS contains the uncovered targets */
10: while TARGETS 6= ∅ do
11: /* more targets have to be covered */
12: Find Dmin = minD(t) : t ∈ T
13: if |Dmin| = 0 then
14: /*TARGETS contains some uncoverable targets*/
15: goto marker
16: else
17: S′ = ∅
18: Find the targets having cardinality equals Dmin
and create a set T ′
19: for each t ∈ T ′
20: for each (si, pj) ∈ −1(t)
21: S′ = S′ ∪ {(si, pj)}
22: Find the most contributing (su, pv) such that:
23: (su, pv)←argmaxsu∈S’,1≤v≤quv∩TARGETS
24: /* selects (su, pv) covering maximum targets */
25: In case of tie, create a subset S′′ ⊆ S′ with
all sensor-pan pairs having same contribution
26: Let (si, pj) ∈ S′′ has the maximum force F′
among all sensor-pan pairs in S′′, i.e.:
27: F′
ij = argmaxvFuv; ∀su ∈ S; ∀pv ∈ P;
∀si ∈ S; ∀pj ∈ P;
28: Ck = Ck ∪{(si, pj)}
29: SENSORS = SENSORS \ {si}
30: TARGETS = TARGETS \ {ij}
31: end if
32: end while
33: C = C ∪ Ck
34: end while
35: marker: return the collection of set covers C

What I have tried:

need help to understand how to convert this algorithm into c++
Updated 29-Sep-22 6:34am
CPallini 21-Mar-19 14:10pm    
You can convert it... Coding!
CodeWraith 22-Mar-19 6:32am    
Just what I thought. :-)
That's what some people mean by saying 'programming'.
RedDk 22-Mar-19 14:19pm    
Actually what all these guys down here are saying is true and there's good suggestions as to how to start but looking at this code ... it almost looks as simple as QBASIC (since it's numbered). You might try just pasting it into an empty C# project module then fill in the function declarations. Been there done that.

I know, not C++ ...

1. write a C++ main function
2. define a C++ function to implement your pseudo code: you need to decide what values need to passed to this function, and what is the desired output
3. create or construct the inputs to your function in the main function (just as an example to be able to test your code)
4. Implement the function: Analyze each statement in your pseudo code and translate them into C++ statements; don't forget to properly define any variables introduced in a pseudo code statement.
5. test and correct your code as needed; if you run into a specific problem that you don't know how to solve, come back here and ask
Share this answer
How can I convert a pseudo code into source code ?

Pseudo code is a convention used by your teacher to describe how the program would work, it is not specific to a programming language, it is rather specific to the domain it is related to.
Reread your courses about pseudo code and C++.

This code is a function:
- make C++ skeleton.
- paste pseudo code in empty function skeleton.
- make each line a comment.
- after each line of pseudo code, add the C++ code that match the pseudo code.

If you have specific question, show what you have done.
Share this answer
merano99 29-Sep-22 12:10pm    
Patrice T 29-Sep-22 13:40pm    
Thank you
The way you implement any new project:

1) Study the pseudo code and extract the algorithm.
2) Design the code (preferably eliminating the goto's) to implement the algorithm.
3) Code the design.
4) Test the code.
At any point, go back to an earlier stage as necessary until it works.
Share this answer
Stefan_Lang 22-Mar-19 6:00am    
Suggesting to design the code before actually coding it is a great suggestion, but it's a bit like trying to run before learning to walk ;-)
I recommend Patrice T.'s and Stefan_Lang's approaches. Here is another help to implement the pseudocode concretely in C++. Since the code, as mentioned by Patrice, is specific for a certain application area, there will probably be special knowledge from the context, which we do not have here.

Every C++ program starts with main :
int main()

   // TODO:

   return 0;

The pseudocode can usually be used directly with minor syntactical changes. I would first insert the pseudocode at // TODO: and comment it out. The description of the input and output data has been assigned a letter as a label, but you have to specify a data type yourself. A set usually consists of similar things. If you know the number, you could define an array, if not a vector or a list would make sense. The data type in the vector can be defined if you know what kind of data it should contain. Simple numbers often have the datatype int. If the datatype should store more than one thing, a struct would be useful.

In line 4 a loop starts, but no termination condition is visible. I assume that it is iterated over all sensors.
4 : while SENSORS 6 = ∅ do

Since with S the set of cameras is assigned, SENSORS seems to have the same data type, otherwise it would not be possible to assign it. In addition, there is a constant ∅(average) with the value 6. If you want to iterate over all sensors, you need a for () loop that starts with the first element and ends with the last.
for (auto si : sensors) { ... }

Usually examples are given as input data. Since these are missing here you can start with your own test data.
const std::vector<int> S = { 1,2,3,4,5,6 }; // set of cameras;
const std::vector<int> T = { 2,4,6,8,12 }; // set of targets;

This help to implement the pseudo code in C++ by yourself. I hope my initial thoughts were helpful.

From my point of view, sample data as well as background knowledge about the task the program has to do here is missing.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900