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 Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by