Minimum and Maximum Values

조회 수: 2 (최근 30일)
Raffi
Raffi 2012년 10월 29일
답변: Swaroopa 2022년 8월 26일
I am trying to display the minimum and maximum values of my program. My program allows the user to input a bunch of numbers and it calculates the average. I am not allowed to use arrays or vectors and I am having trouble getting the minimum and maximum statements to work. Minimum keeps reading 0 and maximum does not work. Thanks appreciate it This is my program:
i=0;
a=99;
b=99;
Minimum=0;
Maximum=0;
for counter=1:stop
Numbers=input('Enter a number');
i=i+Numbers;
if Numbers>=a; if true
Maximum=Numbers;
else Maximum=Numbers;
Numbers<=b;
Minimum=Numbers;
end
a=Numbers;
b=Numbers;
Average=i/stop
fprintf('The minimum value is %g. The maximum value is %g',Minimum,Maximum)
  댓글 수: 2
Sachin Ganjare
Sachin Ganjare 2012년 10월 29일
Please format you code properly first, so that others can understand.

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

답변 (1개)

Swaroopa
Swaroopa 2022년 8월 26일
Hi Raffi,
You can try the code in the following way.
i=0;
stop=3;
Numbers=input('Enter a number');
maximum=Numbers;
minimum=Numbers;
for counter=1:stop-1
Numbers=input('Enter a number');
i=i+Numbers;
if Numbers>=maximum
maximum=Numbers;
else Numbers<=minimum
minimum=Numbers;
end
end
a=Numbers;
b=Numbers;
Average=i/stop;
fprintf('The minimum value is %g. The maximum value is %g',minimum,maximum)
Hope it helps.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by