// demoPracticalUseOfConstCharStuff.cc
// P. Conrad 10/14/05   Demonstrate the practical use of const char, etc.    

#include <iostream>
using namespace std;

#include <cstring>

void printLabel( const char * const label, 
		 const int width);

void printArray(const int * const array, 
		const int size,  
		const char * const arrayName)
{

  const int fieldWidth = 60; // how wide the label should be

  printLabel(arrayName, fieldWidth);
  
  for (int i=0; i<size; i++)
    {
      cout << (arrayName == NULL? "" : arrayName) 
	   << "[" << i << "]=" << array[i] << endl;
    }

}

// print a label, centered in equals signs
// print that label in a field of 60 characters.


void printLabel( const char *  const label, 
		 const int width)
{
  int numChars;

  // compute how many = to print.
  // we will print our label, if it isn't null
  // centered in the equals signs.

  if (label == NULL)
    {
      numChars = width;
    }
  else
    {
      numChars = width - strlen(label); // requires use of #include <cstring>
    }

  // print left half of the equals signs

  for (int i = 0; i<numChars/2; i++)
    cout << "=";

  // print the label, only if the pointer isn't NULL

  if (label != NULL)
    cout << label;

  // print second half of the equals signs

  for (int i = 0; i<numChars/2; i++)
    cout << "=";

  cout << endl;

}

int main(void)
{

  int a[] = {1, 2, 3, 4};
  int b[] = {2, 4, 6, 8};

  int scores[]={92,94,96,75};


  int temps[]={55, 72, 64, 53, 73};

  // print four arrays

  printArray(a, 4, "a");

  printArray(b, 4, "b");

  printArray(scores, 4, NULL); // NULL means don't print any label

  printArray(temps, 5, "Temperatures Array");




}

