im struggling with this one guys like the oddsum and the rest is undefeatable with me

1 Ansicht (letzte 30 Tage)
its kinda fading
  4 Kommentare

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Stephen23
Stephen23 am 15 Jun. 2023
Bearbeitet: Stephen23 am 15 Jun. 2023
N = 'S2307605'; % must be character
V = N(2:end)-'0'
V = 1×7
2 3 0 7 6 0 5
R = rem(sum(3*V(1:2:end))+sum(V(2:2:end)),11)
R = 5
S = struct('S','A':'K','G','K':'U');
Z = [N,S.(N(1))(1+R)]
Z = 'S2307605F'
  1 Kommentar
Stephen23
Stephen23 am 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);
Odd Sum: 39
fprintf('Even Sum: %d\n', evenSum);
Even Sum: 10
fprintf('Total Sum: %d\n', totalSum);
Total Sum: 49
fprintf('Remainder: %d\n', remainder);
Remainder: 5
% 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);
NRIC with checksum: S2307605F
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by