0

I have to find out the biggest number from a txt file. Numbers are for example:

9 8 7 6 5

Someone told me, that it should works, but it didn't, and I have no clue how to work with file bcs.

program file;
uses crt;
var i,count,help:integer;

numb:array [1..9] of integer;
f:text;

begin
clrscr;

 assign(f,'file1.txt');
 reset(f);

   readln(f,count);

   for i:=1 to count do

    readln(f,numb[i]);

 close(f);

 for i:=2 to count do
  begin

   if (numb[i-1] < numb[i]) then

     help:=numb[i-1];

     numb[i-1]:=numb[i];

     numb[i]:=help;

  end;  

 for i:=1 to count do
  begin

   write(numb[i]);
  end;

readln;
end.
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
Blablabla
  • 175
  • 3
  • 9
  • 1
    First you need to clarify how the numbers are layed out in the file. Is each number on a separate line as your code suggests or are all numbers separated with a space, on the same line as you show them? Does the first number indicate how many numbers there are, again as your code suggests? Why are you swapping the numbers in your second `for` loop, if you only need to find the biggest number? Also, beware that two or more code statements that should execute together need to be in a `begin` - `end` block. – Tom Brunberg Mar 24 '19 at 11:28

2 Answers2

2

There are a few mistakes in the code provided:

  1. The program name is file. The program name cannot be a keyword;
  2. You read from the file the variable count, but the actual value cannot be found in the file, so count=0. For this reason, the for loop which reads the data from the file is never executed. You either read it from a file or from the keyboard (in the solution below, I chose the second option);
  3. You use readln when you read from the file. readln moves the cursor on the next line after it reads the data. This means that only the first number, 9, is stored into numb. Replace readln with read;
  4. At the second for loop, you say that if ... then. If you want all the three instructions to be executed (and I think you do, because it's an exchange of values), put them between begin and end. Otherwise, only the first instruction is executed if the condition is true, the others are always been executed;
  5. The method to determine the maximum is an overkill. Better if you take a variable, max, which initially gets the value of the first element in the array, then you cycle in the remaining values to see if a value is higher than max.

The final code looks like this:

    program file1;
    uses crt;
    var i,count,help, max:integer;

    numb:array [1..9] of integer;
    f:text;

    begin
     clrscr;
     assign(f,'file1.txt');
     reset(f);  
     writeln('Please input a number for count :');      
     readln(count);  
     for i:=1 to count do  
        read(f,numb[i]);
     close(f);
     max:=numb[1];
     for i:=2 to count do
       if numb[i]>max then
        max:=numb[i];
     write('The result is: ',max); 
    readln;
    end.
Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34
  • I guess the code to initialize the `max` should be `max:=numb[1]` – xmojmr Mar 25 '19 at 06:37
  • Correct. I wrote that part in a hurry. – Bogdan Doicin Mar 25 '19 at 06:38
  • If you **only** want to know the largest number and nothing else, you can actually do that without storing them in an array. Just keep a running max while reading in the numbers. Now if you need the numbers for something else anyway, then your code is fine. – Rudy Velthuis Mar 25 '19 at 07:29
  • Also: why ask the user for the count? The program should stop the loop if there are no more numbers to read. You should not have to ask the user. – Rudy Velthuis Mar 25 '19 at 07:33
  • I adapted the program according to what he wants. Your observations are on point. – Bogdan Doicin Mar 25 '19 at 12:28
2

If you only want to know the highest number, you can use a running maximum while reading the numbers in the file.

As a user you don't have to know how many numbers there are in the file. The program should determine that.

I wrote a little test file, called file1.txt:

9 8 7 6 3 11 17
32 11 13 19 64 11 19 22
38 6 21 0 37

And I only read the numbers, comparing them with Max. That is all you need.

  • No need to read the data into an array and
  • no need to (try to) sort the data. You only want the highest number, right?
  • And there is also no need for the user to know or enter the number of numbers in the text file.
program ReadMaxNumber;

uses
  Crt;

var
  Max, Num: Integer;
  F: Text;

begin
  ClrScr;
  Assign(F, 'file1.txt');
  Reset(F);

  Max := -1;
  while not Eof(F) do
  begin
    Read(F, Num);
    if Num > Max then
      Max := Num;
  end;

  Close(F);
  Writeln('Maximum = ', Max);
  Readln;
end.

When I run this, the output is as expected:

Maximum = 64
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94