Arrays: Outline Arrays Declaring Arrays Examples Using Arrays
Arrays: Outline Arrays Declaring Arrays Examples Using Arrays
Arrays
Outline
Introduction
Arrays
Declaring Arrays
Examples Using Arrays
Introduction
• Arrays
– Structures of related data items
– Static entity (same size throughout program)
• A few types
– Pointer-based arrays (C-like)
– Arrays as objects (C++)
Arrays
• Array
– Consecutive group of memory locations
– Same name and type (int, char, etc.)
• To refer to an element
– Specify array name and position number (index)
– Format: arrayname[ position number ]
– First element at position 0
• N-element array c
c[ 0 ], c[ 1 ] … c[ n - 1 ]
– Nth element as position N-1
Arrays
Arrays
Name of array (Note
that all elements of
this array have the
same name, c)
c[0] -45
c[1] 6
c[2] 0
c[3] 72
c[4] 1543
c[5] -89
c[6] 0
c[7] 62
c[8] -3
c[9] 1
c[10] 6453
c[11] 78
Declaring Arrays
• Initializing arrays
– Initializer list
• Specify each element when array declared
int n[ 5 ] = { 1, 2, 3, 4, 5 };
• If not enough initializers, rightmost elements 0
• If too many syntax error
– To set every element to same value
int n[ 5 ] = { 0 };
– If array size omitted, initializers determine size
int n[] = { 1, 2, 3, 4, 5 };
• 5 initializers, therefore 5 element array
Element Value
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
• Array size
– Can be specified with constant variable (const)
• const int size = 20;
– Constants cannot be changed
– Constants must be initialized when declared
– Also called named constants or read-only variables
Element Value
0 0
1 2
2 4
3 6
4 8
5 10
6 12
7 14
8 16
9 18
• Strings
– Arrays of characters
– All strings end with null ('\0')
– Examples
• char string1[] = "hello";
– Null character implicitly added
– string1 has 6 elements
• char string1[] = { 'h', 'e', 'l', 'l',
'o', '\0’ };
– Subscripting is the same
String1[ 0 ] is 'h'
string1[ 2 ] is 'l'
Multiple-Subscripted Arrays
• Multiple subscripts
– a[ i ][ j ]
– Tables with rows and columns
– Specify row, then column
– “Array of arrays”
• a[0] is an array of 4 elements
• a[0][0] is the first element of that array
Column 0 Column 1 Column 2 Column 3
Row 0 a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
Row 1 a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Row 2 a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Column subscript
Array name
Row subscript
Multiple-Subscripted Arrays
• To initialize
– Default of 0
– Initializers grouped by row in braces
1 2
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; 3 4
Row 0 Row 1
1 0
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; 3 4
Multiple-Subscripted Arrays
– Outputs 0 3 4
Multiple-Subscripted Arrays
Student0 95 85
Student1 89 80
Exercise 1:
(a) Write the command to create a two dimensional array
that stores integer numbers. The name of the array is
grade. The array stores grades of 5 courses of 10
students. (hint: rows represent students, columns
represent grades of courses).
Write the command to create an array that can store 5 )b(
integer numbers and initialize the array values with the
.numbers 10, 20, 30, 40, 50. The name of the array is value
(
Exercise 2:
Write the command to create an array that can store 5 )b(
integer numbers and initialize the array values with the
.numbers 10, 20, 30, 40, 50. The name of the array is value
Define an array a of type int with size 5 and fill this )c(
.array with values 1, 4, 7, 10, 13