Thursday, October 27, 2011

Tesing Array

#include <iostream>
using namespace std;

void enterArray(int* a, int size)
{
  // read and save in the array according the the size
  int i;
  for (i = 0; i < size; i++)
  {
    cout << "element " << i+1 << ": ";
    cin >> a[i];
  } // for

  // print out the content
  for (i = 0; i < size; i++)
    cout << a[i] << ' ';
  cout << endl;
}

int main()
{
  int size;
  while (size)
  {
    int* intPtr; // a pointer to the integer type
    cout << endl
         << "What is the size of the array: (0 to quit) ";
    cin >> size;
    int* score = new int[size];
    intPtr = score;
    cout << "array address: "<< *score << endl;

    enterArray(score, size);
    cout << "the pointed items: ";
    for (int i=0; i<size+4; i++)
      cout << *intPtr++ << ' ';
    delete [] score;
  }

  return 0;
} // main

No comments:

Post a Comment