Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Variables, arrays and records


1. Variables, arrays and records
Let us look at some ways to use variables.

The problem: Read in a header value and then up to three triples of numbers representing the three dimensional point (x, y, z) and compute and print the distance from the origin of each point (in reverse order, except for the separate variable approach).

2. Approaches
The following approaches are used.

3. Individual variables
One way is to use individual variables.

Note: This way does not scale up well and requires iteration without loops.

4. Arrays
Another way uses parallel arrays. Here the list starts at position 1 so MAX1+1 parallel array elements are needed.

An array is needed for each of the four variables for x1, y1, z1, d1 so that x2, y2, z2, d2, etc. are not needed.

Parallel arrays are needed but it would be nice to combine each element of each array at the same position into a record structure.

5. Records
Here is a way using structures.

The record structure is given the name Point and an array of Point elements (each with an x, y, z, and d) is declared.

Here the list starts at position 1 so MAX1+1 array elements of record structures are needed.

6. Comparison
Let us compare the three methods for double variables, array of double elements, arrays of record structures. In the case of parallel arrays of elements and arrays of records and index variable/expression can be used to abstract and generalize.

7. Visual comparison
Here are some images to depict the three ways. The question mark "?" in each variable memory location is used to show that these variables are not yet initialized with a value.

8. Individual variables
Separate variablesNote: The individual variables x0, y0, z0, and d0 are not needed, but since the other methods start the list at index 1 and not at 0, these individual variables are shown for visual comparison.

9. Parallel array of variables
Parallel arrays of variablesFour parallel arrays of variables are needed: x, y, z and d.

10. Array of record variables
Array of record variablesOne array of record variables is needed: p. Each element of array p has named fields (variables) x, y, z and d.

11. Header loop
All of the ways start by getting the header value.


12. Individual variables
The individual variable method requires iteration without a loop. See the code below to see how this can be done.

13. Input loop compare
Here is a comparison of the input loop for arrays and records.




14. Process loop compare
Here is a comparison of the process loop for arrays and records.




15. Output loop compare
Here is a comparison of the output loop for arrays and records.




16. Individual variables
Separate variablesNote: The individual variables x0, y0, z0, and d0 are not needed. See notes above. Here is the C code.


17. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


18. Parallel arrays
Parallel arrays of variablesFour parallel arrays of variables are needed: x, y, z and d. Here is the C code.


19. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


20. Records
Array of record variablesOne array of record variables is needed: p. Each element of array p has named fields (variables) x, y, z and d. Here is the C code.


21. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


22. End of page

23. Multiple choice questions for this page