Please send questions to
st10@humboldt.edu .
/*-----
Contract: main: void -> int
Purpose: provide examples of nested loops, filling and displaying
a two-dimensional array of characters
Examples: when the user runs the program fill_2_d, the following should
be printed to the screen:
a b c
d e f
g h i
j k l
by: Sharon M. Tuttle
last modified: 4-23-07
-----*/
#include <iostream>
using namespace std;
int main()
{
const int NUM_ROWS = 4;
const int NUM_COLS = 3;
char initials[NUM_ROWS][NUM_COLS];
char curr_letter = 'a';
for (int i=0; i < NUM_ROWS; i++)
{
// fill all the columns in THIS row
for (int j=0; j < NUM_COLS; j++)
{
initials[i][j] = curr_letter;
curr_letter++; // this sets curr_letter to the
// next character
}
}
// now, prove the array two_d has contents...
for (int i = 0; i < NUM_ROWS; i++)
{
for (int j = 0; j < NUM_COLS; j++)
{
cout << initials[i][j] << " ";
}
// make sure you understand WHY this is here ---
// ...so that each ROW of characters is followed by
// a newline!
cout << endl;
}
return EXIT_SUCCESS;
}