Advertisement
JKattackk

Decoder

Feb 6th, 2020
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.24 KB | None | 0 0
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % Course: ENCMP 100
  3. % Assignment: 2
  4. % Name:
  5. % CCID:
  6. % U of A ID:
  7. %
  8. % Acknowledgements:
  9. % I refered to the MatLab documentation while writing.
  10. %
  11. % Description:
  12. % This program is meant to decode a secret code according to rules outlined
  13. % in the project description.
  14. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  15.  
  16. clc;
  17. clear;
  18. code = int2str(input("Please enter a code to break: "));
  19. days = ["Monday", "Tueday", "Wednesday", "Thursday", "Friday"];                                                                                 %%Day and location arrays for later use.
  20. locations = ["the bridge.", "the library.", "the river crossing.", "the airport.", "the bus terminal.", "the hospital.", "St. Petes Church"];
  21. intArr = (1:9);
  22. if(length(code) == 9)  %%Test for correct code length
  23.     sum = 0;
  24.     for x = 1:length(code)  %%Put the code into an integer array
  25.         intArr(x) = str2double(code(x));
  26.     end
  27.     for i = 1:length(intArr)  %%Sum all digits of the code
  28.         sum = sum + intArr(i);
  29.     end
  30.     if(2*int8(sum / 2) == sum)  %% Test for even code
  31.         day = intArr(1)*intArr(3) - intArr(5);
  32.         if(day > 0 && day <= 7)  %%Test for valid day, 1-7
  33.             if(rem(intArr(2)*intArr(4)-intArr(6), 3) == 0)  %%If divisible by 3
  34.                 loc = intArr(7) - intArr(9);
  35.                 if(loc > 0 && loc <= 7)  %%Test for valid location, 1-7
  36.                     disp("Rescue on " + days(day) + " at " + locations(loc));   %%Print out message using the arrays from the start.
  37.                 else
  38.                     disp("Decoy message: Invalid rendezvous point.");
  39.                 end
  40.             else
  41.                 loc = intArr(8) - intArr(9);
  42.                 if(loc > 0 && loc <= 7)%%Test for valid location, 1-7
  43.                     disp("Rescue on " + days(day) + " at " + locations(loc)); %%Print out message using the arrays from the start.
  44.                 else
  45.                     disp("Decoy message: Invalid rendezvous point.");
  46.                 end
  47.             end
  48.         else
  49.             disp("Decoy message: Invalid rescue day.");
  50.         end
  51.     else
  52.         disp("Decoy message: Sum is odd.");
  53.     end
  54. else
  55.     disp("Decoy message: Not a nine-digit number.");
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement