MATLAB 1. Please download GLaDOShaywire.m This is a broken MATLAB file for a fun
ID: 3723054 • Letter: M
Question
MATLAB
1. Please download GLaDOShaywire.m This is a broken MATLAB file for a function that should be called GLaDOS.
Please fix the filename.
In addition to fixing that, you must fix a number of other things with this file.
When properly functioning, GLaDOS is an AI that is capable of performing a series of
experiments. The function GLaDOS should take one input, a number of tests, and return an
output, the result of each test.
Each test has a difficulty level, and three different indicators of success: a button, a leaver, and a
laser. The difficulty level and the status of each indicator are randomly generated. The result of
each test should be an element in an array that is either successful (1) or not successful (0).
The results of each test are determined by the following rules:
- If difficulty is 1, one or more of the indicators needs to be true to pass the test.
- If the difficulty is 2, two or more of the indicators needs to be true to pass the test
- If the difficulty is 3, all of the indicators need to be true to pass the test.
- If the difficulty is not 1, 2, or 3, the test fails.
However, none of this is clear from the haywire .m file! You need to fix it by:
- Fixing all of the variable names! The program does not consistently use the same name for
each variable. Choose new variable names that are more descriptive.
- Fix test results so they are assigned correctly.
- Improve style by adding indents and white space and adding comments to describe what
the program does (and to show that you know what it is doing).
The purpose of this exercise is to practice debugging, and to practice good style. You are also
responsible for coming up with a method for determining whether your function is working
correctly. Note that there is nothing wrong with how random number generating functions
are called in this function: you will not have to fix calls to those functions.
Explanation / Answer
function [result] = GLaDOS(numTest)
result = zeros(numTest,1);
for i = 1:numTest
difficulty = randi(3);
button = randi(2)-1;
laser = randi(2)-1;
leaver = randi(2)-1;
switch difficulty
case 3
if button && leaver && laser
testResult = 1;
else
testResult = 0;
end
case 2
if button && leaver || button && laser || leaver && laser
testResult = 1;
else
testResult = 0;
end
case 1
if button || laser || leaver
testResult = 1;
else
testResult = 0;
end
otherwise
testResult = 0;
end
result(i) = testResult;
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.