(Latest Revision: Sun Dec 2 23:40:18 PST 2012 ) example code

EXAMPLE CODE



Below is an example of code that writes the values in an array to the screen.
This code writes one value per line.

/* Assume that the name of the array is my_array, and that lastUsedIndex is
   the index of the last element stored in the array. */

int index ;
for (index = 0; index < lastUsedIndex+1; index++)
     cout << my_array[index] << endl ;

====================================
====================================

Here is a little program that prints a comma-separated list.  One could use
count as an index variable for an array, and print the values in the array
instead of the values of count.

#include <iostream>
using namespace std ;

int main()
{
  int count, last;
  cout << "What number would you like me to count up to? ==> " ;
  cin >> last ;
  for (count = 0; count < last; count++ )
	cout << count << ", " ;
  cout << count << "." << endl ;
  return 0 ;
}

====================================
====================================

Here is the code on page 414, defining function index_of_smallest:

int index_of_smallest (const int a[ ], int start_index, int number_used)
{
    int min = a[start_index],
    index_of_min = start_index;

    for (int index = start_index + 1; index < number_used; index++)
	if (a[index] < min)
	{
	   min = a[index];
	   index_of_min = index;
	   //min is the smallest of a[start_index] through a[index]
	}

    return index_of_min;
}

Here's an explanation of how the function would compute the minimum of the
values in a small array of integers:

array values:  | 80 | 30 | 50 | 70 | 60 | 90 | 20 | 30 | 40 |
array indexes:	  0    1    2	 3    4    5	6    7	  8

if start_index is 0,
then initially min = a[0] = 80
and index_of_min is 0

The loop starts by examining slot #1 in the array.  The 30 there is less then
the current value of min (80), so the code changes min to 30 and index_of_min
to 1.

The next 4 times through the loop, there's no change to the variables because
those array elements are not less than 30.

In the 6th iteration of the loop, min changes to 20 and index_of_min to 6.

In the last two iterations of the loop, there are no more changes.

When the loop stops, min is that element of the array which is less than or
equal to every element of the array, and index_of_min is the lowest array
index at which a copy of min is located.

====================================
====================================

Here's a function from page 407 of our text.  It computes the average of some
values in an array.

double compute_average(const int a[ ], int number_used)
{
    double total = 0;
    for (int index = 0; index < number_used; index++)
	total = total + a[index];
    if (number_used > 0)
    {
	return (total/number_used);
    }
    else
    {
	using namespace std;
	cout << "ERROR: number of elements is 0 in compute_average.\n"
	     << "compute_average returns 0.\n";
	return 0;
    }
}
====================================
====================================
====================================
====================================