9.0 Array
9.0 Array
1 [EEE]
Lecture 9: Array
M. Arifur Rahman
Scalar Variables versus
Aggregate Variables
• So far, the only variables we’ve seen are scalar: capable
of holding a single data item.
2
One-Dimensional Arrays
• An array is a data structure containing a number of data
values, all of which have the same type.
• These values, known as elements, can be individually
selected by their position within the array.
• The simplest kind of array has just one dimension.
• The elements of a one-dimensional array a are
conceptually arranged one after another in a single row
(or column).
3
Defining Arrays
• When defining arrays, specify
Name
Type of array (Array can be defined over any type)
Number of elements
o arrayType arrayName[numberOfElements];
Examples:
o float grade[7];
o int c[10];
• Defining multiple arrays of same type
Format similar to regular variables
Example:
o int b[ 100 ], x[ 27 ];
4
Array Elements
• To refer to an element, specify
Array name for the collection
Position number for the member
• Format:
o arrayname[position number]
First element at position 0
n element array named c:
o c[ 0 ], c[ 1 ]...c[ n – 1 ]
5
One-Dimensional Arrays
• To declare an array, we must specify the type of the
array’s elements and the number of elements:
int a[10];
• The elements may be of any type; the length of the array
can be any (integer) constant expression.
• Using a macro to define the length of an array is an
excellent practice:
#define N 10
…
int a[N];
6
Array Subscripting
• To access an array element, write the array name
followed by an integer value in square brackets.
• This is referred to as subscripting or indexing the array.
• The elements of an array of length n are indexed from 0
to n – 1.
• If a is an array of length 10, its elements are designated
by a[0], a[1], …, a[9]:
7
Array Subscripting
• Expressions of the form a[i] are lvalues, so they can be
used in the same way as ordinary variables:
a[0] = 1;
printf("%d\n", a[5]);
++a[i];
8
Array Subscripting
• Many programs contain for loops whose job is to perform
some operation on every element in an array.
• Examples of typical operations on an array a of length N:
for (i = 0; i < N; i++)
a[i] = 0; /* clears a */
10
Using Arrays: Averaging Grades
float grade[7], total;
/* initialize grades somehow */
total = grade[0] + grade[1] + grade[2] + grade[3] +
grade[4] + grade[5] + grade[6];
printf(“average = %f\n”, total/7.0);
• Or,
for (int i=0; i<7; i++)
total+= grade[i];
printf(“average = %f\n”, total/7.0);
12
Array Initialization
• If an initializer is present, the length of the array may be
omitted:
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
13
BREAK
14
Static Array Sizes
• The size of array should be static; no dynamic array in C
The following is wrong:
int x;
scanf(“%d”, &x);
int temp[x];
15
C Does Not Check Array Bounds
double grade[7];
int i = 9;
grade[i] = 3.5;
/* Is i out-of-range? If so, any error message ?*/
16
Program: Checking a Number
for Repeated Digits
• The repdigit.c program checks whether any of the digits
in a number appear more than once.
• After the user enters a number, the program prints either
Repeated digit or No repeated digit:
Enter a number: 28212
Repeated digit
The number 28212 has a repeated digit (2); a number like 9357 doesn’t
17
repdigit.c
18
Multidimensional Arrays
• An array may have any number of dimensions.
• The following declaration creates a two-dimensional
array (a matrix, in mathematical terminology):
int m[5][9];
• m has 5 rows and 9 columns. Both rows and columns are
indexed from 0:
19
Multidimensional Arrays
• To access the element of m in row i, column j, we must
write m[i][j].
• The expression m[i] designates row i of m, and m[i][j]
then selects element j in this row.
20
Multidimensional Arrays
• Although we visualize two-dimensional arrays as tables,
that’s not the way they’re actually stored in computer
memory.
• C stores arrays in row-major order, with row 0 first, then
row 1, and so forth.
• How the m array is stored:
21
Multidimensional Arrays
• Nested for loops are ideal for processing multidimensional arrays.
• Consider the problem of initializing an array for use as an identity
matrix. A pair of nested for loops is perfect:
double ident[10][10];
int row, col, N=10;
24
Initializing a Multidimensional
Array
• If an inner list isn’t long enough to fill a row, the
remaining elements in the row are initialized to 0:
int m[5][9] = {{1, 1, 1, 1, 1, 0, 1, 1, 1},
{0, 1, 0, 1, 0, 1, 0, 1},
{0, 1, 0, 1, 1, 0, 0, 1},
{1, 1, 0, 1, 0, 0, 0, 1},
{1, 1, 0, 1, 0, 0, 1, 1, 1}};
25
Initializing a Multidimensional
Array
• We can even omit the inner braces:
int m[5][9] = {1, 1, 1, 1, 1, 0, 1, 1, 1,
0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 0, 1, 1, 0, 0, 1, 0,
1, 1, 0, 1, 0, 0, 0, 1, 0,
1, 1, 0, 1, 0, 0, 1, 1, 1};
• Once the compiler has seen enough values to fill one row,
it begins filling the next.
26
End of
Lecture
27