`
` BedWiley
`
`|
`
`| a ie
`ae
`oy
`be Pt
`heave.tea a.
`
`uf}
`
`Harness the Programming Power of C++ in the OOP
`Paradigm, and Build High-Performance C++ Apps
`for Any Operating System — Including Windows® 95
`
`Learn How to Exploit the New Standard Template
`Library to Optimize Your DevelopmentEfforts
`Microsoft Corp. Fatt 1043
`
` 5
`
`i.
`
`Tn
`
`Lda es ee)
`
`IDG
`| BOOKS
`
`Microsoft Corp. Exhibit 1043
`
`
`
`a Oriented NAMIR C. SHAMMAS
`
`Object—
`
`Microsoft Corp. Exhibit 1043
`
`
`
`Foundations of C++ and Object-Oriented Programming
`Published by
`IDG Books Worldwide, Inc.
`An International Data Group Company
`919 E. Hillsdale Blvd.
`Suite 400
`Foster City, CA 94404
`Text and art copyright © 1995 by IDG Books Worldwide,Inc. All rights reserved. No part of this book,
`includinginterior design, cover design, and icons, may be reproducedor transmitted in any form, by any
`means (electronic, photocopying, recording, or otherwise) without the prior written permission of the
`publisher.
`Library of Congress Catalog Card No.: 95-83094
`ISBN: 1-56884-709-2
`Printed in the United States of America
`
`10987654321
`1B/SX/RR/ZV
`
`Distributed in the United States by IDG Books Worldwide, Inc.
`Distributed by Macmillan Canada for Canada; by Computer and Technical Booksfor the
`Caribbean Basin; by Contemporanea de Ediciones for Venezuela; by Distribuidora Cuspide for Argentina; by
`CITECfor Brazil; by Ediciones ZETA $.C.R. Ltda. for Peru; by Editorial Limusa SA for Mexico, by Transworld
`Publishers Limited in the United Kingdom and Europe; by Al-Maiman Publishers & Distributors for Saudi
`Arabia; by Simron Pty. Ltd. for South Africa; by IDG Communications (HK) Ltd. for Hong Kong; by Toppan
`CompanyLtd. for Japan; by Addison Wesley Publishing Company for Korea; by Longman Singapore Publishers
`Ltd. for Singapore, Malaysia, Thailand, and Indonesia; by Unalis Corporation for Taiwan; by WS Computer
`Publishing Company,Inc. for the Philippines; by WoodsLane Pty. Ltd. for Australia, by WoodsLane Enterprises
`Ltd. for New Zealand.
`:
`
`For general information on IDG Books Worldwide’s books in the U.S., please call our Consumer Customer
`Service department at 800-762-2974. Forreseller information,including discounts and premium sales, please
`call our Reseller Customer Service department at 800-434-3422.
`For information on where to purchase IDG Books Worldwide’s books outside the U.S., contact IDG Books
`Worldwide at 415-655-3021 or fax 415-655-3295.
`For information ontranslations, contact Marc Jeffrey Mikulich, Director, Foreign & Subsidiary Rights, at IDG
`Books Worldwide, 415-655-3018 or fax 415-655-3295.
`Forsales inquiries and special prices for bulk quantities, write to the address above orcall
`IDG Books Worldwide at 415-655-3200,
`
`For information on using IDG Books Worldwide’s books in the classroom, or ordering examination copies,
`contact Jim Kelly at 800-434-2086.
`For authorization to photocopyitems for corporate, personal, or educational use, please contact Copyright
`Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, or fax 508-750-4470.
`Limitof Liability/Disclaimer ofWarranty: The author and publisher have usedtheir best efforts in
`preparing this book. IDG Books Worldwide, Inc., and the author make no representation or warranties with
`respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied
`warranties of merchantability or fitness for any particular purpose and shall in no eventbeliable for any loss
`of profit or any other commercial damage, including but notlimited to special, incidental, consequential, or
`other damages.
`Trademarks: All brand names and product names used in this book are trademarks, registered trademarks, or
`trade namesoftheir respective holders. IDG Books Worldwideis not associated with any product or vendor
`1
`mentioned in this book.
`
`WORLDWIDE
`
`i—— are trademarks under exclusive license
`IDG to IDG Books Worldwide, Inc., from
`International Data Group,Inc.
`BOOKS
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER
`= 5
`
`
`
`Multidimensional
`Arrays
`
`A™ support in C++ is not limited to single-dimensional arrays. Rather,
`it extends to multidimensional arrays. This chapter looks at declaring,
`accessing, andinitializing multidimensional arrays. In addition, the chapter
`discusses declaring multidimensional arrays as function parameters. You will
`also learn aboutsorting multidimensional arrays using the combsort method,
`as well as searching multidimensional array elements using the linear and
`binary search methods.
`
`Declaring Multidimensional Arrays
`C++ requires that you declare a multidimensional array before you use
`it. The general syntax for declaring a multidimensionalarrayis:
`
`type arrayName| numberOfElement1][numberOfElement2). a5
`
`The above syntax showsthe following aspects:
`
`= The declaration starts by stating the basic type associated with the array
`elements. You can use predefined or previously defined data types.
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`Part Il ¢ THE BASICS OF C++
`
`# The name of the array is followed by a sequence of the number of
`elements for the various dimensions. These numbers appearin square
`brackets. Each number of elements must be a constant(literal or
`symbolic) or an expression that uses constants.
`
`All multidimensional arrays in C++ have indices that start at 0. Thus, the
`numberof array elements in each dimension is one value higher than the
`index of the Jast element in that dimension.
`Here are examples of declaring multidimensionalarrays:
`
`// example 1
`int nIntCube[20][10][5];
`// example 2
`const
`int MAX_ROWS = 50;
`const
`int MAX_COLS = 20;
`double fMatrix[(MAX_ROWS ][MAX_COLS];
`// example 3
`const
`int MAX_ROWS = 30;
`const int MAX_COLS = 10;
`char
`cNameArray[MAX_ROWS+1][MAX_COLS];
`
`The first example declares the int-type three-dimensional array nintCube
`with 20 by 10 by 5 elements. The declaration uses theliteral constants 20,
`10, and 5. Thus,the indicesfor the first dimension are in the range of 0 to
`19, the indices for the second dimension are in the range of 0 to 9, and the
`indices for the third dimension are in the range of0 to 4.
`The second example declares the constants MAX_ROWSand
`MAX_COLS and uses these constants to specify the number of rows and
`columns of the double-type matrix fMatrix.
`The third example declares the char-type matrix cNameArray. The
`constant expression MAX_ROWS+ 1 defines the number of rowsin the
`array cNameArray. The constant MAX_COLS defines the number of
`columnsin the array cNameArray.
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER 11 © MULTIDIMENSIONAL ARRAYS a Ww
`
`Accessing Multidimensional Arrays
`
`Once you declare a multidimensional array, you can access it elements
`using the index operator []. The general syntax for accessing an elementin
`a multidimensionalarray is:
`
`arrayName{ IndexOfDimensionl][IndexOfDimensionz]...
`
`The indices for the various dimensions should be in the valid ranges—
`between 0 and the numberof array elements for the dimension minus one.
`Here is an example of accessing multidimensional array elements:
`
`const int MAX_ROWS=10;
`
`const int MAX_COLS=20;
`double fMatrix[MAX_ROWSJ[EMAX_COLS];
`for (int i = @;
`i
`< MAX_ROWS;
`i++)
`for (int j = @;
`j
`< MAX_COLS;
`j++)
`fMatrixLiJ[j] = double(2 + i
`2 * j)
`
`This code snippet declares the constants MAX_ROWS and MAX_COLS
`and uses these constants in declaring the double-type two-dimensional array
`fMatrix. Thus the array has rows with indices in the range of 0 to
`MAX_ROWS — 1 and columns with indices in the range of 0 to MAX_COLS
`- 1. The code snippet uses nested for loopsto initialize the elements ofthe
`array fMatrix. Notice that the last assignment statement uses the syntax
`fMatrix{i][j] and not fMatrix{i, j] or fMatrix(i, j) as is the case in Pascal and
`BASIC, respectively.
`
`Declaring and Accessing Multidimensional Arrays
`C++ requiresthatyou enclose eachdimensionin a pairof brackets. Thisstylediffersfrom BASIC’s
`syntax, which places the comma-delimited fist of dimensionsin a single pair of parentheses.
`
`|
`|
`|
`
`|
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`9 [370 | ©
`
`Part I © THE BASICS OF C++
`
`—
`Declaring and Accessing Multidimensional Arrays
`
`7
`
`C++ requires that you enclose every dimension in a pair of brackets. This style differs from
`Pascal's syntax, which places the comma-delimitedlist of dimensionsin a single pair of brackets.
`
`Let’s look at a programming example. Listing 11-1 shows the source
`code for the MDARRAY1.CPP program, whichillustrates declaring and
`accessing multidimensional arrays. This exampleillustrates the following
`features:
`
`= Declaring and accessing C++ arrays
`
`= Declaring classes that support multidimensional arrays
`
`The example shows how youdeclare the operator () to access the
`elements of a multidimensional array in a class. Why use the operator()
`instead of the operator []? The answeris that C++ does not allow the
`operator[] to have more than one integer-compatible parameter. When you
`are storing and recalling array elements, then, the choice becomeseither to
`use memberfunctions or to use the operator(), which allows multiple
`indexing parameters. This operator is called the iterator operator;it is
`supposed to be used to sequentially access elements in data structures, such
`aslists.
`
`The program performs the following tasks:
`
`= Declare a C++ matrix and a matrix object
`
`=
`
`Initialize a C++ matrix
`
`« Assign the square root values of the C++ matrix elements to the
`elements of the matrix object
`
`# Display the elements of the matrix object
`= Double the values of the elements in the matrix object
`# Display the elements of the matrix object
`
`Here is the output of the program in Listing 11-1:
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER 11 ¢ MULTIDIMENSIONAL ARRAYS a i |
`
`Listing 11-1
`The source code for the MDARRAY1.CPP program, whichillustrates declaring
`and accessing multidimensionalarrays.
`// A C++ program that illustrates declaring
`// and accessing multidimensional matrix elements
`
`#include <iostream.h>
`#include <stdio.h>
`#include <iomanip.h>
`#include <math.h>
`
`const
`int MAX_ROWS
`const int MAX_COLS Ii]
`
`WW
`
`class myMatrix
`{
`
`public:
`myMatrix(double fInitVal = @);
`double& operator()(int nRow,
`int nCol)
`{
`return m.fMatrix[nRow][nCol];
`}
`void show(const char* pszMsg = "",
`const int nNumRows =
`MAX_ROWS,
`const int nNumCols = MAX_COLS);
`
`protected:
`double m_fMatrix(MAX_ROWS][MAX_COLS];
`
`Matrix
`
`1 m
`
`{
`
`yMatrix::myMatrix(double fInitVal)
`
`i++)
`< MAX_ROWS;
`7
`for (int i = 0;
`j
`< MAX_COLS;
`j++)
`for (int j = 0;
`m_fMatrixLilLj] = flnitval;
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`«= =[Eg= =
`
`Part Il ¢ THE BASICS OF C++
`
`void myMatrix::show(const char* pszMsg,
`const int nNumRows,
`const int nNumCols)
`
`{
`
`char cString[10];
`
`cout << pszMsg << endl;
`{
`i++)
`for (int i = @;
`i
`< nNumRows;
`{
`for (int j = @;
`3
`< nNumCols;
`j++)
`sprintf(cString,
`"%3.11f ", m_fMatrixLiJ[j]);
`cout << cString;
`
`} c
`
`out << endl;
`
`}
`
`ain()
`
`} m
`
`{
`
`double fXMat{MAX_ROWS J[MAX_COLS];
`myMatrix Matrix;
`inti, j:
`
`Matrix
`
`// initialize matrix fXMat
`i++)
`for (i = @;
`i
`< MAX_ROWS;
`for (j = 0;
`j
`< MAX_COLS;
`j++)
`fXMatliJ[j] = 2.0 + (double)(i * j);
`
`// assign values to object Matrix
`for (7 = @;
`i
`< MAX_ROWS;
`i++)
`for (j = @;
`J
`< MAX_COLS;
`j++)
`Matrix(i, J) = sqrt(fXMatliJ[j]);
`
`// show the matrix
`Matrix.show("Initial matrix 1s:");
`
`cout << end];
`
`// double the values in object Matrix
`for (i
`= 0;
`1
`< MAX_ROWS;
`i++)
`for (j = @;
`j
`< MAX_COLS;
`j++)
`Matrix(i, J) t= Matrix(i,
`j);
`
`// show the matrix
`Matrix.show("After doubling values, matrix 1s:");
`
`return @;
`
`
`Listing 11-1 declares the constants MAX_ROWS and MAX_COLS, which
`specify the number of rows and columns, respectively, in the program’s
`matrices. The listing declares class myMatrix, which has the protected data
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER 11 ¢ MULTIDIMENSIONAL ARRAYS | i
`
`member m_fMatrix. This memberis a double-type matrix that has
`MAX_ROWSrows and MAX_COLS columns. The class declares the
`following public members:
`
`= The constructor, which initializes the values of the elements of the data
`member m_fMatrix. The default argumentof parameterflnitValinitializes
`the matrix elements to 0.
`
`« The operator (), which accesses the elements of data member
`m_fMatrix. Notice that the memberfunction has the double& return type
`Ginstead of simply double). Using a reference return type allows you to
`utilize the operator() on either side of the assignment operator. The
`parameters nRow and nColspecify the row and column indices of the
`accessed matrix element.
`
`= The memberfunction show, which displays someorall of the matrix
`elements. The parameters nNumRows and nNumColsspecify the
`number of matrix rows and columns, respectively, to display. The
`parameter pszMsg passes an accompanying message that appears
`before the function lists the matrix elements.
`
`The function main declares the double-type matrix fXMat and the object
`Matrix as an instance of class myMatrix. The declaration initializes the
`elements of the object Matrix to 0. By contrast, the elements of the C++
`matrix fXMathavenoinitial values (except for the arbitrary values in their
`memory locations). The function main then performs the following tasks:
`
`= Assign valuesto the elements of matrix fXMat. This task uses nested for
`loops, which iterate overall the elements of matrix fXMat. Each inner
`loop statementassigns the expression 2.0 + (double}(i + j) to the element
`fXMat{i][j]. The matrix fXMat and the object Matrix have the same
`numberof elements.
`
`= Assign new values to the elements of the object Matrix. This task uses
`nested for loops, which iterate over the elements of object Matrix. Each
`inner loopiteration assigns the square root value of element fXMat{i][j]
`to element Matrix(i, j). Notice that the loop’s statement places Matrix(i, |)
`to the left side of the assignment operator.
`® Display the elements in object Matrix by sending it the C++ message
`show. The argument for this messageistheliteral string “Initial matrix is:”
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`Part Il © THE BASICS OF C++
`
`Double the values in the elements of object Matrix. This task uses
`nested for loops, which iterate over the elements of object Matrix. Each
`inner loop iteration doubles the value of element MatrixG, j) using the
`operator +=. Notice that the loop’s statement places Matrix(, j) to the
`left and right sides of the assignment operator.
`Display the elements in object Matrix by sending it the C++ message
`show. The argumentfor this message is the literal string “After doubling
`values, matrix is:”.
`
`Initializing Multidimensional Arrays
`C++ allows youto initialize some orall of the elements of a multidimen-
`sional array. The general syntax forinitializing a multidimensionalarrayis:
`
`type arrayName[ numberOfElement1](numberOfElement2] =
`
`{
`
`value@ ,...,
`valueN };
`
`Youneed to observe the following rules when youinitialize a multidi-
`mensional array:
`
`. Thelist of initial values appears in a pair of open and close braces and
`is comma-delimited.
`
`. The list may contain a numberofinitial values that is equal to orless
`than the total number of elements in theinitialized array. Otherwise, the
`compiler generates a compile-time error.
`. The compilerassignsthe initializing values in the sequence discussed in
`the sidebar “Initializing Multidimensional Arrays.”
`_ If the list contains fewer values than the numberof elements in the
`array, the compiler assigns zeros to the elements that do notreceive
`initial values from thelist.
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER 11 © MULTIDIMENSIONAL ARRAYS
`
`= «[B@)* =
`
`
`
`Initializing Multidimensional Arrays
`
`How doesthe C++ compiler copy the items in
`a linearlist of initializing values into the ele-
`ments of a multidimensional array? The an-
`sweris that the compilerfills up the elements
`at the higher dimension number(represented
`by the index to the right) before the onesat the
`lower dimension number. Let me explain this
`storage schemeusing thefollowing example.
`Considerthe array fMat, which has three rows
`and two columns:
`
`fMat(0](0)
`fMatlO)1]
`Seale
`fMat(2][0]
`fMat{2)C1)
`
`You can apply the samerule to, say, the array
`fCube[3][2][2] and obtain the following se-
`quenceofinitialized elements:
`
`|
`
`fCube(0J(0I(0]
`double fMat(31(21:
`Nea
`The dimension for the rowsis the low dimen-
`eamestat _
`sion number, whereas the dimension for the
`fCube(1][01f1]
`columnsis the high dimension number. The
`fCube(1][1][0)]
`element fMat{O][0] is the first array element.
`fCubef1]£1]01)
`Thus, the compiler stores the first initializing a eae
`valuein elementfMat{0][0]. The compilerthen
`“ado[2
`i ,
`stores the secondinitializing value in element
`fCubel2]([1]f1]
`fMat{O][1]. The compilerstoresthethird initial-
`izing value in element fMat{1][0], and so on.
`Here is the sequenceofstoring theinitializing
`values in the matrix fMaft:
`
`Here are examples of initializing multidimensional arrays:
`
`// example 1
`double fMat(3][2] = { 1.1,
`// example 2
`iat mMatbodbel
`
`=
`
`{
`
`2; 88m AHA O55 6.6 8
`
`le 2. 3, 45°5 be
`
`The code in example 1 declares the double-type two-dimensional array
`fMatto have three rows and two columns. The declarationalso initializes all
`six elements with the values 1.1, 2.2, 3.3, 4.4, 5.5, and 6.6. The compiler
`stores these values sequentially in elements fMat[O][O], fMai{O][1], and so
`on. The second example declares the int-type matrix nMat to have five rows
`and two columns. The declaration also initializes only the first five elements
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`= =[BQ= =
`
`PART Il © THE BASICS OF C++
`
`(nMat{O][O], nMat{O][1], nMat{1 ][0], nMat{1][1], and nMat{2][O}) with the
`values 1, 2, 3, 4, and 5. Therefore, the compiler assigns zeros to the
`remaining matrix elements.
`Let me present a programming example. Listing 11-2 contains the
`source code for the MDARRAY2.CPP program, whichillustrates initializing
`multidimensional arrays. The program illustrates the following features:
`
`® Declaring andinitializing multidimensional arrays in C++. The program
`also demonstrates using the numberofinitializing values to establish the
`number of array elements.
`Initializing a data memberthat is a multidimensional C++ array. C++
`offers no syntaxto directly initialize data membersthat are arrays.
`
`»
`
`The program declares andinitializes two C++ arrays and an array object.
`It performs the following tasks:
`
`® Display the initial values in the array object
`=
`Incrementthe values in the array object using the values in the two C++
`arrays
`= Display the new values in the array object
`
`Here is the output of the program in Listing 11-2:
`
`mw
`
`omSs
`
`Listing 11-2
`The source code for the MDARRAY2.CPP program, whichillustrates initializing
`multidimensionalarrays.
`// A C++ program that illustrates initializing
`// multidimensional matrix elements
`
`dHinclude <iostream.h>
`#include <stdio.h>
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER 11 © MULTIDIMENSIONAL ARRAYS
`
`= mlEyZ4|= =
`
`#include <iomanip.h>
`#include <math.h>
`
`const
`const
`
`int MAX_ROWS
`
`int MAX_COLS aa amPo
`
`class myMatrix
`{
`
`public:
`myMatrix(int bUseInitArray = 1, double fInitVal = 0);
`double& operator()(int nRow,
`int nCol)
`{
`return m_fMatrixCnRow][nCol];
`}
`void show(const char* pszMsg = “",
`const int nNumRows=MAX_ROWS,
`const int nNumCols=MAX_COLS);
`
`protected:
`double m_fMatrix[MAX_ROWS]EMAX_COLS];
`
`Ne
`
`myMatrix::myMatrix(int bUseInitArray, double fInitVal)
`{
`
`" Initialized matrix
`
`oo
`{
`if (bUseInitArray)
`double fInitMat[MAX_ROWSJ[MAX_COLS] =
`{Tike BG, WCE Gull
`lOn Fe
`20 Sasa Bel, 705 Bed ils
`i++)
`for (int 1 = 0;
`1
`< MAX_ROWS:
`for (int 3 = @;
`j
`< MAX_COLS;
`j++)
`m_fMatrix[i][j] = flnitMatlil(jl;
`
`}e
`
`lse
`i++)
`< MAX_ROWS;
`i
`for (int i = @;
`j
`< MAX_COLS;
`j++)
`for (int j = 0;
`m_fMatrixLil(j] = fInitval;
`
`oid myMatrix::show(const char* pszMsg,
`const int nNumRows,
`const int nNumCols)
`
`char cString[10];
`
`cout << pszMsg << endl;
`{
`i++)
`for (int i = @;
`i
`< nNumRows;
`{
`for (int j = @;
`j
`< mNumCols;
`j++)
`Sprintf(cString, "43.11f ", m_fMatrixli]fj]);
`cout << cString;
`
`} c
`
`out << endl;
`
`} v
`
`}
`
`}
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`e = |EZj= =
`
`PART Il © THE BASICS OF C++
`
`Matrix
`
`main()
`{
`
`double fXMat[2][MAX_ROWS][MAX_COLS] =
`{
`// data for sub-array fMatfO][J{)
`19.9, 29.3, 87.6, 43.2, 76.7,
`32.9, 43.8, 88.7, 87.5, 64.5,
`// data for sub-array fMat(1I](J(]
`111.9, 222.3, 777.6, 333.2, 676.7,
`222.9, 333.8, 688.7, 787.5, 484.5 |;
`myMatrix Matrix;
`
`// show the matrix
`Matrix.show( "Initial matrix is:");
`
`cout << endl;
`
`// update matrix Matrix
`i++)
`for (int i = 0;
`i
`< MAX_ROWS;
`for (int j = @;
`j
`< MAX_COLS;
`j++)
`Matrix(i, J) = fXMatClICiJ£j]
`/
`
`// show the matrix
`Matrix.show("New matrix 1S:");
`
`fXxMat(@Jlil(jl;
`
`return @;
`
`
`Listing 11-2 declares the constants MAX_ROWS and MAX_COLS, which
`define the number of rows and columnsof the matrices used in the
`program. The listing declares the class myMatrix, whichis similar to that in
`Listing 11-1. The main difference is that the new class version has a
`constructorthat allows youtoinitialize the matrix in one of two ways:
`Whenthe argument ofthe parameter bUselnitMatrix is 1 (which is the
`default argument) or any nonzero value, the constructor uses the local
`initialized C++ matrix flnitMat to provide theinitializing values for the
`elements of data member m_fMatrix. The constructor declares the matrix
`flnitMat to have MAX_ROWSrows and MAX_COLS columns.If the
`parameter bUselnitMatrix is 0, the constructor assigns the value of parameter
`flnitVal to each element in the data member m_fMatrix.
`The function main declares andinitializes the three-dimensional C++
`matrix fXMat. This array conceptually models two matrices glued to each
`other. The declaration of matrix fXMat specifies a dual matrix with
`MAX_ROWSrows and MAX_COLScolumns. The function main also
`declares the object Matrix as an instance of class myMatrix. The way the
`source code declares object Matrix ends up using the default argument for
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER 11 © MULTIDIMENSIONAL ARRAYS | |
`
`the constructor of class myMatrix. Consequently, the runtime system
`initializes data member m_fMatrix in object Matrix using the matrix flnitMat.
`The function main then performs the following tasks:
`
`= Display the initial elements in the object Matrix by sending it the C++
`message show. The argumentfor this message is the string literal “Initial
`matrix is:”.
`
`= Update the values in object Matrix. This task uses nested for loops,
`whichiterate over the elements of the matrices. Each inner loop
`iteration assignsthe ratio of fXMat{1][i][j] to XMat{O]fi][j], to the element
`Matrix(i, j).
`« Display the new values of the elements in the object Matrix by sending
`it the C++ message show. The argumentfor this messageis the string
`literal “New matrix is:”.
`
`Declaring Multidimensional Arrays
`as Function Parameters
`
`C++ allows you to declare multidimensional arrays as parameters in
`functions. The programming language supports two syntaxes, one for fixed
`arrays and the other for open arrays:
`
`// fixed array parameter
`type parameterName[ numberOfElement1|[number0fElement2]...
`// open-array parameter
`type parameterName[ ][numberOfElement2]..
`
`Thefixed-array parameterstates the numberof elements for each
`dimension. By contrast, the open array parameterlists the number of
`elements for the second dimension and up. In other words, the open
`parameter leaves the number of elements forthe first dimension unspecified.
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`i | PART Il © THE BASICS OF C++
`
`Open MultidimensionalArray Parameters
`
`C++ puts restrictions on the level of generality of open multidimensional array parameters. Only
`thefirst dimensionofthe arrayis variable. Thus, C++ can allow you to write matrix manipulating
`functions that deal with matrices that have a variable number of rows buta fixed number of
`columns.This feature is morelimited than that in modern implementations of BASIC that support
`truly general-purpose matrix parameters.
`
`Here are examples of declaring multidimensional array parameters:
`
`double mySum(double fMyMatrix[MAX_ROWS1][MAX_COLS]);
`double theirSum(double fTheirMatrix[MAX_ROWS2][MAXCOLS],
`int nNumRows = MAX_ROWS1,
`int nNumCols = MAX_COLS);
`double yourSum(double fYourMatrix£J[MAX_COLS],
`int nNumRows,
`int nNumCols);
`
`The function mySum declares the matrix parameter f{MyMatrix and
`specifies that the parameter has MAX_ROWS1 rows and MAX_COLS
`columns. This kind of function expects the caller to pass a double-type
`matrix that also has MAX_ROWS1 rows and MAX_COLS columns.Since the
`function’s parameterlist does not have parameters that pass the number of
`rows and columnsto process,it is safe to assumethat function mySum
`processesall of the elements in parameter f{MyMatrix.
`The function theirSum has three parameters. Thefirst one is the matrix
`parameter fTheirMatrix and specifies MAX_ROWS2 rows and MAX_COLS
`columns. The second and third parameters, nNumRows and nNumCols,
`specify the number of rows and columns, respectively, to process. Thus, the
`first parameter suggests that the arguments for parameter fTheirMatrix must
`be double-type matrices with MAX_ROWS2 rows and MAX_COLS columns.
`However, the presence of parameters nNumRows and nNumCols might
`suggest that the function can process a portion of the matrix fTheirMatrix.
`The function yourSum declares the parameter fYourMatrix, which is an
`open double-type matrix. This function can take arguments that are double-
`type matrices of different numbers of rows. However, the arguments for the
`matrix parameter must have MAX_COLS columns. The parameters
`nNumRows and nNumCols specify the number of matrix rows and columns,
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER 11 © MULTIDIMENSIONAL ARRAYS
`
`a =(Eau) i |
`
`respectively, to process. The argumentfor parameter nNumRowsshould be
`equalto or less than the numberof rowsin the argumentfor fYourMatrix.If
`not, the function risks accessing data that lie beyond the space occupied by
`the matrix argument.
`Here are examplesof calling these functions:
`
`const int MAX_ROWS1=10;
`const int MAX_ROWS2=10;
`
`const
`int MAX_COLS = 20;
`
`double fMatrix1£MAX_ROWS1J[MAX_COLS];
`double fMatrix2[MAX_ROWS2][MAX_COLS];
`double fSum;
`
`fSum = mySum(fMatrixl);
`fSum = theirSum(fMatrix2);
`
`fSum = theirSum(fMatrix2, MAX_ROWS2
`
`/ 2, MAX_COLS / 2);
`
`#Sum = yourSum(fMatrixl, MAX_ROWS1, MAX_COLS);
`fSum = yourSum(fMatrix2, MAX_ROWS2, MAX_COLS);
`
`This code snippet declares the constants MAX_ROWS1, MAX_ROWS2,
`and MAX_COLS, which define the number of rows and columnsin the
`double-type matrices fMatrix] and fMatrix2, respectively. The code defines
`the double-type variable fSum.It then makes the following calls to the
`tested functions:
`
`= Call function mySum with the argument fMatrix1
`= Call function theirSum with the argument fMatrix2. This function call
`uses the default arguments of MAX_ROWS2 and MAX_COLSfor
`parameters nNumRows and nNumCols, respectively.
`= Call function theirSum with the arguments fMatrix2, MAX_ROWS2 / 2,
`and MAX_COLS / 2
`= Call function yourSum with the arguments fMatrix], MAX_ROWS1, and
`MAX_COLS
`® Call function yourSum with the arguments fMatrix2, MAX_ROWS2, and
`MAX_COLS
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`~ = [E|" m
`
`PART Il © THE BASICS OF C++
`
`The code snippet showsthat function yourSum is very flexible, sinceit
`handles matrices of different row sizes.
`Let’s look at a programming example. Listing 11-3 shows the source
`code for the MDARRAY3.CPP program, which illustrates parameter matrices.
`Thelisting contains functions that sort, copy, and display matrices of
`integers. The program declares two C++ matrices (initializing the first one)
`and performs the following tasks:
`
`® Copythe values from the first matrix into the second matrix. Both
`matrices contain unsorted integers.
`
`= Display the first unsorted matrix.
`
`= Sort the first matrix, using the elements of the first column as the sorting
`key values, and then display its elements. This task uses a sorting
`function that has a matrix parameter with a fixed number of elements.
`
`= Copythe values from the second matrix into the first matrix. The
`elements of the first matrix are now unsorted.
`
`= Sort the first matrix, using the elements of the first column as the sorting
`key values, and then display its elements. This task uses a sorting
`routine that has an open matrix parameter.
`
`= Sort the second matrix, using the elements of the first column as the
`sorting key values, and then display its elements. This task also uses a
`sorting routine that has an open matrix parameter.
`
`Hereis the output of the program in Listing 11-3:
`
`Unsorted matrix nMatrixl
`41 67 55
`98 12 15
`10 65 48
`
`is:
`
`Sorted matrix nMatrixl
`10 65 48
`41 67 55
`98 12 15
`
`is:
`
`Unsorted matrix nMatrixl
`41 67 55
`98 12 15
`10 65 48
`
`is:
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER 11 © MULTIDIMENSIONAL ARRAYS
`
`Sorted matrix nMatrixl
`10 65 48
`4] 67 55
`98 12 15
`
`is:
`
`Sorted matrix nMatrix2 is:
`10 65 48
`41 67 55
`98 12 15
`
`Listing 11-3
`The source code for the MDARRAY3.CPP program.
`// A C++ program that illustrates
`// multidimensional array parameters
`
`#include <iostream. h>
`#include <iomanip.h>
`
`int MAX_ROWS1 = 3;
`const
`const int MAX_ROWS2=10;
`const
`int MAX_COLS = 3;
`
`void bubbleSortl(int nMatrix[MAX_ROWS1][MAX_COLS],
`int nSortColIndex = Q,
`int nNumRows = MAX_ROWS1,
`int nNumCols = MAX_COLS);
`void bubbleSort2(int nMatrix[ JEMAX_COLS],
`int nSortCol Index,
`int nNumRows,
`int nNumCols = MAX_COLS);
`void copyMatrix(int nSourceMatrix({J(MAX_COLS],
`int nTargetMatrix[ ][MAX_COLS],
`int nNumRows,
`int nNumCols);
`void showMatrix(const char* pszMsqg,
`int nMatrix{][MAX_COLS],
`int nNumRows,
`int nNumCols);
`
`main()
`{
`
`int nMatrix1 [MAX_ROWS1][MAX_COLS] =
`{ 41, 67, 55, 98, 12, 15, 10, 65, 48 };
`int nMatrix2[MAX_ROWS2][MAX_COLS];
`int nColIndex = @;
`
`into nMatrix2
`// copy elements of array nMatrixl
`copyMatrix(nMatrixl, mMatrix2, MAX_ROWS1, MAX_COLS);
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`PartIl © THE BASICS OF C++
`
`// display unsorted matrix nMatrixl
`is:\n",
`showMatrix("Unsorted matrix nMatrixl
`nMatrixl, MAX_ROWS1, MAX_COLS);
`// sort matrix nMatrixl
`bubbleSort](nMatrixl);
`// display sorted matrix nMatrixl
`is:\n",
`showMatrix("Sorted matrix nMatrixl
`nMatrixl, MAXROWS], MAX_COLS);
`
`
`
`
`
`
`
`
`
`// copy elements of matrix nMatrix2 into nMatrixl
`copyMatrix(nMatrix2, nMatrixl, MAXROWS1, MAX_COLS);
`// display unsorted matrix nMatrixl
`is:\n",
`showMatrix("Unsorted matrix nMatrixl
`nMatrixl, MAX_ROWS1, MAX_COLS);
`matrix nMatrixl
`sort
`//
`bubbleSort2(nMatrixl, nColIndex, MAXROWSL, MAX_COLS);
`// display sorted matrix mMatrixl
`is:\n",
`showMatrix("Sorted matrix nMatrixl
`nMatrixl, MAX_ROWS1, MAX_COLS);
`matrix nMatrix2
`// sort
`bubbleSort2(nMatrix2, nColIndex, MAX_ROWS1, MAX_COLS);
`// display sorted matrix nMatrix2
`showMatrix("Sorted matrix nMatrix2 is:\n",
`nMatrix2, MAX_ROWS], MAX_COLS);
`
`return Q;
`
`} v
`
`{
`
`oid bubbleSortl (int nMatrix[MAX_ROWS1 ][MAX_COLS],
`int nSortColIndex,
`int nNumRows,
`int nNumCols)
`
`int nSwap;
`
`i++)
`
`- 1);
`(nNumRows
`<
`7
`@;
`=
`7
`for (int
`< mNumRows;
`j++)
`dg
`for (int J = 1;
`if (nMatrixLil{nSortCol Index]
`>
`{
`nMatrixLjj[nSortColindex])
`for (int k = @
`;
`k
`< nNumCols; k++)
`nSwap = nMatrixLiJ[k];
`aMatrix(iJ[k] = nMatrix[jJCkl:
`nMatrixfjiCk] = nSwap;
`
`}
`
`|
`
`Microsoft Corp. Exhibit 1043
`
`}
`
`} v
`
`{
`
`oid bubbleSort2(int nMatrix[][MAXCOLS],
`int nSortColindex,
`int nNumRows,
`int nNumCols)
`
`int nSwap;
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER 11 © MULTIDIMENSIONAL ARRAYS a a
`
`i++)
`
`- 1);
`< (nNumRows
`1
`for (int 1 = 0;
`j
`< nNumRows;
`j++)
`for (int j} = i;
`if (nMatrixLil[nSortCol Index]
`>
`{
`nMatrixLj]{nSortColIndex])
`for (int k = 0; k < nNumCols; k++)
`nSwap = nMatrixliILk];
`nMatrixliJ£k] = nMatrix[jICkl:
`nMatrixCj]£k] = nSwap;
`
`}
`
`{
`
`}
`
`oid copyMatrix(int nSourceMatrix( JEMAX_COLS],
`int nTargetMatrix({ JLMAX_COLS],
`int nNumRows,
`int nNumCols)
`
`| v
`
`{
`
`i++)
`< nNumRows;
`i
`for (int 7 = 0;
`j
`< nNumCols; j+)
`for (int j = 0;
`nTargetMatrixlil[j] = nSourceMatrixlilljd;
`
`oid showMatrix(const char* pszMsg,
`int nMatrix[ J[MAX_COLS],
`int nNumRows,
`int nNumCols)
`
`} v
`
`cout << pszMsg;
`{
`i++)
`< nNumRows;
`7
`for (int i = ®;
`j
`< nNumCols;
`j++)
`for (int j = @;
`cout << nMatrixli](j] << ‘3
`cout << endl;
`
`} c
`
`out << endl;
`
`
`Listing 11-3 declares the constants MAX_ROWS1, MAX_ROWS2, and
`MAX_COLS, which are the row and column sizes for two different kinds of
`integer matrices. The listing also declares the prototypes for the following
`functions:
`
`® The function bubbleSort] sorts the arguments for parameter nMatrix.
`This is a mult