Você está na página 1de 33

Chapter 7 Single-Dimensional Arrays

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


1
Introducing Arrays
Array is a data structure that represents a collection of the
same types of data.
double myList [10];

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

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


2
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
But it would be OK, if size is a constant as follow:
const int size = 4;
double myList[size]; // Correct

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


3
Arbitrary Initial Values
When an array is created, its elements are assigned
with arbitrary values.

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


4
Indexed Variables
The array elements are accessed through the index. Array
indices are 0-based; that is, they start from 0 to arraySize-1.
In the example in Figure 7.1, myList holds ten double
values and the indices are from 0 to 9.

Each element in the array is represented using the


following syntax, known as an indexed variable:

arrayName[index];

For example, myList[9] represents the last element in the


array myList.
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
5
Using Indexed Variables
After an array is created, an indexed variable can
be used in the same way as a regular variable.
For example, the following code adds the value
in myList[0] and myList[1] to myList[2].

myList[2] = myList[0] + myList[1];

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


6
No Bound Checking
C++ does not check arrays boundary. So, accessing
array elements using subscripts beyond the
boundary (e.g., myList[-1] and myList[11]) does not
does cause syntax errors, but the operating system
might report a memory access violation.

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


7
Array Initializers
Declaring, creating, initializing in one step:
dataType arrayName[arraySize] = {value0, value1,
..., valuek};

double myList[4] = {1.9, 2.9, 3.4, 3.5};

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


8
Declaring, creating, initializing
Using the Shorthand Notation
double myList[4] = {1.9, 2.9, 3.4, 3.5};
This shorthand notation is equivalent to the
following statements:

double myList[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


9
CAUTION
Using the shorthand notation, you
have to declare, create, and initialize
the array all in one statement.
Splitting it would cause a syntax
error. For example, the following is
wrong:
double myList[4];
myList = {1.9, 2.9, 3.4, 3.5};
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
10
Implicit Size
C++ allows you to omit the array size when
declaring and creating an array using an initilizer.
For example, the following declaration is fine:

double myList[] = {1.9, 2.9, 3.4, 3.5};

C++ automatically figures out how many elements


are in the array.

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


11
Partial Initialization
C++ allows you to initialize a part of the array. For
example, the following statement assigns values
1.9, 2.9 to the first two elements of the array. The
other two elements will be set to zero. Note that if
an array is declared, but not initialized, all its
elements will contain garbage, like all other local
variables.

double myList[4] = {1.9, 2.9};

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


12
Initializing arrays with random
values
The following loop initializes the array myList with
random values between 0 and 99:

for (int i = 0; i < ARRAY_SIZE; i++)


{
myList[i] = rand() % 100;
}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


13
animation
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]={0,1,2,3,4}; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 2

values[i] = values[i] + values[i-1]; 3 3

} 4 4

values[0] = values[1] + values[4];


}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


14
animation
Trace Program with Arrays
i becomes 1

int main()
{ After the array is created

int values[5]={0,1,2,3,4}; 0 0
for (int i = 1; i < 5; i++) 1 1
{ 2 2

values[i] = values[i] + values[i-1]; 3 3

} 4 4

values[0] = values[1] + values[4];


}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


15
animation
Trace Program with Arrays
i (=1) is less than 5

int main()
{ After the array is created

int values[5] ={0,1,2,3,4}; 0 0


for (int i = 1; i < 5; i++) 1 1
{ 2 2

values[i] = values[i] + values[i-1]; 3 3

} 4 4

values[0] = values[1] + values[4];


}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


16
animation
Trace Program with Arrays
After this line is executed, value[1] is 1

int main()
{ After the first iteration

int values[5] ={0,1,2,3,4}; 0 0


for (int i = 1; i < 5; i++) 1 1
{ 2 2

values[i] = values[i] + values[i-1]; 3 3

} 4 4

values[0] = values[1] + values[4];


}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


17
animation

Trace Program with Arrays


After i++, i becomes 2

int main()
{
int values[5] ={0,1,2,3,4}; After the first iteration

for (int i = 1; i < 5; i++) 0 0

{ 1 1

2
2
values[i] = values[i] + 3 3

values[i-1]; 4 4

}
values[0] = values[1] +
values[4];
}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


18
animation
Trace Program with Arrays
i (= 2) is less than 5
int main()
{
int values[5] ={0,1,2,3,4};
for (int i = 1; i < 5; i++) After the first iteration

{ 0 0

values[i] = values[i] + 1 1

values[i-1]; 2 2

3 3
} 4
4
values[0] = values[1] +
values[4];
}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


