Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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

Respuesta aceptada

Image Analyst
Image Analyst el 26 de Oct. de 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

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by