Fundamental of Strucutred
Programming
[Link] Kamal 1
ARRAY
CHAPTER 7
[Link] Kamal 6
Arrays Hold Multiple Values
Array: variable that can store multiple
values of the same type.
Values are stored in consecutive memory
locations.
Declared using [] operator.
General Form: Datatype arrayName [SIZE];
Example: const int ISIZE = 5;
int tests[ISIZE];
[Link] Kamal 7
Array Storage in Memory
The Definition:
int tests[ISIZE];// ISIZE is 5
Allocates the following memory:
0 1 2 3 4
tests[0] tests[1] tests[2] tests[3] tests[4]
Element 1 Element 2 Element 3 Element 4 Element 5
[Link] Kamal 8
Array Terminology
In the definition int tests[ISIZE];
– int is the data type of the array elements.
– tests is the name of the array.
– ISIZE, in [ISIZE], is the size declarator.
It shows the number of elements in the array.
– The size of an array is the number of bytes
allocated for it :
(number of elements) * (bytes needed for each element)
[Link] Kamal 9
Array Terminology
[Link] Kamal 10
Array Terminology Examples
Examples:
Assumes int uses 4 bytes and double uses 8 bytes
const int ISIZE = 5, DSIZE = 10;
int tests[ISIZE]; // holds 5 ints,
array occupies 20 bytes (4*5).
double volumes[DSIZE];// holds 10
doubles,array occupies 80 bytes (10*8).
[Link] Kamal 11
Accessing Array Elements
Each array element has a subscript, used
to access the element.
Array elements accessed by arrayName
and subscript.
Subscripts start at 0
subscripts 0 1 2 3 4
– First element at position 0
– n element array c:
c[ 0 ], c[ 1 ]…c[ n - 1 ]
[Link] 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
Position number of the element within array c
[Link] 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
[Link] Kamal 14
Arrays Examples
const int SIZE = 6;
int Hight[SIZE];
[Link] Kamal 15
Inputting and Displaying
Array Contents
cout and cin can be used to display
values from and store values into an array
const int ISIZE = 5;
int tests[ISIZE];//Define 5-elt Array.
cout << "Enter first test score ";
cin >> tests[0];
[Link] Kamal 16
Inputting and Displaying
Array Contents
[Link] Kamal 17
Array Subscripts
Array subscript can be an integer constant,
integer variable, or integer expression.
Examples: Subscript is
cin >> tests[3]; int constant
cout << tests[i]; int variable
cout << tests[i+j]; int expression
[Link] Kamal 18
Accessing All Array Elements
To access each element of an array:
– Use a loop.
– Let the loop control variable be the array
subscript.
– A different array element will be referenced
each time through the loop
for (i = 0; i < 5; i++)
cout << tests[i] << endl;
[Link] Kamal 19
Array Initialization
Can be initialized during program execution
with assignment statements
tests[0] = 79;
tests[1] = 82; // etc.
Can be initialized at array definition with an
initialization list
const int ISIZE = 5;
int tests[ISIZE] ={79,82,91,77,84};
[Link] Kamal 27
Array Initialization
[Link] 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
Must use either array size declarator or
initialization list when array is defined
[Link] Kamal 32
Implicit Array Sizing
for(int val = 0 ; val<size;val++)
cout<< planets[val]<<endl;
[Link] Kamal 33
Implicit Array Sizing
// Finds size of string array : planets[]
int size = sizeof(planets)/sizeof(planets[0]);
for(int val = 0 ; val<size;val++)
cout<< planets[val]<<endl;
[Link] 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.
[Link] Kamal 35
The Range-Based for Loop
The General Form:
dataType must be the same as the data type of the
array elements.
rangeVariable is the name of the range variable.
array is the name of an array on which you wish the
loop to operate.
statement is a statement that executes during a loop
iteration.
[Link] Kamal 36
The Range-Based for Loop
The General Form:
Example:
[]
[Link] Kamal 37
The Range-Based for Loop
[Link] Kamal 38
Using Increment and Decrement
Operators with Array Elements
When using ++ and -- operators, don’t
confuse the element with the subscript
tests[i]++; // adds 1 to tests[i]
tests[i++]; // increments i, but has
// no effect on tests
// array value.
[Link] 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!
for (int count = 0; count < SIZE; count++)
newValues[count] = oldValues[count];
[Link] 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];
[Link] 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;
[Link] 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;
A similar algorithm exists to find the smallest element.
for (int tnum = 1; tnum < ISIZE; tnum++)
{ if (tests[tnum] < smallest)
smallest = tests[tnum];
}
[Link] Kamal 44
Exercise
Largest/Smallest Array Values:
Write a program that lets the user enter
10 values into an array. The program
should then display the largest and
smallest values stored in the array.
[Link] Kamal 45
Answer
[Link] Kamal 46
[Link] Kamal 49