0% found this document useful (0 votes)
6 views

Delphi Array Activity

The document provides Delphi code for displaying the top 20 songs from a text file, including their positions and movements in the charts. It outlines the `DisplaySongData` procedure that reads song names, determines their movements using the `DetermineMovement` function, and displays the results in a RichEdit component. The code also emphasizes the need to declare and initialize the `arrSongs` array with a sorted list of songs before execution.

Uploaded by

mavislossie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Delphi Array Activity

The document provides Delphi code for displaying the top 20 songs from a text file, including their positions and movements in the charts. It outlines the `DisplaySongData` procedure that reads song names, determines their movements using the `DetermineMovement` function, and displays the results in a RichEdit component. The code also emphasizes the need to declare and initialize the `arrSongs` array with a sorted list of songs before execution.

Uploaded by

mavislossie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

You have been provided with a text file,

Top20.txt, that contains the names of


the new top 20 songs ranked in order from
1 to 20.
Example of the first line of the text file:
Edges of Dawin
Write Delphi code to do the following:
Display the headings Songs, Position and
Movement for the columns in
Extract the new top 20 songs from the
text file.
the rich edit redQ4.
Compare the songs extracted from the text
file with the songs in the
sorted array arrSongs to determine the
movement of the songs in the
charts.
Display the song, position and movement
of the songs. The movement
should show the number of positions the
songs moved up, down, have
the same position or whether a new song
entered the charts.
```delphi
procedure TForm1.DisplaySongData;
var
Top20File: TextFile;
SongName: string;
Position: Integer;
Movement: string;
begin
// Display the column headings
RichEdit1.Lines.Add('Songs' + #9 +
'Position' + #9 + 'Movement');
// Extract the new top 20 songs from the
text file
AssignFile(Top20File, 'Top20.txt');
Reset(Top20File);
while not Eof(Top20File) do
begin
Readln(Top20File, SongName);
Inc(Position);
Movement :=
DetermineMovement(SongName,
Position);
RichEdit1.Lines.Add(SongName + #9 +
IntToStr(Position) + #9 + Movement);
end;
CloseFile(Top20File);
end;
function
TForm1.DetermineMovement(const
SongName: string; const Position: Integer):
string;
var
i: Integer;
begin
Result := '';
for i := 0 to High(arrSongs) do
begin
if SongName = arrSongs[i] then
begin
if i + 1 < Position then
Result := 'Moved up ' +
IntToStr(Position - (i + 1)) + ' position(s)'
else if i + 1 > Position then
Result := 'Moved down ' + IntToStr((i +
1) - Position) + ' position(s)'
else
Result := 'No movement';
Exit;
end;
end;
Result := 'New song entered the charts';
end;
```
In this code, the `DisplaySongData`
procedure reads the song names from the
"Top20.txt" file, determines their positions
and movements using the
`DetermineMovement` function, and
displays the song data in the RichEdit
component.
The `DetermineMovement` function
compares each song from the file with the
songs in the `arrSongs` array. It checks if
the song is found in the array and
calculates the movement based on the
position of the song in the array compared
to its position in the file.
Please note that you need to declare the
`arrSongs` array and initialize it with the
sorted list of songs before using the
`DisplaySongData` procedure.

You might also like