Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Function Name: voteCounter Inputs (1): - (char) filename of votes Outputs (0): -

ID: 3537843 • Letter: F

Question

Function Name: voteCounter

Inputs (1): - (char) filename of votes

Outputs (0): - none

Function Description:

You use the brand new VoteMaster 3000 to tally up votes in the recent

elections of some of your extracurricular organizations. To your dismay,

it only outputs a txt file with a title describing the election and a

list of the candidates' names, with each line representing a vote for

that candidate.

In addition to suing the VoteMaster 3000 makers for false advertising,

you now have to create a MATLAB function named voteCounter that takes in

the txt file of names and writes another txt file containing the

candidates and their respective votes, in descending order. Furthermore,

the filename of the written txt file should be the first line of the

input txt file.

For example, if the input txt file contained

1371 Head TA Elections

McGraw

Carter

Tahler

Bragg

Carter

your written txt file should be named '1371 Head TA Elections.txt' and contain

Carter 2

McGraw 1

Tahler 1

Bragg 1

Notes:

- in the written text file, there is a space between the candidate name

and his/her number of votes

- in the event of a tie, the candidate who appears first in the input

file wins (the sort function should take care of this)

- there should be no empty line at the end of your written file (i.e. the

last line of your written file should be the candidate who received the

least number of votes)

- PLEASE open the txt files in MATLAB to avoid confusion with new lines.


Test Cases:

voteCounter('list1.txt')

=> should look like 'Fight Club Elections 2014_sol.txt'

voteCounter('list2.txt')

=> should look like 'Glee Club Elections 2014_sol.txt'

list1 reads

Fight Club Elections 2014


Durden

Paulson

Durden

Durden

Burtner

Tyndell

Tomanski

Durden

Tomanski

Burtner

Tyndell

Durden

Skalnik

Tyndell

Durden

Durden

Norton

Durden

Burtner

Tyndell

Norton

Paulson

Durden

Tyndell

Tomanski

Tomanski

Durden

Burtner

Tyndell

Skalnik

Durden

Burtner

Paulson

Durden

Skalnik


List 2 reads

Glee Club Elections 2014


Skalnik

Tomanski

Tomanski

Tomanski

Burtner

Burtner

Tyndell

Tomanski

Tomanski

Burtner

Tyndell

Skalnik

Dimmick

Burtner

Tyndell

Tomanski

Tomanski

Burtner

Tyndell

Skalnik

Tyndell

Tomanski

Tomanski

Burtner

Tyndell

Skalnik

Dimmick

Skalnik

Tomanski

Tomanski

Tomanski

Burtner

Explanation / Answer

function voteCounter(file)

fid=fopen(file);

tmp=textscan(fid,'%[^ ]s');

outname=tmp{1};

z=textscan(fid,'%s');

tmp=z{1};

n=max(size(tmp));

names={};

votes={};

k=0;

for j=1:n

curr=tmp{j};

ind=find(ismember(names,curr));

if isempty(ind)

k=k+1;

names{k}=curr;

votes{k}=1;

else

votes{ind}=votes{ind}+1;

end

end

out=char(strcat(outname,'.txt'));

final = [ names ; votes ];

final = sortrows(final',-2);

fclose(fid);

f=fopen(out,'w');

fprintf(f,'%s %d',final{1,1},final{1,2});

for j=2:k

fprintf(f,' ');

fprintf(f,'%s %d',final{j,1},final{j,2});

end

fclose(f);

end