Source of ptr_ex5.cpp


  1: // Filename: PTR_EX5.CPP
  2: // Purpose:  Illustrates array/pointer parameters

  4: #include <iostream>
  5: using namespace std;

  7: #include "PAUSE.H"

  9: void MakeZero1(/* inout */ double a[], /* in */ int size)
 10: // Pre:  "size" has been initialized.
 11: // Post: All "size" values in a have been initialized to 0.0.
 12: {
 13:     for (int i = 0; i < size; i++) a[i] = 0.0;
 14: }

 16: void MakeZero2(/* inout */ double* a, /* in */ int size)
 17: // Pre:  "size" has been initialized.
 18: // Post: All "size" values in a have been initialized to 0.0.
 19: {
 20:     for (int i = 0; i < size; i++) a[i] = 0.0;
 21: }

 23: void PrintArray(/* in */ double* a, /* in */ int size)
 24: // Pre:  "a" and "size" have both been initialized.
 25: // Post: The values of "a" have been displayed, separated by one space.
 26: {
 27:     for (int i = 0; i < size; i++) cout << a[i] << " ";
 28: }


 31: int main()
 32: {
 33:     cout << "\nThis program illustrates pointers as function parameters."
 34:          << "\nStudy the source code and the output simultaneously.\n\n";

 36:     int i;
 37:     double values[10];

 39:     for (i = 0; i < 10; i++)
 40:         values[i] = i*i;
 41:     cout << endl;
 42:     PrintArray(values, 10);  cout << endl;
 43:     Pause(0);

 45:     MakeZero1(values, 10);
 46:     PrintArray(values, 10);  cout << endl;
 47:     Pause(0);

 49:     for (i = 0; i < 10; i++)
 50:         values[i] = 2.5*i;
 51:     cout << endl;
 52:     PrintArray(values, 10);  cout << endl;
 53:     Pause(0);

 55:     MakeZero2(values, 10);
 56:     PrintArray(values, 10);  cout << endl;
 57:     Pause(0);

 59:     return 0;
 60: }