Reputation: 13
this is my first time posting so I hope I'm doing it right. I am a beginner and just wrote a simple program that should print the Minimum of 20 numbers and it's position in an array. I'm using free pascal. However if I run it (ctrl+F9) and type in the 20 numbers (with space) and confirm nothing happens. Do I have to press anything else? Or is anything wrong with the program? Thx in advance!
This the program (it has some German text):
program MinimumPositionFinder (input, output);
const
FELDGROESSE = 20;
type
tIndexPosition = 1..FELDGROESSE;
tFeld = array [tIndexPosition] of integer;
var
Feld : tFeld;
i: tIndexPosition;
Minimum : integer;
Position : tIndexPosition;
begin
writeln ('Bitte geben Sie', FELDGROESSE:4 , 'Zahlen ein.');
for i := 1 to FELDGROESSE do
readln (Feld[i]);
Minimum := Feld[1];
for i := 2 to FELDGROESSE do
if Feld[i] < Minimum then
Feld[i] := Minimum;
for i := 1 to FELDGROESSE do
if Feld[i] = Minimum then
Position := i;
Writeln ('Das Minimum ist' , Minimum:2 , '.' ,
'Es befindet sich an Position', Position:2 , '.')
end.
Upvotes: 1
Views: 226
Reputation: 1249
As a complement to Chris's answer, to find minimum of given inputs, you don't need store them in an array. Just find the minimum and its position in input loop:
program MinimumPositionFinder(input, output);
const
FELDGROESSE = 20;
type
TIndexPosition = 1..FELDGROESSE;
var
i, Position: TIndexPosition;
Feld, Minimum : integer;
begin
Minimum := MaxInt;
writeln ('Bitte geben Sie', FELDGROESSE:4 , 'Zahlen ein.');
for i := 1 to FELDGROESSE do
begin
readln (Feld);
if Feld < Minimum then
begin
Minimum := Feld;
Position := i;
end;
end;
Writeln ('Das Minimum ist' , Minimum:2 , '.' ,
'Es befindet sich an Position', Position:2 , '.')
end.
Upvotes: 0
Reputation: 36506
As stated in comments, since you are reading with readln
ensure you hit enter/return after you input each number. Alternatively, read your numbers in with read
.
You will also want to record your position at the same time you record the minimum. It's more performant, and likely more correct if you're seeking the first position of the minimum value from the left.
Minimum := Feld[1];
Position := 1;
for i := 2 to FELDGROESSE do
if Feld[i] < Minimum then
begin
Feld[i] := Minimum;
Position := i;
end
Upvotes: 1