Professional Documents
Culture Documents
Array-2015-Part 1
Array-2015-Part 1
1
"All students to receive arrays!" reports 2
Dr. Austin.
Declaring arrays
Inspecting arrays
scores : 85 79 92 57 68 80 . . .
0 1 2 3 4 5 98 99
Motivations
Often you will have to store a
large number of values during
the execution of a program.
3
4
Design Problem
Why??
5
Possible Solutions
Use arrays!!
7
Arrays
myList[0] 5.6
myList[1] 4.5
myList[2] 3.3
myList[3] 13.2
myList[4] 4.0
Array element at
myList[5] 34.33 Element value
index 5
myList[6] 34.0
myList[7] 45.45
myList[8] 99.993
myList[9] 111.23
10
11
Base Address of an Array and Array in
Computer Memory
12
Note declaration
13
Declaring Arrays
Syntax:
Data_type Array_name [constant];
Note declaration from our example
Declaring Arrays
Example specifies an array…
» each element is an integer
» there is space for 100 elements
» The indicies are numbered 0 through 99
scores : 85 79 92 57 68 80 . . .
0 1 2 3 4 5 98 99
Declaring Array Variables
datatype arrayRefVar[arraySize];
Example:
double myList[10];
C++ requires that the array size used to declare an array must be a
constant expression. For example, the following code is illegal:
int size = 4;
double myList[size]; // Wrong
scores : 85 79 92 57 68 80 . . .
0 1 2 3 4 5 98 99
Index can be:
max = scores[0];
- constant
for (x = 0; x < 100; x++)
- variable
if (scores[x] > max)
- expression
max = scores[x];
MUST be an integer
Using Indexed Variables
17
18
double myList[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
20
CAUTION
double myList[4];
myList = {1.9,
21 2.9, 3.4, 3.5};
Implicit Size
C++ allows you to omit the array size when
declaring and creating an array using an
initializer.
http://aline-costa.livejournal.com/
22
Partial Initialization
Case:
Aggregate Operations
Operations?
27
Trace Program with Arrays
Declare array variable values, create an
array, and assign its reference to values
int main()
After the array is created
{
int values[5];
for (int i = 1; i < 5; i++) 0 0
{ 1 0
values[i] = i + values[i-1]; 2 0
} 0
3
values[0] = values[1] + values[4];
4 0
}
28
Trace Program with Arrays
i becomes 1
29
Trace Program with Arrays
i (=1) is less than 5
int main()
After the array is created
{
int values[5];
for (int i = 1; i < 5; i++) 0 0
{ 1 0
values[i] = i + values[i-1]; 2 0
}
3 0
values[0] = values[1] + values[4];
4 0
}
30
Trace Program with Arrays
After this line is executed, value[1] is 1
31
Trace Program with Arrays
After i++, i becomes 2
int main()
{ After the first iteration
int values[5];
for (int i = 1; i < 5; i++) 0 0
{ 1 1
values[i] = i + values[i-1]; 2 0
} 3 0
values[0] = values[1] + 4 0
values[4];
}
32
Trace Program with Arrays
i (= 2) is less than 5
int main()
{
int values[5]; After the first iteration
for (int i = 1; i < 5; i++)
{ 0 0
1
values[i] = i + values[i-1]; 1
0
} 2
3 0
values[0] = values[1] +
values[4]; 4 0
33
Trace Program with Arrays
After this line is executed,
values[2] is 3 (2 + 1)
int main()
{ After the second iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
values[i] = i + values[i-1]; 3 0
} 4 0
34
Trace Program with Arrays
After this, i becomes 3.
int main()
{ After the second iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
values[i] = i + values[i-1]; 3 0
} 4 0
35
Trace Program with Arrays
i (=3) is still less than 5.
int main()
{ After the second iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
values[i] = i + values[i-1]; 3 0
} 4 0
36
Trace Program with Arrays
After this line, values[3] becomes 6 (3 + 3)
int main()
{ After the third iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
values[i] = i + values[i-1]; 3 6
} 4 0
37
Trace Program with Arrays
After this, i becomes 4
int main()
{ After the third iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
values[i] = i + values[i-1]; 3 6
} 4 0
38
Trace Program with Arrays
i (=4) is still less than 5
int main()
{ After the third iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
values[i] = i + values[i-1]; 3 6
} 4 0
39
Trace Program with Arrays
After this, values[4] becomes 10 (4 + 6)
int main()
{ After the fourth iteration
int values[5]; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 3
values[i] = i + values[i-1]; 3 6
} 4 10
40
Trace Program with Arrays
After i++, i becomes 5
int main()
{
int values[5]; After the fourth iteration
for (int i = 1; i < 5; i++)
{
values[i] = i + values[i-1]; 0 0
} 1 1
values[0] = values[1] + values[4]; 2 3
}
3 6
4 10
41
Trace Program with Arrays
i ( =5) < 5 is false. Exit the loop
int main()
{ After the fourth iteration
int values[5];
for (int i = 1; i < 5; i++) 0 0
{ 1 1
values[i] = i + values[i-1]; 2 3
} 3 6
values[0] = values[1] + values[4];
4 10
}
42
Trace Program with Arrays
After this line, values[0] is 11 (1 + 10)
int main()
{
int values[5]; 0 11
for (int i = 1; i < 5; i++)
{ 1 1
values[i] = values[i] + values[i-1]; 2 3
}
values[0] = values[1] + values[4]; 3 6
} 4 10
43
Printing arrays
44
Copying Arrays
Can you copy array using a syntax like this?
list = myList;
This is not allowed in
C++. You have to copy
individual elements from
one array to the other
45
Summing All Elements
double total = 0;
for (int i = 0; i < ARRAY_SIZE; i++)
{
total += myList[i];
}
46
Finding the Largest Element
Use a variable named max to store the largest element.
Initially max is myList[0]. To find the largest element in
the array myList:
47
Finding the index of the largest element
49
Shifting Elements
double temp = myList[0]; // Retain the first element
// Shift elements left
for (int i = 1; i < myList.length; i++)
{
myList[i - 1] = myList[i];
}
// Move the first element to fill in the last position
myList[myList.length - 1] = temp;
50
Problem: Lotto Numbers
Design Problem
Consider the task of
keeping track of data
about parts for
manufacture
» part number,
description, qty
needed, unit price
56
Parallel Array
Use “Parallel” arrays
One array each for part
num, descrip, qty, price partid desc qty price
nth item in any one of
A100 xxx 5 1.23 0
the arrays associated
with same nth item of all
B25 yyy 23 8.95 1
the arrays
» string partid[]
» string desc[] 2
» int qty[]
» double price[]