% Function Name: IDfinder % % Inputs (2): - (char) file name % - (double) ID num
ID: 3538732 • Letter: #
Question
% Function Name: IDfinder
%
% Inputs (2): - (char) file name
% - (double) ID number
%
% Outputs (2): - (logical) A logical true or false if the name ID number in the file.
% - (double or char) The line on which the ID number occurs or if the skipper is found
%
% Function Description:
% Write a function, IDfinder, that scans a company's attendance database
% (text file) for ID numbers of people they think are skipping work.
% The file will have one ID number per line. The ID numbers on the file are unique.
% If the ID number exists in the file, output a logical true and the line on which it occurs.
% If the ID number does not exist, output a logical false and instead of the line number,
% output 'Found a Skipper!'.
%
% Test Case:
% ID.txt contains the following and is in the current folder:
% -------------------------------------(lines not part of the file)
% 16232
% 29564
% 35663
% 46552
% 58654
% 66665
%-------------------------------------(lines not part of the file)
%
% [log lineNum] = IDfinder('ID.txt','35663');
% -->log = true
% -->lineNum = 3
%
% [log lineNum] = IDfinder('ID.txt','11111');
% -->log = false
% -->lineNum = 'Found a Skipper!'
Explanation / Answer
function [log lineNum] = IDfinder(inp,num)
if isstr(num)
num=str2num(num);
end
log = false;
fid = fopen(inp);
tline = fgetl(fid);
k=1;
while ischar(tline)
tline=str2double(tline);
if tline == num
log=true;
lineNum =k;
end
tline = fgetl(fid);
k=k+1;
end
fclose(fid);
if log == false
lineNum= 'Found a Skipper!';
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.