im struggling with this one guys like the oddsum and the rest is undefeatable with me
2 views (last 30 days)
Show older comments

its kinda fading
4 Comments
Answers (1)
Stephen23
on 15 Jun 2023
Edited: Stephen23
on 15 Jun 2023
N = 'S2307605'; % must be character
V = N(2:end)-'0'
R = rem(sum(3*V(1:2:end))+sum(V(2:2:end)),11)
S = struct('S','A':'K','G','K':'U');
Z = [N,S.(N(1))(1+R)]
1 Comment
Stephen23
on 15 Jun 2023
Lets fix your code too:
nric = 'S2307605';
assert(ischar(nric)&&numel(nric)==8,'The NRIC must have 8 characters.')
% Check if the first character is 'S' or 'G'
assert(any(strncmp(nric,{'S','G'},1)),'The first character must be "S" or "G".')
% Check if there are any non-digit characters in the numeric portion of the NRIC
numericPortion = nric(2:end);
assert(all(isstrprop(numericPortion, 'digit')),'The numeric portion of the NRIC should only contain digits.')
% Extract the first character to determine the status
status = nric(1);
% Convert the numeric portion to a numeric array
numericArray = sscanf(numericPortion,'%1d',[1,Inf]);
% Calculate the sum of digits in odd-numbered positions
oddSum = sum(3*numericArray(1:2:end));
% Sum the digits in even positions
evenSum = sum(numericArray(2:2:end));
% Calculate the total sum
totalSum = oddSum + evenSum;
% Calculate the remainder when divided by 11
remainder = mod(totalSum, 11);
% Display the intermediate calculations
fprintf('Odd Sum: %d\n', oddSum);
fprintf('Even Sum: %d\n', evenSum);
fprintf('Total Sum: %d\n', totalSum);
fprintf('Remainder: %d\n', remainder);
% Determine the checksum letter based on the status
if status == 'S'
checksumLetter = getChecksumLetterS(remainder);
elseif status == 'G'
checksumLetter = getChecksumLetterG(remainder);
else
error('Invalid NRIC format.');
end
% Add the checksum letter to the NRIC
nricWithChecksum = [nric, checksumLetter];
fprintf('NRIC with checksum: %s\n', nricWithChecksum);
function letter = getChecksumLetterS(remainder)
switch remainder
case 0
letter = 'A';
case 1
letter = 'B';
case 2
letter = 'C';
case 3
letter = 'D';
case 4
letter = 'E';
case 5
letter = 'F';
case 6
letter = 'G';
case 7
letter = 'H';
case 8
letter = 'I';
case 9
letter = 'J';
case 10
letter = 'K';
otherwise
error('Invalid remainder.');
end
end
function letter = getChecksumLetterG(remainder)
switch remainder
case 0
letter = 'K';
case 1
letter = 'L';
case 2
letter = 'M';
case 3
letter = 'N';
case 4
letter = 'O';
case 5
letter = 'P';
case 6
letter = 'Q';
case 7
letter = 'R';
case 8
letter = 'S';
case 9
letter = 'T';
case 10
letter = 'U';
otherwise
error('Invalid remainder.');
end
end
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
