Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
error in running code
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
The following code checks for majority voting algorithm.
The input is 4 floating point numbers (0.40,0.40,0.40,0.30).
The amswer is 0.40.
simple code is written and does not work.
kinldy help me.
function candidate = MajorityVote(floatno)
n = size(floatno,2);
candidate = floatno(1);
counter = 0;
for ii=1:n
if counter == 0
candidate = floatno(ii);
counter = 1;
disp(candidate);
disp(counter);
elseif candidate == floatno(ii)
counter = counter + 1;
else
counter = counter - 1;
end
end
% STEP 2: Determine if the remaining element is a valid majority element.
counter = 0;
for ii = 1:n
if floatno(ii) == candidate
counter = counter + 1;
end
end
if counter < (n+1)/2
disp('There is no Majority');
candidate = '';
end
end
0 comentarios
Respuestas (1)
Sulaymon Eshkabilov
el 19 de Oct. de 2019
It is working. It has been tested in MATLAB 2019b (it is an M-file and not a function file in this test). It also runs well if saved as a function file (under file name: MajorityVote.m) without first two lines used for testing.
A= [0.40,0.40,0.40,0.30];
candidate = MajorityVote(A)
function candidate = MajorityVote(floatno)
n = size(floatno,2);
candidate = floatno(1);
counter = 0;
for ii=1:n
if counter == 0
candidate = floatno(ii);
counter = 1;
disp(candidate);
disp(counter);
elseif candidate == floatno(ii)
counter = counter + 1;
else
counter = counter - 1;
end
end
% STEP 2: Determine if the remaining element is a valid majority element.
counter = 0;
for ii = 1:n
if floatno(ii) == candidate
counter = counter + 1;
end
end
if counter < (n+1)/2
disp('There is no Majority');
candidate = '';
end
end
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!