Filter löschen
Filter löschen

How to write a function that returns the amount of steps that happened before running into a wall?

1 Ansicht (letzte 30 Tage)
Im trying to write a function that lets my object take a certain number_ of _steps in its current direction tracking how many steps it took before it runs into a wall. Then I want the function to return the number of steps taken before hitting the wall. So far I have this (code below) but its not working and im not sure what i did wrong.
function walk(number_of_steps)
steps_made_successfully= number_of_steps;
if steps 0
step_made_successfully 1
return(step_made_successfully)
end

Akzeptierte Antwort

Image Analyst
Image Analyst am 26 Okt. 2022
So many things wrong with this. number_of_steps should be returned from the function, and it is not (yet). The input should be the distance from the start to the wall, and the steps length (stride distance).
function number_of_steps = walk(distanceToWall, stepLength)
number_of_steps= 0; % Initialize
distanceSoFar = 0
while distanceSoFar < distanceToWall && number_of_steps < 999999 % 999999 is the fail safe to prevent infinite loop.
% Here is where you write some more code.....
end % of while
end % of function

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by