Minimum and Maximum Values

3 次查看(过去 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)

回答(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