Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My code fails the tests because Vectorization is not allowed...(can\'t use dot o

ID: 3554808 • Letter: M

Question

My code fails the tests because Vectorization is not allowed...(can't use dot operators, sum, min, max, colon operator..)..Also not allowed to use the mod function like my code has....Please fix my code so that it doesn't use vectorization: The requirements of the input are in the error message in the code:

CODE:

%%

function [X] = derp(Input)

if size(Input,1) > 1

error('Input must be a scalar or in a row array')

elseif(max(Input)>1*10^10)

error('All numbers in the input array must be less than 1 x 10^10');

elseif any(~mod(Input,1)==0)

error('All numbers in the input array must be integers')

end

Explanation / Answer

function main

Input=[1.5, 2.5]

derp(Input)

end

function [X] = derp(Input)

[nrow ncol]=size(Input);

is_greater=0;

for i=1:nrow

    for j=1:ncol     

        if (Input(i,j)>1*10^10)

           is_greater=1;

           break;

        end       

    end

    if (is_greater==1)

          break;

    end

end

not_integer=0;

for i=1:nrow

    for j=1:ncol   

        temp_str=num2str(Input(i,j));

        for p=1:length(temp_str)          

            if (temp_str(p)=='.')               

                not_integer=1;

                break;

            end           

        end  

        if (not_integer==1)

          break;

        end

    end

    if (not_integer==1)

          break;

    end

end

if (nrow > 1)

error('Input must be a scalar or in a row array')

end

if(nrow <= 1 && is_greater==1)

error('All numbers in the input array must be less than 1 x 10^10');

end

if (nrow <=1 && is_greater~=1 && not_integer==1)

error('All numbers in the input array must be integers')

end

end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote