Programming Fundamentals/Arrays/Defined-Value Arrays

An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.[1] This activity introduces arrays with values assigned as the arrays are defined. This activity will help you understand how to use arrays in a program.

Objectives

edit
  • Understand array concepts
  • Understand how array values may be defined in a program.
  • Use a for loop to display array values.

Prerequisites

edit

Learners should already be familiar with for loops and the syntax for defining array values in their preferred programming language.

Introduction

edit

Review the following pseudocode.

Declare String values = [“F”, “D”, “C”, “B”, “A”]

For index = 0 To 4
    Output values[index]
End For

Questions

edit
  • What syntax is used to define array elements in your preferred programming language?
  • What syntax is used to reference array elements in your preferred programming language?
  • What number is used to reference the first element in an array?
  • What number is used to reference the last element in an array?

Activity

edit

With a partner, use Repl.it or your preferred integrated development environment (IDE) to create a program based on the pseudocode above.

  1. Start by declaring an array with five values: “F”, “D”, “C”, “B”, and “A”.
  2. Create a for loop to display the array values.
  3. Save the program.
  4. Test the program to verify that it works correctly.
  5. Trade places, so that both partners have an opportunity to "drive" the programming environment.
  6. Modify the program to change the values in the array.
  7. Modify the program to increase the number of values in the array.
  8. Modify the program to decrease the number of values in the array.
  9. Experiment with using the wrong starting or ending index values in the for loop (For example, 1 to 5 rather than 0 to 4).
  10. Working together, create a list of changes that might be made to use arrays in programs you've already written. What parts could be replaced with an array?

Applications

edit
  • Identify specific steps which must be followed when creating a program using arrays.
  • Discuss your activity experience with your classmates. What surprised you? What have you learned that you can apply to your own school or work environment?

References

edit