Array
Array
Programming
FSP-Dr.Mai Kamal 1
ARRAY
CHAPTER 7
FSP-Dr.Mai Kamal 6
Arrays Hold Multiple Values
FSP-Dr.Mai Kamal 8
Array Terminology
In the definition int tests[ISIZE];
FSP-Dr.Mai Kamal 9
Array Terminology
FSP-Dr.Mai Kamal 10
Array Terminology Examples
Examples:
FSP-Dr.Mai Kamal 11
Accessing Array Elements
subscripts 0 1 2 3 4
– First element at position 0
– n element array c:
c[ 0 ], c[ 1 ]…c[ n - 1 ]
FSP-Dr.Mai Kamal 12
Accessing Array Elements
Name of array (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 int c [12];
c[5] -89
c[6] 0
c[7] 62
c[8] -3
c[9] 1
c[10] 6453
c[11] 78
FSP-Dr.Mai Kamal 13
Accessing Array Elements
Examples:
tests
0 1 2 3 4
tests[0] = 79;
cout << tests[0];
cin >> tests[1];
tests[4] = tests[0] + tests[1];
cout << tests; // illegal due to
// missing subscript
FSP-Dr.Mai Kamal 14
Arrays Examples
FSP-Dr.Mai Kamal 15
Inputting and Displaying
Array Contents
FSP-Dr.Mai Kamal 16
Inputting and Displaying
Array Contents
FSP-Dr.Mai Kamal 17
Array Subscripts
FSP-Dr.Mai Kamal 18
Accessing All Array Elements
FSP-Dr.Mai Kamal 19
Array Initialization
FSP-Dr.Mai Kamal 27
Array Initialization
FSP-Dr.Mai Kamal 28
Implicit Array Sizing
Can determine array size by the size of
the initialization list
short quizzes[]={12,17,15,11};
12 17 15 11
FSP-Dr.Mai Kamal 32
Implicit Array Sizing
FSP-Dr.Mai Kamal 33
Implicit Array Sizing
FSP-Dr.Mai Kamal 34
The Range-Based for Loop
The range-based for loop is designed to work with a
built-in variable known as the range variable.
Each time the range-based for loop iterates, it copies
an array element to the range variable.
Example, the first time the loop iterates, the range
variable will contain the value of element 0,
The second time the loop iterates, the range variable
will contain the value of the
element 1, and so forth.
FSP-Dr.Mai Kamal 35
The Range-Based for Loop
The General Form:
FSP-Dr.Mai Kamal 36
The Range-Based for Loop
The General Form:
Example:
[]
FSP-Dr.Mai Kamal 37
The Range-Based for Loop
FSP-Dr.Mai Kamal 38
Using Increment and Decrement
Operators with Array Elements
FSP-Dr.Mai Kamal 39
Copying One Array to Another
The following code defines two integer arrays:
newValues and oldValues.
newValues is uninitialized and oldValues is
initialized with 10, 100, 200, and 300.
Example:
const int SIZE = 4;
int oldValues[SIZE] = {10, 100, 200, 300};
int newValues[SIZE];
newValues = oldValues; // Wrong!
FSP-Dr.Mai Kamal 40
Copying One Array to Another
Cannot copy with an assignment
statement:
tests2 = tests; //won’t work
Must instead use a loop to copy element-
by-element:
for (int indx=0; indx < ISIZE; indx++)
tests2[indx] = tests[indx];
FSP-Dr.Mai Kamal 41
Sum, Average of Array
Elements
Use a simple loop to add together array
elements:
float average, sum = 0;
for (int tnum=0; tnum< ISIZE; tnum++)
sum += tests[tnum];
Once summed, average can be computed
average = sum/ISIZE;
FSP-Dr.Mai Kamal 43
Largest Array Element
Use a loop to examine each element and find the
largest element (i.e., one with the largest value)
int largest = tests[0];
for (int tnum = 1; tnum < ISIZE; tnum++)
{ if (tests[tnum] > largest)
largest = tests[tnum];
}
cout << "Highest score is " << largest;
FSP-Dr.Mai Kamal 45
Answer
FSP-Dr.Mai Kamal 46
FSP-Dr.Mai Kamal 49