0% found this document useful (0 votes)
10 views

Array Points

Uploaded by

221090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Array Points

Uploaded by

221090
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

COMPUTER

PROGRAMMING

LECTURE # ARRAYS

1
NO BOUNDS CHECKING IN C++

 When you use a value as an array subscript, C++ does not


check it to verify that it is a valid subscript.
 In other words, you can use subscripts that are beyond the
bounds of the array.

 The original C++ language don’t provide any bound checking


CODE FROM PROGRAM 7-5
 The following code defines a three-element array, and then
writes five values to it!
WHAT THE CODE DOES
NO BOUNDS CHECKING IN C++

 Be careful not to use invalid subscripts.

 Doing so can corrupt other memory locations, crash program,


or lock up computer, and cause elusive bugs.
WATCH FOR OFF-BY-ONE ERRORS
 A common mistake is the off-by-one error .
 An off-by-one error happens when you use array subscripts that
are off by one.
 This can happen when you start subscripts at 1 rather than 0:

// This code has an off-by-one error.


During the loop’s execution, the
const int SIZE = 10; variable count takes on the values 1
int numbers[SIZE]; through 10, when it should take on the
values 0 through 9.

for (int count = 1; count <= SIZE; count++)


numbers[count] = 0;
IMPLICIT ARRAY SIZING
 Can determine array size by the size of the initialization
list:
int quizzes[]={12,17,15,11};

12 17 15 11

 Must use either array size declarator or initialization list


at array definition
PROCESSING ARRAY CONTENTS
 Array elements can be treated as ordinary variables of
the same type as the array

 When using ++, -- operators, don’t confuse the element


with the subscript:
tests[i]++; // add 1 to tests[i]

tests[i++]; // increment i, no
// effect on tests
COPYING AN ARRAY TO ANOTHER

To copy one array to another,


 Don’t try to assign one array to the other:

newTests = tests; // Won't work

 Instead, assign element-by-element:


for (i = 0; i < ARRAY_SIZE; i++)
newTests[i] = tests[i];
PRINTING THE CONTENTS OF AN ARRAY

 You can display the contents of a character array by sending


its name to cout:

char fName[] = "Henry";


cout << fName << endl;

But, this ONLY works with character arrays!


PRINTING THE CONTENTS OF AN ARRAY

 For other types of arrays, you must print element-by-element:

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


cout << tests[i] << endl;
STATEMENT EXECUTION

3 7 5 9 6

Iteration No. Value of i Statement


Execution
1st 0 Sum=sum+arr[0]
2nd 1 Sum=sum+arr[1]
3rd 2 Sum=sum+arr[2]
4th 3 Sum=sum+arr[3]
5th 4 Sum=sum+arr[4]

13
STATEMENT EXECUTION

3 7 5 9 6

Iteration No. Value of i Statement Value of


Execution sum
1st 0 Sum=sum+arr[0] 3
2nd 1 Sum=sum+arr[1] 10
3rd 2 Sum=sum+arr[2] 15
4th 3 Sum=sum+arr[3] 24
5th 4 Sum=sum+arr[4] 30

14
SUMMING AND AVERAGING
ARRAY ELEMENTS

 Use a simple loop to add together array elements:


int tests;
double average, sum = 0;
for(tnum = 0; tnum < SIZE; tnum++)
sum += tests[tnum];
 Once summed, can compute average:
average = sum / SIZE;
FINDING THE MAX VALUE IN AN ARRAY
int count;
int highest;
int SIZE = 4
highest = numbers[0];
for (count = 1; count < SIZE; count++)
{
if (numbers[count] > highest)
highest = numbers[count];
}
When this loop is finished, the highest variable will
contains the highest value in the numbers array.
FINDING THE MIN VALUE IN AN ARRAY

int count;
int lowest;
lowest = numbers[0];
for (count = 1; count < SIZE; count++)
{
if (numbers[count] < lowest)
lowest = numbers[count];
}

When this code is finished, the lowest variable will contains the lowest value in
the numbers array.
PROCESSING PARTIALLY-FILLED
ARRAYS
 Sometimes you need to store a series of items in
an array, but you do not know the number of
items that there are.
 E.g.taking a quiz in class
 Don’t know in advance about the no of students
present
 As a result, you do not know the exact number
of elements needed in the array
 One solution is to make the array large enough to
hold the largest possible number of items.
PROCESSING PARTIALLY-FILLED
ARRAYS
 This can lead to another problem, however if the
actual number of items stored in the array is less
than the number of elements
 i.e. the array will be only partially filled.
 When you process a partially filled array, you
must only process the elements that contain valid
data items
 To deal with partially filled arrays, use a counter
variable to keep track of the number of valid items
stored in the array.
SAFE USE OF PARTIALLY FILLED
ARRAYS
const int SIZE = 100;
int numbers[SIZE];
int count = 0;
Each time we add an item to the array, we must increment count .
int num;
cout << "Enter a number or −1 to quit: ";
cin >> num;
55 60 80 -1
while (num != −1 && count < SIZE)
{
numbers[count] = num;
count++;
cout << "Enter a number or −1 to quit: "; Count=3
cin >> num;
0 1 2 …… 99
} 55 60 80 ……. ……
SAFE USE OF PARTIALLY FILLED
ARRAYS
Using the count to determine the maximum array subscript
to use.
0 1 2 …… 99
55 60 80 ……. ……

Count=3

for (int index = 0; index < count;


index++)
{
cout << numbers[index] << endl;
}
SPECIAL FOR LOOP IN C++ 11
 The range-based for loop is a loop that iterates once for
each element in an array.

 Each time the loop iterates, it copies an element from the


array to a variable.

 The range-based for loop was introduced in C++ 11.


SPECIAL FOR LOOP IN C++ 11
for (dataType rangeVariable :
array )
statement;
 datatype: is the data type of the range
variable & array
 rangeVariable: is the name of the range
variable.
 This variable will receive the value of a different/next
array element during each loop iteration.
 array: name of the array
 Statement: statement that executes
SPECIAL FOR LOOP IN C++ 11
int numbers[] = { 3, 6, 9 };
for (int val : numbers)
cout << val << endl;
 Unlike
regular for loop, this loop automatically
know the no of elements in the array
 Therefore you don’t need to specify counter variable
to control its iteration as you do in regular for loop
 Stepping is also automatically carried out
SPECIAL FOR LOOP IN C++ 11
THE USE OF AUTO KEYWORD
 You can use the auto key word to specify the range
variable’s data type

int[] numbers = { 3, 6, 9 };
for (auto val : numbers)
cout << val << endl;
MODIFYING AN ARRAY WITH A RANGE-
BASED FOR LOOP
 As the range-based for loop executes, its range variable
contains only a copy of an array element.

 As a consequence, you cannot use a range-based for


loop to modify the contents of an array unless you
declare the range variable as a reference
MODIFYING AN ARRAY WITH A RANGE-
BASED FOR LOOP
 A reference variable is an alias for another value.
 Any changes made to the reference variable are actually
made to the value for which it is an alias.
 To declare the range variable as a reference variable,
simply write an ampersand ( & ) in front of its name in
the loop header
0 1 2 3 4
1 2 3 4 5
RANGE-BASED FOR VS. REGULAR FOR
 Can be used in any situation where you need to step
through the elements of an array, and you do not need to
use the element subscripts.
 It will not work, however, in situations where you need
the element subscript for some purpose.
 In those situations, you need to use the regular for loop.

You might also like