19
animation
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,1,2,3,4}; 0 0


for (int i = 1; i < 5; i++) 1 1
{ 2 3

values[i] = values[i] + values[i-1]; 3 3

} 4 4

values[0] = values[1] + values[4];


}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


20
animation
Trace Program with Arrays
After this, i becomes 3.

int main()
{ After the second iteration

int values[5] ={0,1,2,3,4}; 0 0


for (int i = 1; i < 5; i++) 1 1
{ 2 3

values[i] = values[i] + values[i-1]; 3 3

} 4 4

values[0] = values[1] + values[4];


}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


21
animation
Trace Program with Arrays
i (=3) is still less than 5.

int main()
{ After the second iteration

int values[5] ={0,1,2,3,4}; 0 0


for (int i = 1; i < 5; i++) 1 1
{ 2 3

values[i] = values[i] + values[i-1]; 3 3

} 4 4

values[0] = values[1] + values[4];


}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


22
animation
Trace Program with Arrays
After this line, values[3] becomes 6 (3 + 3)

int main()
{ After the third iteration

int values[5] ={0,1,2,3,4}; 0 0


for (int i = 1; i < 5; i++) 1 1
{ 2 3

values[i] = values[i] + values[i-1]; 3 6

} 4 4

values[0] = values[1] + values[4];


}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


23
animation
Trace Program with Arrays
After this, i becomes 4

int main()
{ After the third iteration

int values[5] ={0,1,2,3,4}; 0 0


for (int i = 1; i < 5; i++) 1 1
{ 2 3

values[i] = values[i] + values[i-1]; 3 6

} 4 4

values[0] = values[1] + values[4];


}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


24
animation
Trace Program with Arrays
i (=4) is still less than 5

int main()
{ After the third iteration

int values[5] ={0,1,2,3,4}; 0 0


for (int i = 1; i < 5; i++) 1 1
{ 2 3

values[i] = values[i] + values[i-1]; 3 6

} 4 4

values[0] = values[1] + values[4];


}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


25
animation
Trace Program with Arrays
After this, values[4] becomes 10 (4 + 6)

int main()
{ After the fourth iteration

int values[5] ={0,1,2,3,4}; 0 0


for (int i = 1; i < 5; i++) 1 1
{ 2 3

values[i] = values[i] + values[i-1]; 3 6

} 4 10

values[0] = values[1] + values[4];


}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


26
animation
Trace Program with Arrays
After i++, i becomes 5

int main()
{
int values[5] ={0,1,2,3,4};
for (int i = 1; i < 5; i++)
{ After the fourth iteration
values[i] = values[i] + values[i-1];
} 0 0
values[0] = values[1] + values[4]; 1 1
} 2 3

3 6

4 10

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


27
animation

Trace Program with Arrays


i ( =5) < 5 is false. Exit the loop

int main()
{
int values[5] ={0,1,2,3,4};
for (int i = 1; i < 5; i++) After the fourth iteration
{
values[i] = values[i] + values[i-1]; 0 0

1 1
} 2 3

values[0] = values[1] + values[4]; 3 6

} 4 10

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


28
animation
Trace Program with Arrays
After this line, values[0] is 11 (1 + 10)

int main()
{
int values[5] ={0,1,2,3,4};
for (int i = 1; i < 5; i++) 0 11

{ 1 1

values[i] = values[i] + values[i-1]; 2 3

} 3 6

values[0] = values[1] + values[4]; 4 10

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


29
Printing arrays
To print an array, you have to print each element in the
array using a loop like the following:

for (int i = 0; i < ARRAY_SIZE; i++)


{
cout << myList[i] << " ";
}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


30
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 as follows:

for (int i = 0; i < ARRAY_SIZE; i++)


{
list[i] = myList[i];
}
Copyright 2013 by Pearson Education, Inc. All Rights Reserved.
31
Summing All Elements
Use a variable named total to store the sum. Initially total
is 0. Add each element in the array to total using a loop
like this:

double total = 0;
for (int i = 0; i < ARRAY_SIZE; i++)
{
total += myList[i];
}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


32
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, compare each element in myList with
max, update max if the element is greater than max.

double max = myList[0];


for (int i = 1; i < ARRAY_SIZE; i++)
{
if (myList[i] > max) max = myList[i];
}

Copyright 2013 by Pearson Education, Inc. All Rights Reserved.


33

Você também pode gostar