:eek: hi, Im new here, anyway i'm trying to work out how to get the smallest number from the user's input of four numbers, n1,n2,n3, and n4.. im a novice @ this so i'll copy the section of the program here and you can see whats going wrong with it..i have to work out 4 things, the total, the average, the smallest number and the biggest number... :rolleyes: ;)

begin
c:='a';//First Value Which Must Be<>q While (c<>'q')and(c<>'Q') do
begin
writeln('Now Please Choose From The Following Options');
writeln('(a) Calculate The Total');
writeln('(b) Obtain The Average');
writeln('(c) Find The Biggest Number');
writeln('(d) Find The Smallest Number');
writeln('(Q) Quit..!');
readln(c);
case c of
'a','A':writeln('You Entered Option a');
total:=(n1+n2+n3+n4);
writeln('The Total is ==> ',total);
'b','B':average:=n1+n2+n3+n4 div 4;
'c','C':small:=;
'd','D':writeln('You Entered Option (d)');
'q','Q':writeln('*****G O O D B Y E + G O O D R I D E N C E*****');
else writeln('**Error!! **Error!! Inncorrect Character!! Try Again');
end;
end;
readln;
end
end
end
end

end
.

any help would be apprecaited!!!

.Rom.:mrgreen:

Dani AI

Generated

The menu idea in 's post is fine, but the snippet misses a few fundamentals: the program never reads n1..n4, a case arm with more than one statement needs a begin ... end, the average should group the sum before dividing (and use real division if fractions are wanted), and the smallest value must be assigned and updated by comparison. was right to flag the division/grouping issue.

A simple, robust approach is to read the four numbers first, then compute total, average and smallest with explicit comparisons. The function below shows a clear, compiler-friendly way to get the minimum of four integers; call it from your menu option for (d).

function MinOfFour(a, b, c, d: Integer): Integer;
var
  m: Integer;
begin
  m := a;
  if b < m then m := b;
  if c < m then m := c;
  if d < m then m := d;
  MinOfFour := m;
end;

Common pitfalls to avoid:

  • Always readln the four values before doing arithmetic.
  • Use average := (n1 + n2 + n3 + n4) / 4.0; for a real result; use div only for integer averages and wrap the whole sum in parentheses.
  • In a case branch that prints multiple lines or assigns several variables, wrap those statements in begin ... end;.
  • Declare variable types (Integer vs Real) consistently and initialize loop/menu control variables before use.

These small fixes will make each menu option work reliably and let the smallest/biggest computations be simple and readable.

Recommended Answers

All 2 Replies

'b','B':average:=(n1+n2+n3+n4) div 4;

you can verify which is the smallest and biggest number when you are reading the variables.

best regards,

thank you for that.. very helpful!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.