Dear Sir,

After finishing the 1st part, I still have many problem of the question.

Program Student_millionaire;

{$APPTYPE CONSOLE}

{

}

uses
  SysUtils, OurCrt;

Var User_Choice:Integer;

Procedure ContestantsList;
  Begin
    WriteLn('1 pressed,listing all the contestants...');
    ReadLn;
  End;

Procedure Generate10finalists;
  Begin
    WriteLn('2 pressed,generating the 10 finalist''s numbers...');
    ReadLn;
  End;

Procedure UpdateFinalists;
  Begin
    WriteLn('3 pressed,finding and listing the finalists...');
    ReadLn;
  End;

Procedure ChangeDetails;
  Begin
    WriteLn('4 pressed,changing the phone a friend details...');
    ReadLn;
  End;

Procedure EnrolContestant;
  Begin
    WriteLn('5 pressed,enroling a new contestant...');
    ReadLn;
  End;

Procedure GetOutOfHere;
  Begin
    WriteLn('6 pressed,exiting...');
    Sleep(1000);

  End;

Begin
   User_Choice:=0;
   While User_Choice <> 6 Do Begin
      ClrScr;
      WriteLn('Please enter an integer in the range 1...6');
      WriteLn;
      WriteLn('  1 List all the contestants');
      WriteLn('  2 Generate the 10 finalist''s numbers');
      WriteLn('  3 Find and list the finalists');
      WriteLn('  4 Change the phone a friend details');
      WriteLn('  5 Enrol a new contestant');
      WriteLn('  6 Exit');
      WriteLn;

      ReadLn(User_Choice);

      Case (User_Choice) of
         1:ContestantsList;
         2:Generate10finalists;
         3:UpdateFinalists;
         4:ChangeDetails;
         5:EnrolContestant;
         6:GetOutOfHere;
      Else
        Begin
             WriteLn('Wrong Number..');
             WriteLn('Exiting...');
         End;
      End;
    End;
End.

The 2nd part is to generate the 10 finalist's numbers. Randomly generate 10 numbers, these numbers must range from 1 to the number of people in the file. I cannot have duplicate numbers being drawn.

Cheers,

Dani AI

Generated

A practical, robust way to pick 10 unique finalists from a file is: 1) find how many records/lines (N) are in the contestants file, 2) refuse to proceed if N < 10, 3) build an index list 1..N, 4) shuffle that index list with Fisher–Yates, and 5) take the first 10 indices and read those records. was correct to point at block I/O for file access; for most Delphi/FreePascal programs using a typed file you can use FileSize/Seek/Read. The shuffle guarantees no duplicates and is O(N).

Here is a concise Pascal example you can place inside your Generate10finalists procedure (adjust TContestant to match your stored layout and the filename):

type
  TContestant = record
    Name: string[50];
    Phone: string[20];
    {other fixed-size fields...}
  end;

procedure Generate10finalists;
var
  F: file of TContestant;
  indices: array of Integer;
  rec: TContestant;
  n, i, j, tmp: Integer;
begin
  AssignFile(F, 'contestants.dat');
  Reset(F);
  n := FileSize(F);
  if n < 10 then begin
    WriteLn('Not enough contestants: ', n);
    CloseFile(F);
    Exit;
  end;
  SetLength(indices, n);
  for i := 0 to n - 1 do indices[i] := i + 1;
  Randomize;
  for i := n - 1 downto 1 do begin
    j := Random(i + 1);
    tmp := indices[i]; indices[i] := indices[j]; indices[j] := tmp;
  end;
  for i := 0 to 9 do begin
    Seek(F, indices[i] - 1);
    Read(F, rec);
    WriteLn(indices[i], ': ', rec.Name, ' ', rec.Phone);
  end;
  CloseFile(F);
end;

Troubleshooting notes: call Randomize once (not inside the selection loop). If your contestants file is text/CSV, count lines or load lines into a dynamic array and shuffle indices instead of using file of. If the binary file uses managed types (AnsiString) you must store fixed-size fields (e.g. string[50]) or use a serialization method — otherwise file of/BlockRead will not behave as expected. For very large N and limited memory the shuffle still works by shuffling only indices; as an alternative for streaming data consider reservoir sampling or generating random unique indices tracked by a boolean array.

Replace your placeholder Generate10finalists with this approach and adapt record layout and filename to match how contestants are stored.

long ago,you asked the community for a lotto games,there I send a source for the generating numbers,and one number exists only once.Try to use your head a bit,the mechanism was written.Of course that is another question that how can you read a file to array that contains records?,.
You need to learn more about the pascal language.
You have to use the BlockRead and BlockWrite procedures. See after these.

Reads one or more records from an open file into a variable.

Unit

System

Category

IO routines

Delphi syntax:

procedure BlockRead(var F: File; var Buf; Count: Integer [; var AmtTransferred: Integer]);

Description

F is an untyped file variable, Buf is any variable, Count is an expression of type Integer, and AmtTransferred is an optional variable of type Integer.

BlockRead reads Count or fewer records from the file F into memory, starting at the first byte occupied by Buf. The actual number of complete records read (less than or equal to Count) is returned in AmtTransferred.

The entire transferred block occupies at most Count * RecSize bytes. RecSize is the record size specified when the file was opened (or 128 if the record size was not specified).

If the entire block was transferred, AmtTransferred is equal to Count.

If AmtTransferred is less than Count, ReadBlock reached the end of the file before the transfer was complete. If the file's record size is greater than 1, AmtTransferred returns the number of complete records read.

If AmtTransferred isn't specified, an I/O error occurs if the number of records read isn't equal to Count. If the $I+ compiler directive is in effect, errors raise an EInOutError exception.

**** **** **** **** ****
Writes one or more records from a variable to an open file.

Unit

System

Category

IO routines

Delphi syntax:

procedure BlockWrite(var f: File; var Buf; Count: Integer [; var AmtTransferred: Integer]);

Description

F is an untyped file variable, Buf is any variable, Count is an expression of type Integer, and AmtTransferred is an optional variable of type Integer.

BlockWrite writes Count or fewer records to the file F from memory, starting at the first byte occupied by Buf. The actual number of complete records written (less than or equal to Count) is returned in AmtTransferred. 

The entire block transferred occupies at most Count * RecSize bytes. RecSize is the record size specified when the file was opened (or 128 if the record size was unspecified). 

If the entire block is transferred, AmtTransferred is equal to Count on return. 

If AmtTransferred is less than Count, the disk became full before the transfer was complete. In this case, if the file's record size is greater than 1, AmtTransferred returns the number of complete records written.

BlockWrite advances the current file position by AmtTransferred records.

If AmtTransferred isn't specified, an I/O error occurs if the number written isn't equal to Count. If the $I+ compiler directive is in effect, errors raise an EInOutError exception.
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.