10. Arrays
Basic Organization of a Program
Many of the problems we encounter have a certain structure. For example, a problem may first specify what the input data is. Then it describes what a desired result is, and finally in what form it should be output.
To make some order in programs that solve such problems, it is recommended to divide them in three distinct parts:
- data input,
- data processing,
- data output.
In the first part the program performs data input, where all the data is read in from whatever source; in our case the source will usually be standard input. The input data is stored in one or many variables and arrays. This allows the remainder of the program to easily access any input data it needs.
The second part of the program finds or computes the result. This is where the interesting things happen, so increasingly advanced methods of computing the result will be provided by this book. The result is then stored in one or many variables and arrays.
The final part of the program outputs the result either in a form specified by the problem, or in some convenient form whenever no particular form is specified.
It is obvious that the arrays are essential for separating the program into the three said parts. Arrays can be used to store the input data, computed result, or any other data required. The only case when arrays are not required, at least at data input part of the program, is when the input data consists only of a few values that can be stored in plain variables, as was the case in our first few programs.
This general idea of separating data input, data processing and data output in programs can be observed in the last program, which computes the minimum value. The three parts are clearly separated in the source code of the program.