Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % Course: ENCMP 100
- % Assignment: 2
- % Name:
- % CCID:
- % U of A ID:
- %
- % Acknowledgements:
- % I refered to the MatLab documentation while writing.
- %
- % Description:
- % This program is meant to decode a secret code according to rules outlined
- % in the project description.
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- clc;
- clear;
- code = int2str(input("Please enter a code to break: "));
- days = ["Monday", "Tueday", "Wednesday", "Thursday", "Friday"]; %%Day and location arrays for later use.
- locations = ["the bridge.", "the library.", "the river crossing.", "the airport.", "the bus terminal.", "the hospital.", "St. Petes Church"];
- intArr = (1:9);
- if(length(code) == 9) %%Test for correct code length
- sum = 0;
- for x = 1:length(code) %%Put the code into an integer array
- intArr(x) = str2double(code(x));
- end
- for i = 1:length(intArr) %%Sum all digits of the code
- sum = sum + intArr(i);
- end
- if(2*int8(sum / 2) == sum) %% Test for even code
- day = intArr(1)*intArr(3) - intArr(5);
- if(day > 0 && day <= 7) %%Test for valid day, 1-7
- if(rem(intArr(2)*intArr(4)-intArr(6), 3) == 0) %%If divisible by 3
- loc = intArr(7) - intArr(9);
- if(loc > 0 && loc <= 7) %%Test for valid location, 1-7
- disp("Rescue on " + days(day) + " at " + locations(loc)); %%Print out message using the arrays from the start.
- else
- disp("Decoy message: Invalid rendezvous point.");
- end
- else
- loc = intArr(8) - intArr(9);
- if(loc > 0 && loc <= 7)%%Test for valid location, 1-7
- disp("Rescue on " + days(day) + " at " + locations(loc)); %%Print out message using the arrays from the start.
- else
- disp("Decoy message: Invalid rendezvous point.");
- end
- end
- else
- disp("Decoy message: Invalid rescue day.");
- end
- else
- disp("Decoy message: Sum is odd.");
- end
- else
- disp("Decoy message: Not a nine-digit number.");
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement