# Multi-Dimensional Arrays

CS157 MultiDArrays

# Two-Dimensional Arrays

• A two-dimensional array, or table, consists of both rows and columns of elements
• Similar to a spreadsheet or table
• Each row is an array
```int val[3][4]; // 3 rows, 4 columns
```

# Two-Dimensional Arrays

Column 0 Column 1 Column 2 Column 3 8 16 9 52 3 15 28 6 14 25 2 10
• `val[1][3]` means row 1, column 3. There’s a 6 there.
• It’s `val[1][3]`, not `val[1,3]`.
• Rows go across.
• Columns go up & down, like Roman columns.

# Two-Dimensional Arrays

Initialization:

```#define NUMROWS 3
#define NUMCOLS 4
int val[NUMROWS][NUMCOLS] = { { 8, 16,  9, 52},
{ 3, 15, 27,  6},
{14, 25,  2, 10} };
```

First row is an array containing `{8, 16, 9, 52}`.

The inner braces can be omitted, if you have no soul:

```int val[NUMROWS][NUMCOLS] =
{8,16,9,52,3,15,27,6,14,25,2,10};
```

# Initialization

• Initialization is done in row order
• The first row, 0, is filled first, element [0][0] first, then the next row, etc:
Column 0 Column 1 Column 2 Column 3 first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth

# Two-Dimensional Arrays

```#define NUMROWS 3
#define NUMCOLS 4
int val[NUMROWS][NUMCOLS] = {{8,16,9,52},
{3,15,27,6},
{14,25,2,10}};

printf("The hard way:\n");
printf(" %2d %2d %2d %2d\n",
val[0][0], val[0][1], val[0][2], val[0][3]);
printf(" %2d %2d %2d %2d\n",
val[1][0], val[1][1], val[1][2], val[1][3]);
printf(" %2d %2d %2d %2d\n",
val[2][0], val[2][1], val[2][2], val[2][3]);

printf("The easy way:\n");
for (int i=0; i<NUMROWS; i++) {
for (int j=0; j<NUMCOLS; j++)
printf(" %2d", val[i][j]);
printf("\n");
}
```
```The hard way:
8 16  9 52
3 15 27  6
14 25  2 10
The easy way:
8 16  9 52
3 15 27  6
14 25  2 10
```

# Two-Dimensional Arrays

```#define NUMROWS 3
#define NUMCOLS 4
int val[NUMROWS][NUMCOLS] = {{8,16,9,52},
{3,15,27,6},
{14,25,2,10}};

/* Multiply each element by 10 and display it */
for (int i=0; i<NUMROWS; i++) {
for (int j=0; j<NUMCOLS; j++) {
val[i][j] *= 10;
printf(" %3d", val[i][j]);
}
printf("\n");
}
```
```  80 160  90 520
30 150 270  60
140 250  20 100
```

# Two-Dimensional Arrays

```#define NUMROWS 3
#define NUMCOLS 4

void display(int array[NUMROWS][NUMCOLS]);

int main() {
int val[NUMROWS][NUMCOLS] = {{8,16,9,52},
{3,15,27,6},
{14,25,2,10}};
display(val);
return 0;
}

void display(int nums[NUMROWS][NUMCOLS]) {
/* Multiply each element by 10 and display it */
for (int i=0; i<NUMROWS; i++) {
for (int j=0; j<NUMCOLS; j++) {
nums[i][j] *= 10;
printf(" %3d", nums[i][j]);
}
printf("\n");
}
}
```
```  80 160  90 520
30 150 270  60
140 250  20 100
```

# Common Programming Errors

• Forgetting to declare the array
• Using a subscript that refers to a nonexistent array element
• Not using a large enough conditional value in a `for` loop counter to cycle through all the array elements
• Forgetting to initialize the array
• Forgetting that arrays start at 0, not 1

# Summary of 1D arrays

• A single-dimensional array is a data structure that can store a list of values of the same data type
• Elements are stored in contiguous locations
• Referenced using the array name and a subscript
• Single-dimensional arrays may be initialized when they are declared
• Single-dimensional arrays are passed to a function by passing the name of the array as an argument

# Summary of 2D arrays

• A two-dimensional array is declared by listing both a row and a column size with the data type and name of the array
• Two-dimensional arrays may be initialized when they are declared
• Two-dimensional arrays are passed to a function by passing the name of the array as an argument

# Even Worse

• You can have more than two dimensions on arrays.
• Here be dragons.

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-22T14:04

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA