//
you're reading...
Uncategorized

For-Loop-Array Patterns in C++

In this lecture we look at commonly used working for-loop patterns over C++ arrays. All the examples will use a declared SIZE constant int and an array A of that declared size.

const int SIZE = 100;
int A[SIZE];

The fillpattern
Here we use a for-loop to initialize the array with random integers.

for (int i=0; i<SIZE; ++i){
    A[i] = rand();
}

The filtered-count pattern

Here is a counting pattern that involves filtering based on a predicate. When filtering, we use an if statement to decide whether we should count an item or not. Suppose we wish to count the number of times an item z is in the array:

z-count = 0;
for (int i=0; i< SIZE; ++i){
    if (A[i] == z)
        z-count = z-count + 1;
}
cout << "There are" << z-count << "array items equal to z."

Of course this is a pattern, and we could replace the if test (A[i]==z) with any predicate test – that is an expression that returns True or False.


The reduce or accumulate pattern

Suppose you wish the items in the array to be added all together. That process is called reduction or accumulation and has a simple pattern:

total = 0;  // start an accumulation at the identity
for (int i=0; i<SIZE; ++i) {
    total = total + A[i];
}
cout << "The Array total sum is" << total << endl;

For this pattern, we could replace the summation (+) with any
binary operation, for example, multiplication (*).

The filtered-reduction pattern
Let us simply compose the two patterns:

total = 0;
for (int i=0; i<SIZE; ++i) {
    if (A[i] == z)
       z-total = z-total + A[i];
}

cout << "The z-total in array is" << z-total << endl;

The extreme pattern

Suppose we wish to find the most extreme value of an item in the array, usually the largest or smallest value. Here is an approach in which we use a variable largest (or largest-so-far) that assumes the first item is the largest and then corrects that assumption with each iteration.

largest = A[0];
for (int i=0; i<SIZE; ++i) {
    if (A[i] > largest)
        largest = A[i];
}
cout << "The largest item in the array is" << largest << endl;

The extreme-index pattern

We often wish to determine a location (or index) of the most extreme value in an array of items, rather than the actual extreme value itself. As above, we will assume the first item (index zero) holds the extreme value, and iterate to correct that assumption:

l-index = 0;
largest = A[0];
for (int i=0; i<SIZE; ++i) {
    if (A[i] > largest) {
        largest = A[i];
        l-index = i;
}
cout << "The index of the largest value in arrayis" << l-index;
Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: