arrays
arrays
Darin Brezeale
Arrays – p. 1/18
Arrays
While single variables have many uses, there are
times in which we wish to store multiple related
values. For these we may wish to use an array.
Arrays – p. 2/18
Arrays – Declaring
The declaration for arrays is similar to the declaration
for other data types. The difference is the addition of
square brackets (i.e., []) to the declaration.
Arrays – p. 3/18
Arrays – Declaring cont.
Example of declaring and initializing an array.
double someData[3]; /* declare the array someData
that will hold 3 doubles */
Arrays – p. 4/18
Arrays – Example
Once the array has been initialized, the members can
be referenced using the array name and the index of
the member.
#include <stdio.h>
int main(void)
{
double myData[] = {3.5, 4.0, 9.34};
myData[2] = 6;
printf("The last member of myData is now %3.2f\n", myData[2]);
}
Output
The last member of myData is 9.34
The last member of myData is now 6.00
Arrays – p. 5/18
Arrays – Notes
Some comments on using arrays:
• Each element of the array will be of the same
type.
• Array indices began at 0.
Arrays – p. 6/18
Arrays – Indexing Error
This program attempts to change a value outside the
assigned range.
#include <stdio.h>
int main(void)
{
int data[3] = {6, 9, 12}; /* these are referenced as data[0],
data[1], and data[2] */
int i;
Arrays – p. 7/18
Arrays – Indexing Error cont.
Arrays – p. 8/18
More 1D Array Examples
#include <stdio.h>
int main(void)
{
/* reserve memory for 4 ints; initialize the first 2 here */
int data[4] = {6, 94}; /* data[0] and data[1] */
int i;
data[3] = 3;
Arrays – p. 9/18
Initialization Notes
If we initialize only some of the array values when the
array is declared,
• The initialized values will be at the beginning of
the array.
• The remaining values will be initialized to zero.
Arrays – p. 10/18
Arrays versus Single Variables
Why should we use arrays when we could just use
single variables? When we have many related values,
storing them in an array can make them easier to work
with.
Arrays – p. 11/18
Multidimensional Arrays
So far we have dealt with one-dimensional arrays. We
can also have arrays of arrays, also known as
multidimensional arrays.
Arrays – p. 12/18
Multidimensional Arrays cont.
A 2D array can be viewed like a table.
1 2 3
4 5 6
To create a 2D array for this table of values, we could
use
int some_data[2][3] = { {1, 2, 3},
{4, 5, 6} };
Arrays – p. 13/18
Multidimensional Arrays cont.
As with one-dimensional arrays, 2D arrays indices
begin at zero. The difference is now we have to keep
track of indices in two dimensions. So, if we create
the following array:
int sales[2][3] = { {1, 2, 3},
{4, 5, 6} };
the individual elements are referenced with the
following combinations of array name and indices:
sales[0][0] sales[0][1] sales[0][2]
sales[1][0] sales[1][1] sales[1][2]
Arrays – p. 14/18
2D Array Example
#include <stdio.h>
int main(void)
{
int i;
/* age, weight (lbs), height (in) */
int demographics[2][3] = { {24, 180, 72},
{39, 175, 65} };
Output
180
175
Arrays – p. 15/18
3D Array Example
#include <stdio.h>
int main(void)
{
/* two 2D arrays; each 2D array consists of three 1D arrays
with four elements */
int data[2][3][4] = {{{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12}},
{{13, 14, 15, 16},
{17, 18, 19, 20},
{21, 22, 23, 24}}};
int i;
Output
17 18 19 20
Arrays – p. 16/18
1D and 2D Arrays Differences
When declaring a 2D array, the number of columns
must be stated.
Example
Arrays – p. 17/18
1D and 2D Arrays Differences
In function declarations and definitions that have 2D
arrays as parameters, the number of columns must be
stated.
#include <stdio.h>
int main(void)
{
int array2D[][3] = { {4, 5, 6},
{7, 8, 9} };
printColumn(2, array2D); /* print the third column */
}