Writing a script that mirrors the abs function? I'm doing something wrong but not sure what

Here is the question:
The abs function finds the absolute value of a number or numbers in a matrix.
Using only relational operators and no built-in MATLAB functions, write a script
that recreates the abs function.
Here is what I did:
prompt='Enter a number';
x=input(x)
if x>0
disp(x)
elseif x<0
disp(x*(-1))
end
Help? Please?

1 comentario

In the future, it's best to include the complete error message that you are seeing. Also, I used the CODE icon to format your code.

Iniciar sesión para comentar.

Respuestas (1)

I think you meant
x=input(prompt)

4 comentarios

Trisha wrote the following as an "answer". I moved it here as a comment on my answer.
======================
prompt='Enter a number';
x=input(prompt)
if x>0
disp(x)
elseif x<0
disp(x*(-1))
end
When I enter this, the script doesn't run past the first line. All I get is Enter a Number. How do I fix it?
I assumed that since you used the input function, you knew what it did.
The command window is waiting for you to input a number, which will then be stored as x in your program. Just type in your number, and hit Enter.
Do I set the number equal to x?
I suggest you read the documentation for the input function, to understand what it does.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 20 de Oct. de 2019

Comentada:

el 20 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by