`Paradigm, and Build High-Performance C++ Apps
`for Any Operating System — Including Windows“ 95
`
`x
`
`»
`‘m
`
`in. pnooupnms
`'
`‘
`Microsoft Corp. mbit 1043
`DC I
`J
`
`,
`,,
`00 c
`
`learn How to Exploit the New Standard Template
`library to Optimize Your Development Efforts
`
`i 3
`
`PRESS
`
`Microsoft Corp. Exhibit 1043
`
`
`
`Oriented
`
`Objgct-
`
`NAMIR C. SHAMMAS
`
`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,
`including interior design, cover design, and icons, may be reproduced or 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
`
`10 9 8 7 6 5 4 3 2 1
`lB/SX/RR/ZV
`
`Distributed in the United States by IDG Books Worldwide, Inc.
`
`Distributed by Macmillan Canada for Canada; by Computer and Technical Books for the
`Caribbean Basin; by Contemporanea de Ediciones for Venezuela; by Distribuidora Cuspide for Argentina; by
`CITEC for Brazil; by Ediciones ZETA S.C.R. Ltda. for Peru; by Editorial Lirnusa SA for Mexico; by Transworld
`Publishers Limited in the United Kingdom and Europe; by Al-Maiman Publishers 8: Distributors for Saudi
`Arabia; by Simron Pty. Ltd. for South Africa; by IDG Communications (HK) Ltd. for Hong Kong; by Toppan
`Company Ltd. 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. For reseller information, including discounts and premium sales, please
`call our Reseller Customer Service department at 800-434—5422.
`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 415655-3295.
`
`For information on translations, contact Marc Jeffrey Mikulich, Director, Foreign & Subsidiary Rights, at IDG
`Books Worldwide, 415-655-3018 or fax 415-655-3295.
`
`For sales inquiries and special prices for bulk quantities, write to the address above or call
`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-454-2086.
`For authorization to photocopy items for corporate, personal, or educational use, please contact Copyright
`Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, or fax 508-750-4470.
`Limit of Liability/Disclaimer of Warranty: The author and publisher have used their 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 event be liable for any loss
`of profit or any other commercial damage, including but not limited to special, incidental, consequential, or
`other damages.
`
`Trademarks: All brand names and product names used in this book are trademarks, registered trademarks, or
`trade names of their respective holders. IDG Books Worldwide is not associated with any product or vendor
`I
`mentioned in this book.
`
`PROGRAMMERS
`
` ii].
`
`§§ll|||l
`
`WORLDWIDE
`
`are trademarks under exclusive license
`to IDG Books Worldwide, Inc, from
`International Data Group, Inc.
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`Arrays
`
`Multidimensional
`
`Array support in C++ is not limited to single-dimensional arrays. Rather,
`it extends to multidimensional arrays. This chapter looks at declaring,
`accessing, and initializing multidimensional arrays. In addition, the chapter
`discusses declaring multidimensional arrays as function parameters. You Will
`also learn about sorting multidimensional arrays using the cornbsort 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 multidimensional array is:
`
`type arrayName[ numberOfElementl ] [numberOfEl ementZ]. ..
`
`The above syntax shows the following aspects:
`
`I 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 II 0 THE BASICS 0F C++
`
`I The name of the array is followed by a sequence of the number of
`elements for the various dimensions. These numbers appear in 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
`number of array elements in each dimension is one value higher than the
`index of the last element in that dimension.
`
`Here are examples of declaring multidimensional arrays:
`
`// example 1
`int n1ntCube[2®l[l@][5];
`// example 2
`const
`int MAX_ROWS = 50;
`const
`int MAXaCOLS = 20;
`double fMatrix[MAX_RONS][MAX_COLS]:
`// example 3
`const
`int MAX_ROWS = 30;
`const int MAX_COLS = 10;
`char
`cNameArray[MAX_RONS+1][MAX_COLS];
`
`The first example declares the inf—type three-dimensional array nlnICube
`with 20 by 10 by 5 elements. The declaration uses the literal constants 20,
`10, and 5. Thus, the indices for the first dimension are in the range of 0 to
`19, the indices for the second dimension are in the range of O to 9, and the
`indices for the third dimension are in the range of 0 to 4.
`The second example declares the constants MAX_ROWS and
`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 cNomeArroy. The
`constant expression MAX_ROWS + 1 defines the number of rows in the
`array cNameArray. The constant MAX_COLS defines the number of
`columns in the array cNomeArroy.
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER 1 l ' MUL'I'IDIMENSIONAL ARRAYS
`
`I I
`
`I I
`
`Accessing Multidimensional Arrays
`
`Once you declare a multidimensional array, you can access it elements
`using the index operator
`The general syntax for accessing an element in
`
`a multidimensional array is:
`
`arrayNamelIndexOfDimensionJ][IndexOfDimensionZ]...
`
`The indices for the various dimensions should be in the valid ranges-—
`
`between 0 and the number of array elements for the dimension minus one.
`
`Here is an example of accessing multidimensional array elements:
`
`10;
`int MAX_ROWS
`const
`20:
`const int MAX_COLS
`double fMatrlX[MAX_ROWS][MAX_COLS];
`for (int i = 0;
`i
`< MAX_ROWS;
`i++)
`for (int J = 0;
`j
`< MAX_COLS;
`j++)
`fMatrixii][j] = double(2 + i
`2 * 3)
`
`This code snippet declares the constants MAX_ROWS and MAX_COLS
`and uses these constants in declaring the double-type two-dimensional array
`lMolrix. Thus the array has rows with indices in the range of O to
`MAX_ROWS - l and columns With indices in the range of 0 to MAX_COLS
`- l . The code snippet uses nested For loops to initialize the elements of the
`array lMolrix. Notice that the last assignment statement uses the syntax
`lMalrix[i][i] and not lMalrixU,
`or lMolrixU,
`as is the case in Pascal and
`BASIC, respectively.
`
`Declaring and Accessing Multidimensional Arrays
`
`C++ requires thatyou enclose eachdimension in a pairof brackets. This style differsfrom BASIC’s
`syntax, which places the comma-delimited list of dimensions in a single pair of parentheses.
`
`‘
`
`I
`
`'|
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`lit.
`
`a! II «1
`
`PART II - THE BASics or C++
`
`Diduring and Accessing Multidimensional Arrays
`
`’
`
`*
`
`Til
`
`C++ requires that you enclose every dimension in a pair of brackets. This style differs from
`
`Pascal'ssyntax, whichplacesthecomma-delimitedlistofdimensionsinasinglepairofbrackets.
`
`‘
`
`Let’s look at a programming example. Listing 11-] shows the source
`
`code for the MDARRAY1.CPP program, which illustrates declaring and
`
`accessing multidimensional arrays. This example illustrates the following
`features:
`
`I Declaring and accessing C++ arrays
`
`I Declaring classes that support multidimensional arrays
`
`The example shows how you declare the operator
`to access the
`elements of a multidimensional array in a class. Why use the operator
`instead of the operator
`The answer is 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 becomes either to
`
`use member functions or to use the operator 0, which allows multiple
`indexing parameters. This operator is called the iteratoropemtor; it is
`supposed to be used to sequentially access elements in data structures, such
`as lists.
`
`The program performs the following tasks:
`
`I Declare a C++ matrix and a matrix object
`
`I
`
`Initialize a C++ matrix
`
`I Assign the square root values of the C++ matrix elements to the
`
`elements of the matrix object
`
`I Display the elements of the matrix object
`
`I Double the values of the elements in the matrix object
`
`I 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 'I 'I 0 MULTIDIMENSIONAI. ARRAYS
`
`I H
`
`II I
`
`Listing 1 1-1
`The source code for the MDARRAYI .CPP program, which illustrates declaring
`and accessing multidimensional arrays.
`// 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 IIN CA)(.A.)
`
`class myMatrix
`{
`
`public:
`myMatrix(double fInitVal = 0);
`double& operator()(int nRow,
`int nCol)
`{
`return m_fMatrix[nRow][nCol];
`l
`void show(c0nst char* pszMsg = "",
`const int nNumRows = MAX_RONS.
`const int nNumCols = MAX_COLS);
`
`protected:
`double m_fMatriX[MAX_ROWS][MAX_COLS];
`
`fl4atrtx
`
`l;
`
`myMatrix::myMatrix(double finitVal)
`[
`
`i++)
`< MAX_R0wS;
`l
`for (int i = 0;
`j
`< MAX_COLS;
`j++)
`for (int j = 0;
`m_fMatrix[i][j] = finitVal;
`
`Microsoft Corp. Exhibit i043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`Ii 5
`
`l l
`
`PART II 0 THE BASICS or C++
`
`void myMatr1x::show(const char* pszMsg,
`const 1nt nNumRows,
`const 1nt nNumCols)
`
`[
`
`char cString[1@];
`
`cout << pszMsg << end];
`I
`for (1nt
`1 = Q;
`1
`< nNumRows; 1++)
`[
`for (int j = 0;
`J
`< nNumCols; 1++)
`sprintf(cStr1ng, “%3.1lf ", m_fMatr1x[1][jl);
`cout << cString;
`
`l c
`
`out << end];
`
`1
`
`a1n()
`
`l m
`
`I
`
`double fXMatEMAX_ROWS][MAX_COLS];
`myMatrix Matrix;
`1nt 1, j:
`
`Alatrix
`
`// 1n1t1a11ze matrix fXMat
`for (1 = 0;
`1
`< MAX_RONS; 1++)
`for (j = 0;
`j
`< MAX_COLS; 1++)
`fXMat[1][j] = 2.0 + (double)(1 * j);
`
`// assign values to object Matr1x
`for (1 = 0:
`1
`< MAX_RONS; 1++)
`for (j = 0;
`J
`< MAX_COLS; 1++)
`Matr1x(1, J) = sqrt(fXMat[1][j]):
`
`// Show the matr1x
`Matrix.sh0w("ln1t1al matrix 15:“):
`
`cout << endl;
`
`// double the values 1n object Matr1x
`for (1 = 0;
`1
`< MAX_RONS; 1++)
`for (J = 0;
`j
`< MAX_COLS;
`j++)
`Matr1x(1, J) += Matr1x(1,
`j);
`
`// Show the matr1x
`Matr1x.show("After doubling values, matr1x 13:");
`
`return 0;
`
`
`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 myMo’rrix, Which has the protected data
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER I 'I 0 MULTIDIMENSIONAL ARRAYS
`
`I I
`
`I 1:"-
`
`member m_fMatrix. This member is a double—type matrix that has
`MAX_ROWS rows and MAX_COLS columns. The class declares the
`
`following public members:
`
`I The constructor, which initializes the values of the elements of the data
`member m_tMatrix. The default argument of parameter tlnitVai initializes
`the matrix elements to 0.
`
`I The operator 0, which accesses the elements of data member
`m_tMatrix. Notice that the member function has the double& return type ,
`(instead 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 nCoi specify the row and column indices of the
`accessed matrix element.
`
`I The member function show, which displays some or all of the matrix
`elements. The parameters nNumRows and nNumCois specify 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 doubletype matrix tXMat 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++
`matn'x tXMat have no initial values (except for the arbitrary values in their
`memory locations). The function main then performs the following tasks:
`
`I Assign values to the elements of matrix tXMat. This task uses nested for
`loops, which iterate over all the elements of matrix tXMat. Each inner
`loop statement assigns the expression 2.0 + (double)(i +
`to the element
`tXMat[i][i]. The matrix tXMat and the object Matrix have the same
`number of elements.
`
`I Assign new values to the elements of the object Matrix. This task uses
`nested tor loops, which iterate over the elements of object Matrix. Each
`inner loop iteration assigns the square root value of element tXMat[i][i]
`to element Matrix(i, i). Notice that the loops statement places Matrix( i. i)
`to the left side of the assignment operator.
`
`I Display the elements in object Matrix by sending it the C++ message
`show. The argument for this message is the literal string “Initial matrix is:”
`Microsoft Corp. Exhibit $1043
`
`I
`
`Microsoft Corp. Exhibit 1043
`
`
`
`
`
`.e
`
`PART II ° THE BASICS 0F C++
`
`I 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 MoirixG, i) using the
`operator +=. Notice that the loop’s statement places MoirixG, i) to the
`left and right sides of the assignment operator.
`
`I Display the elements in object Matrix by sending it the C++ message
`show. The argument for this message is the literal string “After doubling
`values, matrix is:".
`
`Initializing Multidimensional Arrays
`
`C++ allows you to initialize some or all of the elements of a multidimen—
`sional array. The general syntax for initializing a multidimensional array is:
`
`type arrayName£numberOfElementl][numberOfElementZ] =
`
`l vaiuefl
`valueN I:
`
`You need to observe the following rules when you initialize a multidi-
`
`mensional array:
`
`1. The list of initial values appears in a pair of open and close braces and
`is comma—delimited.
`
`2. The list may contain a number of initial values that is equal to or less
`than the total number of elements in the initialized array. Otherwise, the
`
`compiler generates a compile-time error.
`
`3. The compiler assigns the initializing values in the sequence discussed in
`the sidebar “Initializing Multidimensional Arrays.”
`
`4. If the list contains fewer values than the number of elements in the
`
`array, the compiler assigns zeros to the elements that do not receive
`initial values from the list.
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER I 'l 0 MULTIDIMENSIONAL ARRAYS
`
`EHIEE
`
`
`
`Initializing Multidimensional Array
`
`Howdoesthe C++ compilercopythe items in
`a linear list of initializing values into the ele-
`ments of a multidimensional array? The an-
`swer is that the compiler fills up the elements
`at the higher dimension number (represented
`by the index to the right) before the ones at the
`lower dimension number. Let me explain this
`storage scheme using the following example.
`Consider the array lMat, which has three rows
`and two columns:
`
`I
`
`The dimension for the rows is the low dimen—
`sion number, whereas the dimension for the
`columns is the high dimension number. The
`element lMallOHO] is the first array element.
`Thus, the compiler stores the first initializing
`value in element lMal{0][O]. The compiler then
`stores the second initializing value in element
`lMot[0][l ]. The compiler stores the third initial-
`izing value in element lMalIl][0], and so on.
`Here is the sequence of storing the initializing
`values in the matrix lMat:
`
`fMatW] [0]
`matmm
`
`my;an
`fMatlZ] [1]
`
`You can apply the same rule to, say, the array
`lCube[3][2][2] and obtain the following se-
`quence of initialized elements:
`
`fCube[@l[®][@]
`fCubelwlltlll]
`
`fcube[1][@][1]
`fCube[l][1][@]
`fCubell 1[1][1]
`:Eugelgl [3“?
`fcflb: E 2 l E 1 l E0}
`chbefznum
`
`I
`
`Here are examples of initializing multidimensional arrays:
`
`// example 1
`double fMatl3llZ] = l 1.1.
`// example 2
`int nMatl5ll2] =
`
`l
`
`l.
`
`2.2, 3.3, 4.4, 5.5, 6.6 l;
`
`2, 3, 4,
`
`5
`
`l:
`
`The code in example 1 declares the double—type two—dimensional array
`lMol to have three rows and two columns. The declaration also 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 lMGf[O][O], lMoflOHl ], and so
`on. The second example declares the inf—type matrix nMaf to have five rows
`
`and two columns. The declaration also initializes only the first five elements
`Microsoft Corp. Exhibii 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`III- PART II 0 THE BASICS 0F C++
`
`(nMat[O][O], nMat[O][l ], nMat[l ][O], nMat[l ][l ], and nMai[2][0]) 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 MDARRAYZCPP program, which illustrates initializing
`multidimensional arrays. The program illustrates the following features:
`
`I Declaring and initializing multidimensional arrays in C++. The program
`also demonstrates using the number of initializing values to establish the
`
`number of array elements.
`
`I
`
`Initializing a data member that is a multidimensional C++ array. C++
`offers no syntax to directly initialize data members that are arrays.
`
`The program declares and initializes two C++ arrays and an array object.
`It performs the following tasks:
`
`I Display the initial values in the array object
`
`I
`
`Increment the values in the array object using the values in the two C++
`arrays
`
`I Display the new values in the array object
`
`Here is the output of the program in Listing 11-2:
`
`NHHL;a;A.
`
`(MN—‘-EoL»—4
`
`mNELabxc+
`
`\AOJ—h bukshmm.
`
`ink."
`
`@012boms
`
`\I\IEEna»r+
`
`\lm—h haha KONU; é>;q-.
`
`\lm Gabe
`
`Listing 11-2
`The source code for the MDARRAYZCPP program, which illustrates initializing
`
`multidimensional arrays.
`// A C++ program that illustrates initializing
`// multidimensional matrix elements
`
`#include <iostream.h>
`#include <stdio.h>
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER 'I I ‘ MULTIDIMENSIONAL ARRAYS
`
`III.
`
`#1nc1ude <1oman1p h>
`#1nc1ude <math.h>
`
`const 1nt MAX_ROWS
`const 1nt MAX_COLS
`
`ll
`
`c1ass myMatrix
`l
`
`pub11c:
`myMatr1x(1nt bUseIn1tArray = 1, doub1e f1n1tVa1
`doub1e& operator()(1nt nRow. 1nt nCo1)
`[
`return m_fMatr1x[nRow][nCo1];
`l
`void show(const char* pszMsg
`""
`const int nNumRows
`MAX_ROWS,
`const 1nt nNumCo1s
`MAX_COLS):
`
`protected:
`doub1e m_fMatr1x[MAX_ROWS][MAX_COLS]:
`
`1;
`
`myMatr1X::myMatr1x(1nt bUseIn1tArray, double f1n1tVa1)
`1
`
`1f
`
`[
`(bUseIn1tArray)
`doub1e HmtMatEMALRowSJ[MAXLédLS] =
`l 1.9, 2.3, 7.6, 3.2, 6.7,
`2.9, 3.8, 8.7, 7.5, 4.5 I;
`for (1nt
`1 = 0;
`1
`< MAX_RONS; 1++)
`for (1nt
`J = 0;
`j
`< MAX_COLS;
`j++)
`m fMatr1x[1][j] = f1n1tMat[1][j];
`
`' Initialized matrix
`
`le
`
`} v
`
`1se
`< MAX_ROWS; 1++)
`1
`for (int 1 = 0;
`j
`for (int j = 0;
`< MAX_COLS;
`j++)
`f1n1tVa1;
`m_fMatr1x[1][J]
`
`oid myMatrix::show(const
`const
`const
`
`char* pszMsg,
`1nt nNumRows,
`1nt nNumCo1s)
`
`char cStr1ng[1@];
`
`cout << pszMsg << end];
`for (1nt
`1
`0;
`1
`< nNumRows; 1++)
`1
`for (1nt j = w;
`j
`< nNumCols;
`j++)
`sprintf(cStr1ng,
`"%3.11f ", m_fMatr1x[1][j]);
`cout << cStr1ng;
`
`l c
`
`out << end];
`
`}
`
`1
`
`Microsoft Corp. Exhibit? 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`I I
`
`I I
`
`PART II - THE BASICS or C++
`
`main()
`[
`
`double fXMatEZ][MAX_RONS][MAX_COLS] =
`l
`// data for sub-array fMat[@][][l
`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[1][][]
`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("1nitial matrix 15:");
`
`cout << endl;
`
`Aflqnix
`
`E
`;
`i
`
`// update matrix Matrix
`i++)
`for (int i = 0;
`i
`< MAX_ROWS;
`for (int j = 0;
`j
`< MAX_COLS;
`j++)
`Matrix(i, J) = fXMatllllilljl
`/ fXMathllilljl;
`
`// show the matrix
`Matrix.show("New matrix 15:");
`
`return 0;
`
`
`Listing 11—2 declares the constants MAX_ROWS and MAX_COLS, which
`define the number of rows and columns of the matrices used in the
`
`program. The listing declares the class myMafrix, which is similar to that in
`Listing 11-1. The main difference is that the new class version has a
`
`constructor that allows you to initialize the matrix in one of two ways:
`When the argument of the parameter bUselni’rMotrix is 1 (which is the
`default argument) or any nonzero value, the constructor uses the local
`initialized C++ matrix ilniiMol to provide the initializing values for the
`elements of data member m_iMairix. The constructor declares the matrix
`HniiMoi to have MAX_ROW5 rows and MAX_COLS columns. If the
`parameter bUseInilMofrix is O, the constructor assigns the value of parameter
`ilniiVol to each element in the data member m_iMafrix.
`The function main declares and initializes the three—dimensional C++
`matrix fXMoi. This array conceptually models two matrices glued to each
`other. The declaration of matrix fXMai specifies a dual matrix with
`MAX_ROWS rows and MAX__COLS columns. The function main also
`declares the object Matrix as an instance of class myMoirix. The way the
`source code declares object Matrix ends up using the default argument for
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER I 'I 0 MULTIDIMENSIONAI ARRAYS
`
`I I
`
`I I
`
`the constructor of class myMotrix. Consequently, the runtime system
`initializes data member m_iMc1trix in object Matrix using the matrix flnitMot.
`The function main then performs the following tasks:
`
`I Display the initial elements in the object Matrix by sending it the C++
`message show. The argument for this message is the string literal “Initial
`matrix is:”.
`
`I Update the values in object Matrix. This task uses nested for loops,
`which iterate over the elements of the matrices. Each inner loop
`iteration assigns the ratio of tXMotU
`to tXMat[O][i][i], to the element
`Motrix(i,
`
`I Display the new values of the elements in the object Matrix by sending
`it the C++ message show. The argument for this message is 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[numberOfElementl][numberOfElementZln
`// open-array parameter
`type parameterName[l[number0f£lement21.
`
`The fixed-array parameter states the number of elements for each
`
`dimension. By contrast, the open array parameter lists the number of
`
`elements for the second dimension and up. In other words, the open
`
`parameter leaves the number of elements for the first dimension unspecified.
`
`Microsoft Corp. Exhibit £1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`ii
`
`E if!
`
`PART II 0 THE BASICS 0F C++
`
`Open MlltjdimensionulArray Parameters
`
`C++ puts restrictions on the level of generality of open multidimensional array parameters. Only
`the first dimension of the array is variable. Thus, C++ can allow you to write matrix manipulating
`functions that deal with matrices that have a variable number of rows but a fixed number of
`
`columns. This feature is more limited than that in modern implementations of BASIC that support
`truly general-purpose matrix parameters.
`
`Here are examples of declaring multidimensional array parameters:
`
`double mySum(d0uble fMyMatrix[MAX_R0llSl][MAX‘COLS]);
`double theirSum(double fTheirMatrix[MAX_R0w52][MAX_COLS],
`int nNumRows = MAX_RONSI,
`int nNumCols = MAX_COLS);
`double yourSum(double fYourMatrix[][MAX_COLS],
`int nNumRows.
`int nNumCols);
`
`The function mySum declares the matrix parameter fMyMotrix and
`specifies that the parameter has MAX_ROWSl rows and MAX_COLS
`columns. This kind of function expects the caller to pass a double-type
`matrix that also has MAX_ROWS] rows and MAX_COLS columns. Since the
`
`function’s parameter list does not have parameters that pass the number of
`rows and columns to process, it is safe to assume that function mySum
`processes all of the elements in parameter fMyMotrix.
`The function fheirSum has three parameters. The first one is the matrix
`parameter fTheirMcrfrix and specifies MAX_ROW52 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 fTheirMolrix must
`be doubletype 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 fYourMotrix, 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 'I I 0 MULTIDIMENSIONAI ARRAYS
`
`I I I I
`
`respectively, to process. The argument for parameter nNumRows should be
`equal to or less than the number of rows in the argument for fYourMatrix. If
`not, the function risks accessing data that lie beyond the space occupied by
`
`the matrix argument.
`
`Here are examples of calling these functions:
`
`10;
`const int MAX_RONSI
`10;
`const
`int MAX_ROWS2
`const
`int MAX_COLS = 20;
`
`double fMatrix1[MAX_ROWS1][MAX_COLS];
`double fMatrix2[MAX_R0w52][MAX_COLS]:
`double fSum;
`
`fSum = mySum(fMatr1xl);
`
`fSum = theirSum(fMatrix2);
`
`fSum = theirSum(fMatrix2. MAX_ROWS2
`
`/ 2, MAX_COLS / 2);
`
`fSum = yourSum(fMatrix1. MAX_ROWSl, MAX_COLS);
`
`fSum = yourSum(fMatrix2, MAX_ROWSZ. MAX_COLS);
`
`This code snippet declares the constants MAX_ROWSl, MAX_ROW52,
`and MAX_COLS, which define the number of rows and columns in the
`doubletype matrices fMoirixl and fMofrixZ, respectively. The code defines
`the double-type variable fSum. It then makes the following calls to the
`tested functions:
`
`I Call function mySum With the argument fMatrixl
`
`I Call function fheirSum With the argument fMatrixZ. This function call
`uses the default arguments of MAX_ROW52 and MAX_COLS for
`parameters nNumRows and nNumCols, respectively.
`I Call function theirSum with the arguments fMofrixZ, MAX_ROW52 / 2,
`and MAX_COLS / 2
`
`I Call function yourSum with the arguments fMafrixl, MAX_ROWSl, and
`MAX_COLS
`
`I Call function yourSum with the arguments fMofrixZ‘, MAX_ROW52, and
`MAX_COLS
`
`Microsoft Corp. Exhibit i043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`PART II 0 THE BASICS 0F C++
`
`The code snippet shows that function yourSum is very flexible, since it
`handles matrices of different row sizes.
`
`Let’s look at a programming example. Listing 11-5 shows the source
`
`code for the MDARRAYSCPP program, which illustrates parameter matrices.
`
`The listing 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:
`
`I Copy the values from the first matrix into the second matrix. Both
`matrices contain unsorted integers.
`
`I Display the first unsorted matrix.
`
`I 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.
`
`I Copy the values from the second matrix into the first matrix. The
`elements of the first matrix are now unsorted.
`
`I 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.
`
`I 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.
`
`Here is the output of the program in Listing 11—3:
`
`Unsorted matrix nMatrix1 is:
`41 67 55
`98 12 15
`1% 65 48
`
`Sorted matrix nMatrixl
`10 65 48
`41 67 55
`98 12 15
`
`is:
`
`Unsorted matrix nMatrixl
`41 67 55
`98 12 15
`1o 65 48
`
`is:
`
`Microsoft Corp. Exhibit 1043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER 'I I 0 MULTIDIMENSIONAL ARRAYS
`
`Sorted matrix nMatrixl
`10 65 48
`41 67 55
`98 12 15
`
`is:
`
`Sorted matrix nMatrixZ is:
`10 65 48
`41 67 55
`98 12 15
`
`Listing '| 1-3
`
`The source code for the MDARRAY3.CPP program.
`// A C++ program that iiiustrates
`// multidimensional array parameters
`
`#inciude <iostream.h
`#inciude <iomanip h>
`
`>
`
`3;
`int MAX_RONSl
`const
`10;
`const int MAX_RONSZ
`const
`int MAX_COLS = 3;
`
`void bubbleSort1(int
`int
`int
`int
`void bubb1eSort2(int
`int
`int
`
`nMatrix[MAX_R0w81][MAX_COLS].
`nSortCoiIndex
`0,
`nNumRows = MAX_RONSl,
`nNumCois = MAX_COLS);
`nMatrix[][MAX_COLS].
`nSortCoiIndex.
`nNumRows,
`int nNumCois = MAX_COLS);
`void copyMatrix(int nSourceMatrixE][MAX_COLS].
`int nTargetMatrix[][MAX_COLS],
`int nNumRows,
`int nNumCols);
`void showMatrix(const char* pszMsg,
`int nMatrixE][MAX_COLS].
`int nNumRows,
`int nNumCois);
`
`main()
`{
`
`int nMatr1X1[MAX_RONSl][MAX_COLS] =
`[ 41, 67, 55, 98, 12, 15, 10, 65, 48 1;
`int nMatrixZEMAX_ROWS2][MAX_COLS];
`int nCoiIndex
`0:
`
`into nMatrixZ
`// copy e1ements of array nMatrixl
`c0pyMatrix(nMatrix1, nMatrixZ, MAX_RONSl, MAX_COLS);
`
`Microsoft Corp. Exhibit 1:043
`
`Microsoft Corp. Exhibit 1043
`
`
`
`I II I
`
`PART II 0 THE BASICS 0F C++
`
`// dispiay unsorted matrix nMatrixi
`is:\n",
`showMatrix("Unsorted matrix nMatrixl
`nMatrixl, MAX7R0wSl. MAX7COLS):
`// sort matrix nMatrixl
`bubbleSortl£nMatrix1);
`// display sorted matrix nMatrixl
`is:\n",
`showMatrix("Sorted matrix nMatrixl
`n atrix . MAX_RONSI. MAX_COLS);
`
`:
`i
`'
`
`5 Z a: f? 1 x
`
`
`// copy eiements of matrix nMatrixZ into nMatrixl
`copyMavr'x(n atrixZ, nMatrixl. MAxrROWSI. MAX_COLS);
`// disaiay u sortec matrix nMatrixl
`is \n",
`showMa r x("Jnsorted matrix nMatrixl
`n atrix , MAXIR0N81. MAX7C0LS):
`atrix nMatrixl
`// sor
`bubbleSortZ< Matrixl, nColIndex. MAXIROWSI, MAX7C0LS);
`// disaiay sorted
`atrix nMatrixl
`is:\n",
`showMatr x("Sorted matrix nMatrixl
`n atrix , MAX_R0wSl. MAX7COLS):
`atr x nMa
`rixZ
`// sor
`bubbieSort2<
`' 2, nCoiIndex, MAX_RONSI. MAXICOLS);
`// dispiay sorted
`atrix nMatrixZ
`showMa r'x("Sorted matrix nMatrixZ is:\n",
`nMatrixZ, MAX_RONSI, MAX_COLS):
`
`
`
`
`
`return 0:
`
`l v
`
`{
`
`oid bubbieSortl(int nMatrix[MAX_RONSl][MAX,COLS],
`int nSortCoiIndex,
`int nNumRows,
`int nNumCois)
`
`int nSwap;
`
`i++)
`
`* 1);
`(nNumRows
`<
`1
`;
`=
`i
`for (int
`< nNumRows;
`j++)
`j
`for (int J = i;
`if (nMatrix[i][nSortCoiIndex] >
`l
`nMatrix[j][nSortCoiIndex])
`for (int k = a
`;
`k
`< nNumCois; k++)
`nSwap = nMatrix[i][k];
`nMatrix[i][k] = nMatrix[j][k]:
`nMatrix[j][k] = nSwap;
`
`i
`
`i
`
`Microsoft Corp. Exhibit 1043
`
`l
`
`i v
`
`l
`
`oid bubbieSort2(int nMatrix[][MAX7C0LS],
`int nSortCoiIndex.
`int nNumRows,
`int nNumCois)
`
`int nSwap;
`
`Microsoft Corp. Exhibit 1043
`
`
`
`CHAPTER I I 0 MULTIDIMENSIONAL ARRAYS
`
`i++)
`
`- 1);
`< (nNumRows
`1
`for (int 1 = 0;
`j
`< nNumRows;
`j++)
`for (int J = 1;
`if (nMatrix[i][nSortColIndex] >
`[
`nMatrix[j][nSortColIndexl)
`for (int k = 0
`;
`k < nNumCols; k++)
`nSwap = nMatriinJEk];
`nMatrix[i][k] = nMatrixEjllk];
`nMatrix[j][k] = nSwap;
`
`l
`
`[
`
`l
`
`oia copyMatrix(int nSourceMatrix£l[MAX_COLS],
`int nTargetMatrix[][MAX_COLS],
`int nNumRows,
`int nNumCols)
`
`l v
`
`l
`
`i++)
`< nNumRows;
`i
`for (int 1 = 0;
`j
`< nNumCols;
`j++)
`for (int J = 0;
`nTargetMatrix[1][j] = nSourceMatriin][j];
`
`oid showMatrix(const char* pszMsg.
`int nMatrix[][MAX_COLS].
`int nNumRows,
`int nNumCols)
`
`l v
`
`l
`
`cout << pszMsg;
`I
`i++)
`< nNumRows;
`1
`for (int i = 0;
`j
`< nNumCols;
`j++)
`for (int j = 0;
`cout << nMatrix[i][j] << '
`';
`cout << endl;
`
`} c
`
`out << e