Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

New to matlab, in need of help.

조회 수: 1 (최근 30일)
Rand Renwar
Rand Renwar 2016년 1월 29일
마감: MATLAB Answer Bot 2021년 8월 20일
I have to write a program which asks for three different numbers that then shows the three numbers that i wrote and then adds the biggest and the smallest number that i wrote.
Example:
Write your first number: 5
Write your second number: 4
Write your third number: 8
Sum is: 12
So far I've written a program that asks for three different numbers, it looks like this:
x=input('Write your first number: ')
y=input('Write your second number: ')
z=input('Write your third number: ')
That's everything that i have written so far. I'm thinking that i would need to write some 'elses' and 'if's' which commands which of x,y,z is the largest and the smallest.
Thanks!

답변 (2개)

Ingrid
Ingrid 2016년 1월 29일
it's probably easier if you store the values in one variable, that way you do not need any if-else commands
x = zeros(3,1);
x(1)=input('Write your first number: ') ;
x(2)=input('Write your second number: ') ;
x(3)=input('Write your third number: ')
sprintf('Sum is: %1.0f', min(x) + max(x))
  댓글 수: 2
Rand Renwar
Rand Renwar 2016년 1월 29일
편집: Rand Renwar 2016년 1월 29일
Thank you, but I forgot to mention that I cannot use "sum, sprintf, min or max". I pretty much have to use if and else.
Ilham Hardy
Ilham Hardy 2016년 1월 29일
Can you use sort()?

Image Analyst
Image Analyst 2016년 1월 29일
Hint. Try this:
if x > y && x > z
theMax = x;
elseif y > x && y > z
theMax = y
and so on. Obviously use < instead of > when you want to find the min.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by