Arrays Flashcards

All 8 cards in this deck

What is an array?

A data structure that holds multiple values of the same data type in sequence, with a fixed size set when it is declared.

True or false? In the array scores, the first element is scores[1].

False. Array indexes start at 0, so the first element is scores[0].

What are the steps to declare a 1D array and store a value in it?

e.g. an array scores holding 3 numbers

  1. Declare with identifier and size: array scores[3]
  2. Assign using an index: scores[0] = 10
  3. Read it back with the same index: print(scores[0]) → 10

What are the steps to total all the elements of a 1D array?

e.g. actNumbers holding 10, 14, 6

  1. Set a total variable to 0: count = 0
  2. Loop over every index: for x = 0 to 2 … count = count + actNumbers[x] … next x
  3. Output after the loop: print(count) → 30

An algorithm totals a 3-element array using for x = 0 to 2. What must be changed so it works for a 5-element array?

Only the FOR loop line: for x = 0 to 4, keeping the same counter variable (or use the length of the array).

How is a 2D array declared and how is one element accessed?

e.g. an array score with 3 rows and 2 columns

Declared with rows and columns: array score[3,2]; an element is accessed with both indexes, e.g. score[0,1] is row 0, column 1.

How do you process every element of a 2D array?

e.g. an array with 3 rows and 2 columns

Use nested loops — an outer loop counting through the rows and an inner loop counting through the columns.

How can a 2D array be used to emulate a database table?

Each row stores one record and each column stores one field (attribute) of every record.