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.
A 2D array example


1. A 2D array example
A 1D, one dimensional, array is an array with simple elements such as integers, characters, etc., or a simple record structure. A 2D, two dimensional, array, is an array with elements that are arrays. A 0D (zero dimensional) variable is the plane primitive variable.

2. Program requirement
The program requirement is as follows.

Read in a two dimensional table of rows and columns, square each value, and output the results.

Following good programming design, separate input, processing and output.

3. Pseudo-code
In most programs, the input and output are straight-forward (often trivial). The pseudo-code algorithm is often just the processing part - which is often not trivial. Here is the pseudo-code for the processing part.

FOR EACH cell in the table DO

4. More detailed pseudo-code
Here is a refined pseudo-code - more detailed than would normally be done. Programmers are expected to be able to fill in straight-forward details of pseudo-code.

FOR EACH row in the table DO

5. 2D array declaration
2D arrayHere is a 2D array declaration of array a that has 4 rows and 4 columns.

The variables rlen1 and clen1 are to keep track of exactly how many rows and columns of the maximum total are actually being used.

Rows are indexed from 1 to 3 and columns are indexed, within the rows, from 1 to 3. Why? It was decided to start at 1 and not at 0. If starting at 0, the code needs to be adjusted appropriately.

6. Program structure
The program is divided into input, process, and output parts.

7. Input



8. Process



9. Output


Here is the C code.


10. 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.


11. Zero dimensions
The see the pattern of 1D and 2D arrays, going the other direction, a zero dimension array can be thought of as a variable.

Here is the C code.

Here is the output of the C code.


12. End of page

13. Multiple choice questions for this page