The ++ and -- statements
a++;
is the same as a = a + 1;
a--;
is the same as a = a - 1;
The ++ operator comes in two different guises: prefix and postfix
and they return different values:
int a = 10;
x = a++;
// x is 10; a is 11 (a is incremented AFTER old value is returned)
int a = 10;
x = ++a;
// x is 11; a is 11 (a is incremented and new value is returned)
Note that the value of a is the same in both cases!!
However the value RETURNED by prefix/postfix is different...
In most cases you will use ++ just to bump up a loop variable by one
(don't use the expression if you are confused)
Concept of arrays
What if you wanted to keep track of a COLLECTION of numbers?
One solution is to create many variables to represent the
collection:
// collection of 10 integers
int a, b, c, d, e, f, g, h, i, j;
code to sum them up:
sum = a + b + c + d + e + f + g + h + i + j ;
PROBLEM: this does not generalize very well;
What if you want 20? You need to change the
program to have more variables. CLUMSY!!
What if you want 100 elements? Impossible...
C++ provides the concept of ARRAYS to deal with collections
// declare a collection of size 10
int mycollection[10];
The entire collection is referred to as "mycollection"
Each element of the 10-element collection is referred
to by using its INDEX
In effect, mycollection defines 10 different
variables, and you can refer to each by one
by using its INDEX or number:
VERY IMPORTANT POINT
===================
Array indices ALWAYS start from 0 and go to (n-1)
where "n" is the size of the array
mycollection[0] is the first element
mycollection[1] is the second element
mycollection[2] is the third element
mycollection[3] is the fourth element
mycollection[4] is the fifth element
mycollection[5] is the sixth element
mycollection[6] is the seventh element
mycollection[7] is the eighth element
mycollection[8] is the ninth element
mycollection[9] is the tenth element
// store 10 into the 6th element
mycollection[5] = 10 ;
// a gets the sum of the 5th and 6th elements
a = mycollection[4] + mycollection[5];
// an if statement to compare the 6th and 7th
// elements of an array
if (mycollection[5] == mycollection[6])
cout << "They are the same!!" << endl;
else
cout << "They are different!!" << endl;
What happens if you refer to: mycollection[11]
ARRAY INDEX OUT OF BOUNDS
=========================
This will cause your program to crash
This is an example of a RUN TIME ERROR -- something
that you need to fix
You are ONLY allowed to refer to indices that run from
0 to (n-1) where n is the size of the array
for statement
Arrays require COUNTED loops, i.e., a variable that can be
used to index into the array.
SYNTAX:
for ( initializing-statement ; condition ; increment )
statement
Example:
// store "0" into each element of the array
for (int i = 0; i <= 9 ; i++)
mycollection[i] = 0;
// same as writing the following using a while:
// the for statement is much more compact and
// ideal for use with arrays and array indices
int i = 0;
while (i <= 9)
{
mycollection[i] = 0;
i = i + 1;
}
In C++, the only operations on arrays must use indices. You are
not allowed any other operations.
Fence Post Problem: Common Programming Problem
for (int i = 1; i <= 9 ; i++)
mycollection[i] = 0;
// 1st element is ignored accidentally...
for (int i = 1; i <= 10 ; i++)
mycollection[i] = 0;
// 2 problems: 1st element ignored accidentally,
// AND array index out of bounds when i = 10
Hand simulation will help in understanding the logic of
a loop
Example of an array program:
Normal functioning
Array index out of bounds and Fence Post problems
Example reading input from a file into an array
Since you can never be sure just how many elements are in
an array, you should protect the loop properly to ensure
that the array index is not exceeded:
Sample input files for above program: