Skip to content

Overview

Previous chapters have introduced a number programming building blocks you can create within your code. We have seen how to work with sequences and data, added control flow, organised code with functions and procedures, structured data, and used references and pointers. However, when it comes to working with data, you have been working with individual values. This chapter introduces the concepts and practices that make it easy to work with multiple values in your code.

When you have understood the material in this chapter you will be able to work with multiple values, allowing your programs to work with and manage many data values.

Data is an important part of any program. Earlier chapters have shown how to store and work with data. These chapters covered both the types of data you can work with, and means of storing and exchanging data using variables. So far, each variable has stored a single value, making it difficult to work with multiple values. This chapter introduces the concepts needed start working more effectively with multiple values.

The chapter introduces the following building blocks:

  • Array: A kind of variable that is used to store multiple values. The individual values in the array are called elements.
  • String: We will see how strings are achieved using arrays. This will help you understand the existing string type we have been using.

We will revise the following instructions to see how they work with arrays:

  • For Loop: A loop that can be used to easily repeat a block of code for each element of an array.
  • Expressions: Expressions can read the value from the element of an array.
  • Assignment Statement: The assignment statement can be used to assign a value to an element in an array.

You may need to revise the following building blocks:

  • Variable: The idea of storing data within your code.
  • Local Variable: Storing data in a function or procedure.
  • Parameter: Passing data to a function or procedure.

We will use a simple statistics calculator as an example in this chapter. Using this calculator, the user will be able to enter a number of values, and the program calculates some statistics. An example of this program executing is shown in Listing x.y

$ ./StatsCalc
How many values do you want to enter: 10
Enter value 1: Hello World
Please enter a number.
Enter value 1: 1
Enter value 2: 2
Enter value 3: 3
Enter value 4: 4
Enter value 5: 5
Enter value 6: 6
Enter value 7: 7
Enter value 8: 8
Enter value 9: 9
Enter value 10: 10
Calculating statistics...
Sum: 55.00
Mean: 5.50
Variance: 9.17
Max: 10.00
$
Statistics Calculator run from the terminal