Arrays are key to understanding how the computer can work


Pass Task 5.1: Hand Execution of Arrays

Overview

Arrays are key to understanding how the computer can work with large amounts of data. When you understand how arrays work you will be ready to start making more complex programs.

Purpose: Learn about how arrays work in the computer.

Task: Hand execute the provided code snippets to demonstrate key aspects of working with arrays.

Instructions

Arrays are incredibly useful, and really important to understand. Hand execution can really help you understand these more fully.

To represent an array draw a box divided into sections for each value. This represent the array's location in memory, providing you with access to the array as a whole and to the individual elements.

The following is an example of how to represent an array in your Hand Execution.

1401_figure.jpg

Demonstrate how the following code is executed in the computer.

function ???(const data: array of Integer): Integer;
var
i: Integer;
begin
result := 0;

for i := Low(data) to High(data) do begin
result := result + data[ i ];
end; end;

Hand Execute the program with the following parameter values to see if you can work out what it does.

data

Result

[6, -2, 3, 8, 1]

 

[2, 6, -1, 3]

 

Provide a name for this on your hand execution images.

The following code performs a useful tasks with an array of Integer values. What does it do?

function ???(const data: array of Integer;
val: Integer): Boolean;
var
i: Integer;
begin
result := False;

for i := Low(data) to High(data) do begin
if data[i] = val then begin
result := True;
exit; // end the function
end;
end; end;

Hand execute the function with the following parameter values.

data

val

Result

[2, 6, -3, 3, 7]

3

 

[-1, 8, 2, -4, 9]

6

 

Provide a suitable name for this function on your hand execution image you submit.

Questions

List the actions the computer executes when it runs the following code.

for i := 0 to 3 do begin
WriteLn('i is ', i);
end;

List the actions the computer executes when it runs the following code.

for i := 3 downto 0 do begin
WriteLn('i is ', i);
end;

Assume that names is an array of three names. Names array:

List the actions the computer executes when it runs the following code.
WriteLn('Array of ', Length(names), ' values'); for i := 0 to High(names) do
begin
WriteLn(names[i]);
end;

Assume that a Score record contains a name and a score, and that the scores variable is an array of three Score record values.
Scores array:

Index

Value

0

Name: Fred

Score: 10

1

Name: Wilma

Score: 20

2

Name: Betty

Score: 15

Assume you also have the following procedure:

procedure WriteScore(const toWrite: Score); begin
WriteLn(toWrite.name, ' scored ', toWrite.score);
end;

What will be output when the computer runs the following code:

WriteLn( 'Message 0' ); WriteLn( scores[1].Name ); WriteLn();

WriteLn( 'Message 1' );
for i := 0 to High(scores) do begin
WriteLn( scores[i].Name );
end;

WriteLn();
WriteLn( 'Message 2' );
for i := 0 to High(scores) do begin
WriteScore(scores[i]);
end;a

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Arrays are key to understanding how the computer can work
Reference No:- TGS01401944

Expected delivery within 24 Hours