Clear Filters
Clear Filters

Minimum and Maximum Values

2 views (last 30 days)
Raffi
Raffi on 29 Oct 2012
Answered: Swaroopa on 26 Aug 2022
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)

Answers (1)

Swaroopa
Swaroopa on 26 Aug 2022
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.

Community Treasure Hunt

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

Start Hunting!