# MULTI-DIMENSIONAL ARRAYS

# Multi-Dimensional Arrays

**
Authored by: Lex Jacobson and John Even
**
Faculty Supervisor: Al Day

Some material derived from lessons authored by
Dr. Larry Genalo

HTML documentation by: Julie Sandberg

**
Date last updated: 7/24/96
**

# Introduction

Note: In general they are multidimensional

# Multi-Dimensional Arrays

# Subscripted Variable Rules

- Subscripted variables can be
**double**, **integer**, or
**character**.

- The maximum number of subscripts is dictated by the compiler vendor.
Twelve is common.

- Arithmetic expressions may be used for subscripts.

- Subscripts must be integer valued.

- A statement must be included in your program which sets aside
storage space (dimensions) for your arrays.

# Declaring Multidimensional
Arrays

Arrays are declared with the other variables in the program.

# Reading Arrays with 2 for
Loops

int n, m;
double x[5][6];
FILE *inp;
inp = fopen("IN.DAT","r");
for (n = 0; n < 5; ++n) {
for (m = 0; m < 6; ++m)
fscanf (inp, "%lf", &x[n][m]);
}
fclose (inp);

# Writing Arrays with 2 for
Loops

int n, m;
double x[5][6];
FILE *outp;
outp = fopen("OUT.DAT","w");
for (n = 0; n < 5; ++n) {
for (m = 0; m < 6; ++m)
fprintf (outp, "%10.4f", x[n][m]);
fprintf (outp, "\n");
}
fclose (outp);

Produces a table of output

# Example Problem

**Find the average for each row and the overall average for the entire
matrix:**

# Example Solution

# Function Rules

**When using a function:**
**Multi-dimensional** arrays must be **fully dimensioned** in the
**"calling" module**, and **partially dimensioned** in the **source function**.

As always, the name of the array may be different in each module, but it
must be in the same position in the list and agree in type

# Example 2

When passing multi-dimensional arrays, the only dimension that may be
left off, is the first dimension in the function. All the other
dimensions must match with identical sizes. If the two dimensions have
different sizes for the array, the computer will have corrupted data.

# Example 3

## ARRAY x