program Q..

2007-02-23 7:22 am
output should be..
{Enter numbers (0 to stop):
  12.7
 3.4
 8.5
  0
  Largest=12.7
 Smallest=3.4}
but i don't know why smallest always ouput 8.5 ..plz help me..
更新1:

plz use repeat loop write again to me..

更新2:

thx for your help..but i am not learing this type of programming.. sorry.i haven't said clearly.. Begin l:=0; s:=0; writeln('Enter numbers (0 to stop):'); repeat

更新3:

readln(n); if l 0 then s:=n; until n=0; writeln('Largest=',l:2:2); writeln('Smallest=',s:2:2); d:=l-s; writeln('Difference=',d:2:2) end.

回答 (3)

2007-02-23 5:43 pm
✔ 最佳答案
pascal?
pls chk following code:

program MaxMin_key;
uses crt;
var
d, l, n, s : double;
begin
l := 0;
s := 0;
writeln('Enter numbers (0 to stop)');
repeat
readln(n);
if l = 0 then
l := n;
if s = 0 then
s := n;

if n <> 0 then
begin
if l < n then
l := n;
if s > n then
s := n;
end
until n=0;
writeln('Largest=',l);
writeln('Smallest=',s);
d := l - s;
writeln('Difference=',d)
end.


finally, above code is amanded by the code which u post. ur code cannot accept the enter number is 0. is it ok?
2007-02-23 11:27 pm
just make a variable for input, largest and smallest.
you don&#39;t really have to use array for this program
Everytime you add a new number to input , you have to check
input = new number

if(new number is greater than largest)
(yes)then set new number to largest

if(new number is smaller than smallest)
(yes)then set new number to smallest

if(new number is equal to 0)
(yes)then use break to get off your loop

this is not very hard if you know how to do loop and if-else case
you should try it yourself base on these 3 cases
2007-02-23 7:38 am
用C++

/* Assume the input is not more than 100*/
/* Initially, set the biggest and smallest, you can modify them as you want*/
/* The variable &quot;loop&quot; is only used for loop control*/

float input[100], biggest = 0, smallest = 999999;
int loop = 0;

printf(&quot;Enter numbers (0 to stop):&quot;);

do{
scanf(&quot;%f&quot;,&input[loop]);

if (input[loop] == 0) break
else { if (input[loop]&gt;biggest)
biggest = input[loop];
if (input[loop]
參考: 自己


收錄日期: 2021-04-19 21:35:01
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070222000051KK06008

檢視 Wayback Machine 備份