7.11.2. multtab.cpp

Time: 00:06:51 | Download: Large Small | Streaming

The text presented the first version of multtab in Chapter 3 to demonstrate nested for-loops. The following versions introduce a 12×12 two-dimensional array named table. They fill table by rows with a 12×12 multiplication table and print it to the console, demonstrating the two-dimensional array syntax. The first version defines, fills, and prints the array in main. The second version defines the array in main but passes it to functions that fill and print it, demonstrating how to pass a two-dimensional array as function arguments. Arrays are always passed by pointer, meaning they are INOUT function arguments - data can move in both directions. Finally, recall that an array's name is its address.

multtab 1

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int	table[12][12];		// rows x cols		// (a)

	for (int row = 1; row <= 12; row++)			// (b)
		for (int col = 1; col <= 12; col++)
			table[row - 1][col - 1] = row * col;


	for (int row = 0; row < 12; row++)			// (c)
	{
		for (int col = 0; col < 12; col++)
			cout << setw(4) << table[row][col];
		cout << endl;
	}

	return 0;
}
multtab: Single function version.
  1. Defines a 12 × 12 array named table: the first 12 is the number of rows, the second 12 is the number of columns.
  2. Fills the array: for each iteration of the outer loop, the inner loop runs 12 times, so the assignment statement runs 144 times. Braces are optional as the nested loops form a single statement (one semicolon terminates the three lines of code).
  3. Prints the table: the outer loop iterates 12 times. For each outer loop iteration, the inner loop iterates 12 times. Therefore, the program prints table[row][col] 144 times and end 12 times. The braces are required here as the outer loop contains two distinct statements: the inner or column loop and the output statement. The stew manipulator formats each number so that it displays right-aligned in a field four characters wide, producing a table with neatly aligned columns.

multtab 2

When a program passes an array with two or more dimensions to a function, the function can omit the size of the first dimension but must specify the sizes of the second and subsequent dimensions:

  1. void multtab(int tab[12][12])
  2. void multtab(int tab[][12])

Both versions have their respective advantages and disadvantages. Version 1 is easy to use, requiring only the array parameter. However, it can only operate on a completely filled 12×12 array. We can implement version 2 in two ways. First, it can assume the array has 12 rows, making it similar to version 1 but more vague - a reader seeing the function's prototype is unaware that the number of rows is fixed. Alternatively, we can add a second parameter specifying the number of rows. This approach has the advantage that the function can operate on arrays with any number of rows or partially filled arrays with empty rows at the bottom.

Fixed Number Of Rows Variable Number Of Rows
#include <iostream>
#include <iomanip>
using namespace std;

void fill_table(int table[][12]);
void print_table(int table[][12]);

int main()
{
    int table[12][12];       // rows x cols    // (a)

    fill_table(table);                         // (b)
    print_table(table);                        // (c)

    return 0;
}

void fill_table(int table[][12])                // (d)
{
    for (int row = 1; row <= 12; row++)
        for (int col = 1; col <= 12; col++)
            table[row - 1][col - 1] = row * col;
}

void print_table(int table[][12])                // (e)
{
    for (int row = 0; row < 12; row++)
    {
        for (int col = 0; col < 12; col++)
            cout << setw(4) << table[row][col];
        cout << endl;
    }
}
#include <iostream>
#include <iomanip>
using namespace std;

void fill_table(int table[][12], int nrows);
void print_table(int table[][12], int nrows);

int main()
{
    int table[12][12];       // rows x cols    // (a)

    fill_table(table, 12);                     // (b)
    print_table(table, 12);                    // (c)

    return 0;
}

void fill_table(int table[][12], int nrows)    // (d)
{
    for (int row = 1; row <= nrows; row++)
        for (int col = 1; col <= 12; col++)
            table[row - 1][col - 1] = row * col;
}

void print_table(int table[][12], int nrows)   // (e)
{
    for (int row = 0; row < nrows; row++)
    {
        for (int col = 0; col < 12; col++)
            cout << setw(4) << table[row][col];
        cout << endl;
    }
}
multtab: Multiple function version.
  1. Defines a 12 x 12 array named table
  2. Calls a function to fill the table. The name table is a pointer, so the program passes its address to the function. When the function ends, table is filled
  3. Calls a function to print the table.
  4. Fills the array
  5. Prints the array