Can anyone walk me through the code ..
What does this actually script display?
if in-case any error please correct those :)
x=3;
y=5;
i=2;
z=[4 5 2 6 3];
disp('start')
while i<=6
if (i>5)
disp('done')
elseif (i>3 &i~=5)
back1=confuse2(i,x,y,z);
back1=back1/z(i)
end
i=i+1;
end
function out1=confuse3(j,d,s,k)
switch (k(j))
case {1,2}
out1=d+5
case {3,4}
out1=2*s
otherwise
out1=d+j*j+2*s
end
function out2=confuse2(j,y,x,k)
temp=[1 1 1 1 1];
for i=j:5
temp(i)=k(i)+k(j);
end
out2=sum(temp)
The answer should be:
start
out2 = 24
back1 =4
done
Cheers mate ...
TY for support

VulpesPosted Oct 21, 2013, 5:50 PM
After initializing the scalar variables x, y, i and the array z, the script then displays the string 'start'.
A while loop is then entered which increments i on each iteration until i reaches 6.
Nothing happens until i reaches 4, when the condition i >3 and i ~= 5 (not equals 5) is satisfied.
The function confuse2 is then called with the following arguments:
j = 4
y = 3
x = 5
k = [4 5 2 6 3]
The for statement within that function then runs for i= j to 5 i.e. from 4 to 5.
temp(4) is set to 6 + 6 = 12
temp(5) is set to 3 + 6 = 9
out2 is then set to the sum of the elements of temp (1 + 1 + 1 + 12 + 9 = 24).
As this line isn't followed by a semi-colon (which suppresses output) the following gets output to the command window:
out2 = 24
Back in the while loop, back1 is set to the result of the function call (i.e 24).
This is then divided by z(4) which is 6 giving a result of 4.
As there's no semi-colon, the following gets printed to the command window:
back1 = 4
i is then incremented to 5 and the while loop does nothing. However, when i is finally incremented to 6, the string 'done' is displayed.