Write a function that takes numeric data as its input argument and prints a message to the Command Window stating if the number is positive, or negative, or 0. This function should transfer an output value to the Workspace ONLY when the input value is negative How do i attempt this question?

댓글 수: 3

KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 9월 1일
편집: KALYAN ACHARJYA 2018년 9월 1일
Interesting Question, Void and Output pass parameter simultaneously, I am also waiting for a response from experts.
Greg
Greg 2018년 9월 1일
This sounds like a homework question. We are not here to do it for you. If you want help, try to provide the following:
  • What have you tried so far?
  • What went wrong with it?
  • Which parts specifically do you understand?
  • Which parts specifically are you having trouble with?
melissa tan
melissa tan 2018년 9월 1일
I don't really understand what the question wants. Can you explain it to me?

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2018년 9월 1일

0 개 추천

You need to define a function with one input and one output. The function should examine the input and disp() a message saying whether the input is negative, zero, or positive.
After that, the function should assign any arbitrary value to its output variable if the input was negative (anything is acceptable.)
If the input was zero or positive and the user asked to use the output of the function, then you should expect MATLAB to generate an error message. This is to be expected considering the requirements of the question.
For example,
>> test_pos_zero_neg(-5)
input was negative
ans =
'arbitrary output'
>> test_pos_zero_neg(5)
input was positive
(notice ans was not created in this case.)
>> result = test_pos_zero_neg(-7)
input was negative
result =
'output is not required to be consistent'
>> result = test_pos_zero_neg(7)
input was positive
Output argument "variable_to_hold_arbitrary_output" (and maybe others) not assigned during call to "test_pos_zero_neg".

댓글 수: 5

melissa tan
melissa tan 2018년 9월 6일
편집: Stephen23 2018년 9월 6일
function y=number(a);
if a>0
disp('positve');
else if a<0
disp('negative'), y=a;
else
disp('zero');
end
why i cannot genereate?
[B]=number(1000)
Error: File: number.m Line: 2 Column: 4
At least one END is missing: the statement may begin here.
madhan ravi
madhan ravi 2018년 9월 6일
편집: madhan ravi 2018년 9월 6일
function y=number(a);
if a>0 disp('positve');
elseif a<0
disp('negative')
y=a;
else
disp('zero');
end
end
madhan ravi
madhan ravi 2018년 9월 6일
Your function should have one more end at the last.
Stephen23
Stephen23 2018년 9월 6일
편집: Stephen23 2018년 9월 6일
@madhan ravi: the end is not actually the problem. You already fixed the problem here:
else if a<0
^ space is not valid syntax for IF-ELSEIF-ELSE-END statement.
madhan ravi
madhan ravi 2018년 9월 6일
Thank you @Stephen

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2018년 9월 1일

편집:

2018년 9월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by