// arrayFunctionDemo2B.cpp
// Attempts aggregate swap of two arrays,
// using pointer notation for parameters.
#include <iostream>
void swap(int* a, int* b, int length, ostream& out);
void printArray(const int array[], int length, ostream& out);
/*
* Demonstrates an array swap.
*
* Global variable:
* cout - an ostream object for output to the terminal.
* cout is an external variable, declared
* in library header file <iostream>.
*/
int main()
{
int array1[] = {0, 2, 4, 6, 8};
int array2[] = {1, 3, 5, 7, 9};
const int LENGTH = 5;
cout << "main: array1: ";
printArray(array1, LENGTH, cout);
cout << " array2: ";
printArray(array2, LENGTH, cout);
cout << endl;
swap(array1, array2, LENGTH, cout);
cout << "main: array1: ";
printArray(array1, LENGTH, cout);
cout << " array2: ";
printArray(array2, LENGTH, cout);
cout << endl;
return 0;
} // function main
/*
* void swap(int* a, int* b, int length)
*
* Swaps the contents of two arrays with
* the same length.
*
* Parameters:
* a - an array of integers.
* Precondition: a may contain any integers.
* Postcondition: a contains the previous
* contents of b.
* b - an array of integers, same length as a.
* Precondition: b may contain any integers.
* Postcondition: b contains the previous
* contents of a.
* length - the length of both a and b.
* out - an output stream.
* Precondition: The state of out is true.
* Postconcition: The state of out is still
* true, and out has ended a line of text.
*/
void swap(int* a, int* b, int length, ostream& out)
{
out << "Begin swap: a: ";
printArray(a, length, out);
out << " b: ";
printArray(b, length, out);
out << endl;
int* temp = a;
a = b;
b = temp;
out << "End swap: a: ";
printArray(a, length, out);
out << " b: ";
printArray(b, length, out);
out << endl;
} // function swap
/*
* void printArray(const int array[],
* int length,
* ostream& out)
*
* Prints the contents of an array of integers
* in a horizontal row, separated by spaces,
* and ends the line.
*
* Parameters:
* array - the array to be printed.
* length - length of array.
* out - an output stream.
* Precondition: The state of out is true.
* Postconcition: The state of out is still
* true, and out has ended a line of text.
*/
void printArray(const int array[], int length, ostream& out)
{
if (length > 0)
out << array[0];
for ( int i = 1; i < length; i++ )
out << " " << array[i];
out << endl;
} // function printArray