MATLAB. SOLVE WITHOUT USING CONDITIONALS OR LOOPS. Function Name: fruitEncryptio
ID: 3882057 • Letter: M
Question
MATLAB.
SOLVE WITHOUT USING CONDITIONALS OR LOOPS.
Function Name: fruitEncryption Inputs: 1. (char) MxN array of uncapitalized characters Outputs: 1. (char) updated MxN array of characters Background: You finally decoded the secret messages for APPLES, and you are about to send a message back, when you discover that the evil fruit spies have cracked your code! You have an urgent message to send, and you have to quickly figure out a new way to encrypt this message. To throw the spy off, you decide to incorporate arrays. Function Description Write a function that takes in a character array and encrypts it with the following steps: 1. Shift the characters in the even rows by the number of columns in the array 2. Shift the characters in the odd columns by the number of rows in the array, but backwards (towards 'a) Swap the top half of the array with the bottom half. 3. Note: Follow the order of shifting given above. The alphabet should 'wrap' around. When dividing the array in half, use floor() to account for the possibility that the array could have odd dimensions. Hint: The logic behind shifting character values in this problem is exactly the same as the shifting logic from coldwar().Explanation / Answer
Note :
Matlab function 'fruitEncryption' asks the user to input the matrix of characters. The function encrypts the characters according to the 3 steps given in the question and produces the output. Further clarifications are given as comments in the below code.
Matlab code :
function fruitEncryption()
% provide the input in the command window
inp = input('Input the M X N array of uncapitalized characters : ');
disp('Input matrix is : ');
disp(inp)
[r c] = size(inp); % calculating no. of rows and columns
% right rotation of even rows by the no. of columns with wrap around
inp(2:2:end,:) = 97 + mod((inp(2:2:end,:)-97+ c), 26);
% left rotation of odd columns by the no. of rows with wrap around
inp(:,1:2:end) = 97 + mod(26 + (inp(:,1:2:end) - 97 - mod(r, 26)), 26);
swapind = floor(r/2);
if (swapind == 0)
swapind = 1;
end
% swapping the top half of the array with the bottom half
temp = inp(1:swapind, :);
inp(1:swapind, :) = inp(r - swapind +1, :);
inp(r - swapind +1, :) = temp;
disp('Final output is : ');
disp(inp)
end
Sample output :
Input the M X N array of uncapitalized characters : ['z' 'z';'b' 'd']
Input matrix is :
zz
bd
Final output is :
bf
xz
Explanation of the output :
Matrix after step 1 :
zz
df
Matrix after step 2 :
xz
bf
Matrix after step 3 :
bf
xz
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.