Advanced Placement Computer Science AB:
Lecture 10:  1D and 2D C++ Arrays
    Sewickley Academy, 2000-2001

See Course Home Page.


// lecture10.cpp
//
// These lecture notes are a compilable and executable
// C++ program demonstrating the various topics in the lecture.

#include "stdafx.h"
#include <iostream.h> // for cout

//////////////////////////////////////////////////////
////////////////  Topic 1:  Declaring 1D C++ Arrays
//////////////////////////////////////////////////////

// 1. NOT ON AP EXAM (for that, you MUST use apvector)
// 2. Make array names plurals to indicate array
// 3. You may initialize fixed-size arrays with constants

const int MAX_ID = 20;
int g_ids[MAX_ID]; // ununitialized array

enum COLOR {RED, ORANGE, YELLOW, GREEN, BLUE, BLACK};

COLOR g_pantherColors[2] = {RED,BLACK};

const int MAX_PRIME_INDEX = 5;
int g_primes[MAX_PRIME_INDEX] = {2,3,5,7,11};

void topic1_demo()
{
 // no demo for topic1
}

//////////////////////////////////////////////////////
////////////////  Topic 2:  Accessing 1D C++ Arrays
//////////////////////////////////////////////////////

// 1.  0-based
// 2.  So largest index is (MAX_INDEX-1)
// 3.  So classic iterator:  for (i=0;i<max_index;i++)

void topic2_demo()
{
 const int MAX_ITEMS = 5;
 int items[MAX_ITEMS] = {1,3,5,7,9};
 int index;
 cout << "topic2_demo:" << endl;
 for (index=0; index < MAX_ITEMS; index++)
 {
  cout << "  items[" << index << "] = " << items[index] << endl;
 }
 // The following is a *bug* and will sometimes *crash* the program
 // Sometimes it will seem to work, but that's pure luck...
 items[MAX_ITEMS] = 20;
}

//////////////////////////////////////////////////////
////////////////  Topic 3:  Reversing the elements in an array
//////////////////////////////////////////////////////

// Just an example of how to use an array

// First, we need an array to reverse
const int MAX_ELEMENTS = 13;
int elements[MAX_ELEMENTS] = {1,2,3,4,5,6,7,8,9,10,11,12,13};

// Second, we need a helper function to print out an array
void printIntegerArray(char* name, int a[],int maxIndex)
{
 int index;
 const int MAX_ELEMENTS_PER_LINE = 7;
 cout << "   " << name << "[" << maxIndex << "] ="
  << endl << "     {";
 for (index=0; index < maxIndex; index++)
 {
  // 1.  Add a comma before all but first element
  if (index > 0)
   cout << ",";
  // 2.  Add a carriage return after every 7 elements
  if (((index % MAX_ELEMENTS_PER_LINE) == 0) && (index > 0))
   cout << endl << "      ";
  // 3. Print out this element
  cout << a[index];
 }
 cout << "}" << endl;
}

// Third, we need the function which does the reversing
void reverseArray(int a[],int maxIndex)
{
 int index;
 for (index = 0; index < maxIndex/2; index++)
 {
  // For each element in the first half, swap
  // it with the corresponding element in the second half.
  // First half index = index
  // Second half index = maxIndex - index - 1
  // NOTE: VERIFY THE BOUNDARY CONDITIONS
  // Part 1.  (Here we assume maxIndex is 13)
  // 1.  a[0] swaps with a[12]. Correct.
  // 2.  a[5] is the last element checked.
  //     This is correct because a[6] swaps with itself.
  // 3.  a[5] swaps with a[7].  Correct.
  // Part 2:  Do this again with an even maxIndex, say 12
  // 1.  a[0] swaps with a[11].  Correct.
  // 2.  a[5] is last element checked.  Correct.
  // 3.  a[5] swaps with a[6].  Correct.

  // So swap a[index] with a[maxIndex - index - 1]

  int index2 = maxIndex - index - 1;
  int temp = a[index];
  a[index] = a[index2];
  a[index2] = temp;
 }
}

// Finally, we need to invoke that function
void topic3_demo()
{
 cout << "topic3_demo:" << endl;
 printIntegerArray("original array",elements,MAX_ELEMENTS);
 reverseArray(elements,MAX_ELEMENTS);
 printIntegerArray("reversed array",elements,MAX_ELEMENTS);
 cout << endl;
 // Here we do it again, but with only 12 elements,
 // to demonstrate our array reversal also works for
 // an even number of elements in the array
 printIntegerArray("original array",elements,MAX_ELEMENTS-1);
 reverseArray(elements,MAX_ELEMENTS);
 printIntegerArray("reversed array",elements,MAX_ELEMENTS-1);
}
 

//////////////////////////////////////////////////////
////////////////  Topic 4:  Declaring 2D C++ Arrays (Matrices)
//////////////////////////////////////////////////////

// 1. NOT ON AP EXAM (for that, you MUST use apvector)
// 2. Same rules as 1D arrays (naming, initializing, etc)
// 3. 2D arrays are always array[row][column]

enum CHESS_PIECE {PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING};

CHESS_PIECE chessBoard[8][8];
 

const int ROWS = 2;
const int COLS = 3;

bool a[ROWS][COLS] = {
 {true, false, false},
 {false, false, true}
};

//////////////////////////////////////////////////////
////////////////  Topic 5:  Accessing 2D C++ Arrays
//////////////////////////////////////////////////////

int x[ROWS][COLS] = {
 {1,2,3},
 {-1,-2,-3}
};

void topic5_demo()
{
 // This demo prints out the declaration for x[][] above.
 // That is, it should output exactly:
 //
 // int x[ROWS][COLS] = {
 // {1,2,3},
 // {-1,-2,-3}
 //  };
 //
 cout << "topic5_demo:" << endl;
 int rowIndex, colIndex;
 cout << "int x[" << ROWS << "][" << COLS << "] = {";
 for (rowIndex=0; rowIndex < ROWS; rowIndex++)
 {
  if (rowIndex > 0)
   cout << ",";
  cout << endl << "   {";
  for (colIndex=0;colIndex < COLS; colIndex++)
  {
   if (colIndex > 0)
    cout << ",";
   cout << x[rowIndex][colIndex];
  }
  cout << "}";
 }
 cout << endl << "};" << endl;
}

//////////////////////////////////////////////////////
////////////////  Main routine
//////////////////////////////////////////////////////

int main(int argc, char* argv[])
{
 topic2_demo();
 topic3_demo();
 topic5_demo();
 getchar();
 return 0;
}


See Course Home Page.