Minimum and Maximum Values

3 visualizaciones (últimos 30 días)
Raffi
Raffi el 29 de Oct. de 2012
Respondida: Swaroopa el 26 de Ag. de 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)
  2 comentarios
Sachin Ganjare
Sachin Ganjare el 29 de Oct. de 2012
Please format you code properly first, so that others can understand.

Iniciar sesión para comentar.

Respuestas (1)

Swaroopa
Swaroopa el 26 de Ag. de 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.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by