// arrayFunctionDemo3.cpp
// Demonstrates various issues pertaining
// to parameter passing with 1-dimensional arrays.
#include "textUtility2.h" // includes <iostream>
void function_A(float x);
void function_B(float& x);
void function_C(float a[], int length);
void function_D(float a[], int length);
void function_E(float a[], int length);
void function_F(float a[], int length);
void print(const char text[],
const float array[],
int length,
ostream& out);
int main()
{
float array[] = {0, 2, 4, 6, 8};
const int LENGTH = 5;
print("After initializing array: ",
array, LENGTH, cout);
function_A(array[0]);
print("After call to function_A: ",
array, LENGTH, cout);
function_B(array[1]);
print("After call to function_B: ",
array, LENGTH, cout);
function_C(array, LENGTH);
print("After call to function_C: ",
array, LENGTH, cout);
function_D(array, LENGTH);
print("After call to function_D: ",
array, LENGTH, cout);
function_E(array, LENGTH);
print("After call to function_E: ",
array, LENGTH, cout);
function_F(array, LENGTH);
print("After call to function_F: ",
array, LENGTH, cout);
return 0;
} // function main
void function_A(float x)
{
x = 10;
} // function function_A
void function_B(float& x)
{
x = 20;
} // function function_B
void function_C(float a[], int length)
{
for (int i = 0; i < length; i++)
a[i] = i;
} // function function_C
void function_D(float a[], int length)
{
float b[length];
a = b;
for (int i = 0; i < length; i++)
a[i] = i + 3;
} // function function_D
void function_E(float a[], int length)
{
float* b = a;
for (int i = 0; i < length; i++)
b[i] = 1.0 / (1 + i);
} // function function_E
void function_F(float a[], int length)
{
float* b = a + 2;
for (int i = 0; i < length - 2; i++)
b[i] = 0 - (10 + i);
} // function function_F
/*
* void print(const char text[],
* const float array[],
* int length,
* ostream& out)
*
* Prints first a C-string, then the contents
* of an array of floating-point numbers
* in a horizontal row, 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 print(const char text[],
const float array[],
int length,
ostream& out)
{
out << text;
for ( int i = 0; i < length; i++ )
printRightJustified(array[i], 2, 7, out);
out << endl;
} // function print