Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exposure C++ Chapter XXI C++ Data Structures, the 2D Array apmatrix Implementation.

Similar presentations


Presentation on theme: "Exposure C++ Chapter XXI C++ Data Structures, the 2D Array apmatrix Implementation."— Presentation transcript:

1

2 Exposure C++ Chapter XXI C++ Data Structures, the 2D Array apmatrix Implementation

3 Single Index Seating Chart [16] Darlene [17] Sean [18] Astrid [19] Bert [20] Stephany [15] Gene [10] Michelle [05] Heidi [14] Bob [01] Isolde [06] Diana [11] Holly [12] Blake [07] Jessica [02] John [03] Greg [08] David [13] Ingrid [04] Maria [09] Remy

4 Double Index Seating Chart [1][1] Darlene [1][2] Sean [1][3] Astrid [1][4] Bert [1][5] Stephany [2][5] Gene [3][5] Michelle [4][5] Heidi [2][4] Bob [4][1] Isolde [3][1] Diana [2][1] Holly [2][2] Blake [3][2] Jessica [4][2] John [4][3] Greg [3][3] David [2][3] Ingrid [4][4] Maria [3][4] Remy

5 APCS Examination Alert The AP Computer Science Examination will include both multiple choice questions, and free response questions on a special case study. The case study changes about every two or three years. Information about the current case study can be obtained from the College Board.

6 // PROG2101.CPP // This program demonstrates defining a two-dimensional // array with the apmatrix class. #include #include "APMATRIX.H" void main() { apmatrix Square(3,3); int K = 0; int Row,Col; for (Row = 0; Row < 3; Row++) for (Col = 0; Col < 3; Col++) { K++; Square[Row][Col] = K; } for (Row = 0; Row < 3; Row++) { for (Col = 0; Col < 3; Col++) cout << Square[Row][Col] << " "; cout << endl; } PROG2101.CPP OUTPUT 1 2 3 4 5 6 7 8 9

7 This short program contains several very significant statements that require closer inspection. First there is the statement that gets the ball rolling by defining the two-dimensional array. apmatrix Square(3,3); The appearance of this program statement is very similar to an apvector definition. The array element is indicated with the same syntax, followed by the matrix identifier. The biggest difference is that two dimensions need to be given. By convention the first integer is the number of rows of the matrix, and the second integer is the number of columns. Square[Row][Col] = K; The next important statement to consider, is the syntax required for accessing a single matrix element.

8 // PROG2102.CPP // This program demonstrates apmatrix range checking. #include #include "APMATRIX.H" void main() { apmatrix Square(3,3); int K = 0; int Row,Col; for (Row = 1; Row <= 3; Row++) for (Col = 1; Col <= 3; Col++) { K++; Square[Row][Col] = K; } for (Row = 1; Row <= 3; Row++) { for (Col = 1; Col <= 3; Col++) cout << Square[Row][Col] << " "; cout << endl; } PROG2102.CPP OUTPUT There is no normal output. The program crashes with the following message: Illegal vector index: 3 max index = 2 Abnormal program termination

9 // PROG2103.CPP // This program initializes every element of a 3 X 3 matrix // with values of 100. #include #include "APMATRIX.H" void main() { apmatrix Square(3,3,100); int Row,Col; for (Row = 0; Row < 3; Row++) { for (Col = 0; Col < 3; Col++) cout << Square[Row][Col] << " "; cout << endl; } PROG2103.CPP OUTPUT 100 100 100

10 apmatrix Syntax Constructing an apmatrix object without initialization: apmatrix identifier(row,col) apmatrix Matrix(5,5); Constructing an apmatrix object with initialization: apmatrix identifier(row,col,initial val) apmatrix Matrix(5,5,100); Access two-dimensional array elements with [ ][ ] notation: access function Matrix[Row][Col]; cout << Matrix[3][4]; The index range of an apmatrix object with dimension R and C is [0..R-1][0..C-1].

11 // PROG2104.CPP // This program demonstrates the numrows, numcols, and the resize function. void main() { int R,C; // loop counters and array indices int Rows; // number of rows in the matrix int Cols; // number of cols in the matrix apmatrix Matrix(3,4,100); Rows = Matrix.numrows(); Cols = Matrix.numcols(); cout << "Matrix has " << Rows << " rows and " << Cols << " columns"; cout << endl; for (R = 0; R < Rows; R++) { for (C = 0; C < Cols; C++) cout << Matrix[R][C] << " "; cout << endl; } cout << endl << endl; Matrix.resize(4,5); Rows = Matrix.numrows(); Cols = Matrix.numcols(); cout << "Matrix has " << Rows << " rows and " << Cols << " columns"; cout << endl; for (R = 0; R < Rows; R++) { for (C = 0; C < Cols; C++) cout << Matrix[R][C] << " "; cout << endl; } PROG2104.CPP OUTPUT Matrix has 3 rows and 4 columns 100 100 Matrix has 4 rows and 5 columns 100 100 100 100 7 100 100 100 100 0 100 100 100 100 -28457 4 4 -28458 4 -28457

12 The apmatrix numrows and numcols The apmatrix numrows function returns the number of rows in an apmatrix object. The apmatrix numcols function returns the number of columns in an apmatrix object. apmatrix Matrix(5,6); R = Matrix.numrows; // R = 5 C = Matrix.numcols; // C = 6

13 The apmatrix resize The apmatrix resize function alters the number of rows and columns in the current apmatrix object to the values of the resize function’s arguments. apmatrix Matrix(5,6); R = Matrix.numrows; // R = 5 C = Matrix.numcols; // C = 6 Matrix.resize(7,10); R = Matrix.numrows; // R = 7 C = Matrix.numcols; // C = 10

14 // PROG2105.CPP // This program demonstrates apmatrix parameter passing. #include #include "APMATRIX.H" void EnterMatrix(apmatrix &Matrix); void ShowMatrix(const apmatrix Matrix); void main() { apmatrix Matrix(3,3); EnterMatrix(Matrix); ShowMatrix(Matrix); } PROG2105.CPP OUTPUT 1 2 3 4 5 6 7 8 9

15 // PROG2106.CPP // This program demonstrates array dimensions entered by the user. #include #include "APMATRIX.H" void EnterMatrix(apmatrix &Mat); void ShowMatrix(const apmatrix Mat); void main() { apmatrix Matrix; EnterMatrix(Matrix); ShowMatrix(Matrix); }

16 void EnterMatrix(apmatrix &Mat) { int Rows,Cols; int R,C; int K = 0; cout Cols ===>> "; cin >> Rows >> Cols; Mat.resize(Rows,Cols); for (R = 0; R < Rows; R++) for (C = 0; C < Cols; C++) { K++; Mat[R][C] = K; } PROG2106.CPP OUTPUT Enter Rows Cols ===>> 6 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

17 // PROG2107.CPP // This program demonstrates using typedef with the apmatrix class. #include #include "APMATRIX.H" typedef apmatrix TwoDArray; void EnterMatrix(TwoDArray &Mat); void ShowMatrix(const TwoDArray Mat); void main() { TwoDArray Matrix; EnterMatrix(Matrix); ShowMatrix(Matrix); } PROG2107.CPP OUTPUT Enter Rows Cols ===>> 6 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

18 // PROG2108.CPP // This program demonstrates apmatrix assignment. void main() { TwoDArray Matrix1(2,3,100); TwoDArray Matrix2(3,4,200); TwoDArray Matrix3(4,5,300); cout << "MATRIX1" << endl; ShowMatrix(Matrix1); cout << endl << endl; cout << "MATRIX2" << endl; ShowMatrix(Matrix2); cout << endl << endl; cout << "MATRIX3" << endl; ShowMatrix(Matrix3); Matrix1 = Matrix2; cout << endl << endl; cout << "MATRIX1" << endl; ShowMatrix(Matrix1); Matrix3 = Matrix2; cout << endl << endl; cout << "MATRIX3" << endl; ShowMatrix(Matrix3); } PROG2108.CPP OUTPUT MATRIX1 100 100 100 MATRIX2 200 200 MATRIX3 300 300 300 300 300 MATRIX1 200 200 MATRIX3 200 200

19 Matrix Addition

20 Matrix Addition Rules You can only add matrixes together if they have the same dimensions. An A x B plus another A x B matrix results in a third A x B matrix. A does not necessarily have to equal B.

21 Wrong Matrix Multiplication

22 Correct Matrix Multiplication

23

24 The Unit or Identity Matrix There is one more example. In this case the first matrix has some special properties. Note that the first matrix has one diagonal of 1s and all other values are 0s. This is called a "unit matrix" or an "identity matrix". Multiplying the unit matrix times any second matrix will yield a product matrix equivalent to the second matrix.

25 Matrix Multiplication Rules You can only multiply matrixes together if the number of columns in the first matrix is equal to the number of rows in the second matrix. An A x B times a B x C matrix results in an A x C matrix. A does not necessarily have to be different from B. C does not necessarily have to be different from B. The "Commutative Property" works for matrix addition, but NOT for matrix multiplication: Mat1 + Mat2 = Mat2 + Mat1 Mat1 * Mat2  Mat2 * Mat1

26 Case Study Explanation A case study is a presentation of a particular program in a style that assist students with learning program design. The program is not just presented in its final completed form, but rather in many stages, starting with the very first development stage. Additionally, the case study gives the rationalization for the approach that is used at the various stages. After studying a case study a student hopefully has benefited in two important areas: 1. Understanding a new computer science concept that is the focus of the case study. 2. Appreciating and learning strategies for program development by studying the steps that were presented in the case study. This type of understanding carries across to the development of other programs, even if the concepts are different.

27 // MATMULT1.CPP // Stage 1 of the matrix multiplication case study. This stage sets up the stubs for the program. #include #include "APMATRIX.H" typedef apmatrix TwoDArray; void EnterDimensions(int &R1, int &C1, int &R2, int &C2); void EnterMatrix(TwoDArray M); void MultiplyMatrices(const TwoDArray M1, const TwoDArray M2, TwoDArray &M3); void DisplayProduct(const TwoDArray M3); void main() { int Row1,Col1,Row2,Col2; EnterDimensions(Row1,Col1,Row2,Col2); TwoDArray Matrix1(Row1,Col1); TwoDArray Matrix2(Row2,Col2); TwoDArray Matrix3; EnterMatrix(Matrix1); EnterMatrix(Matrix2); MultiplyMatrices(Matrix1,Matrix2,Matrix3); DisplayProduct(Matrix3); } // Stage 1 STUB functions void EnterDimensions(int &R1, int &C1, int &R2, int &C2) { } void EnterMatrix(TwoDArray M) { } void MultiplyMatrices(const TwoDArray M1, const TwoDArray M2, TwoDArray &M3) { } void DisplayProduct(const TwoDArray M3) { } MATMULT1.CPP OUTPUT Stage1 does not have any output yet. This program will crash if you try to executes Click to see stub functions.

28 // MATMULT2.CPP // Stage 2 Completes the EnterDimensions function. // This stage also dimensions the third matrix. void EnterDimensions(int &R1, int &C1, int &R2, int &C2) { cout << "ENTER DIMENSIONS FUNCTION" << endl; cout << endl; cout > "; cin >> R1; cout > "; cin >> C1; R2 = C1; cout << "Matrix 2 row dimension must be " << R2 << endl; cout > "; cin >> C2; R1++; C1++; R2++; C2++; } MATMULT2.CPP OUTPUT ENTER DIMENSIONS FUNCTION Enter Matrix 1 row dimension ===>> 2 Enter Matrix 1 col dimension ===>> 3 Matrix 2 row dimension must be 3 Enter Matrix 2 col dimension ===>> 2

29 // MATMULT3.CPP // Stage 3 Completes the EnterMatrix function. void EnterMatrix(TwoDArray M) { cout << endl << endl; cout << "ENTER MATRIX FUNCTION" << endl; cout << endl; int Rows = M.numrows(); int Cols = M.numcols(); int R,C; for (R = 1; R < Rows; R++) { for (C = 1; C < Cols; C++) { cout > "; cin >> M[R][C]; } cout << endl; }

30 MATMULT3.CPP OUTPUT ENTER DIMENSIONS FUNCTION Enter Matrix 1 row dimension ===>> 2 Enter Matrix 1 col dimension ===>> 2 Matrix 2 row dimension must be 2 Enter Matrix 2 col dimension ===>> 2 ENTER MATRIX FUNCTION Enter Row 1, Column 1 ===>> 1 Enter Row 1, Column 2 ===>> 2 Enter Row 2, Column 1 ===>> 3 Enter Row 2, Column 2 ===>> 4 ENTER MATRIX FUNCTION Enter Row 1, Column 1 ===>> 5 Enter Row 1, Column 2 ===>> 6 Enter Row 2, Column 1 ===>> 7 Enter Row 2, Column 2 ===>> 8

31 // MATMULT4.CPP // Stage 4 initializes matrix 3 with values to test the completed DisplayProduct // function. void main() { int Row1,Col1,Row2,Col2; EnterDimensions(Row1,Col1,Row2,Col2); TwoDArray Matrix1(Row1,Col1); TwoDArray Matrix2(Row2,Col2); TwoDArray Matrix3(Row1,Col2,100); EnterMatrix(Matrix1); EnterMatrix(Matrix2); MultiplyMatrices(Matrix1,Matrix2,Matrix3); DisplayProduct(Matrix3); } void DisplayProduct(const TwoDArray M3) { cout << endl << endl; cout << "DISPLAY PRODUCT FUNCTION" << endl; cout << endl; int Rows = M3.numrows(); int Cols = M3.numcols(); int R,C; for (R = 1; R < Rows; R++) { for (C = 1; C < Cols; C++) cout << setw(5) << M3[R][C]; cout << endl; }

32 MATMULT4.CPP OUTPUT ENTER DIMENSIONS FUNCTION Enter Matrix 1 row dimension ===>> 3 Enter Matrix 1 col dimension ===>> 3 Matrix 2 row dimension must be 3 Enter Matrix 2 col dimension ===>> 3 ENTER MATRIX FUNCTION Enter Row 1, Column 1 ===>> 1 Enter Row 1, Column 2 ===>> 2 Enter Row 1, Column 3 ===>> 3 Enter Row 2, Column 1 ===>> 4 Enter Row 2, Column 2 ===>> 5 Enter Row 2, Column 3 ===>> 6 Enter Row 3, Column 1 ===>> 7 Enter Row 3, Column 2 ===>> 8 Enter Row 3, Column 3 ===>> 9 MATMULT4.CPP OUTPUT Continued ENTER MATRIX FUNCTION Enter Row 1, Column 1 ===>> 1 Enter Row 1, Column 2 ===>> 0 Enter Row 1, Column 3 ===>> 0 Enter Row 2, Column 1 ===>> 0 Enter Row 2, Column 2 ===>> 1 Enter Row 2, Column 3 ===>> 0 Enter Row 3, Column 1 ===>> 0 Enter Row 3, Column 2 ===>> 0 Enter Row 3, Column 3 ===>> 1 DISPLAY PRODUCT FUNCTION 100 100 100

33 Program Development Hint Most programs follow the general sequence of input, process and output. The process stage is usually the most complex - and most likely to cause problems - function. The correctness of the process function cannot be properly tested without some type of output. For this reason it make sense to finish at least a working stage of the output function before the process function is started. Checking the correctness of every program development stage is vital. Failure to check every stage can result in a compounding of errors that becomes very difficult to isolate and virtually impossible to fix.

34 // MATMULT5.CPP // Stage 5 is the first step of the multiplication function. // In this stage only position [1][1] of the product matrix is computed. void MultiplyMatrices(const TwoDArray &M1, const TwoDArray &M2, TwoDArray &M3) { cout << endl << endl; cout << "MULTIPLY MATRICES FUNCTION" << endl << endl; int R1 = M1.numrows(); int C1 = M1.numcols(); int R2 = M2.numrows(); int C2 = M2.numcols(); int R3 = M3.numrows(); int C3 = M3.numcols(); int K; int Sum; Sum = 0; for (K = 1; K < C1; K++) Sum = Sum + M1[1][K] * M2[K][1]; M3[1][1] = Sum; } Assume the matrix entries. Only the result of the multiplication is shown. MULTIPLY MATRICES FUNCTION DISPLAY PRODUCT FUNCTION 1 100 100 100 100 100

35 void MultiplyMatrices(const TwoDArray &M1, const TwoDArray &M2, TwoDArray &M3) { cout << endl << endl; cout << "MULTIPLY MATRICES FUNCTION" << endl; cout << endl; int R1 = M1.numrows(); int C1 = M1.numcols(); int R2 = M2.numrows(); int C2 = M2.numcols(); int R3 = M3.numrows(); int C3 = M3.numcols(); int K,C; int Sum; for (C = 1; C < C2; C++) { Sum = 0; for (K = 1; K < C1; K++) Sum = Sum + M1[1][K] * M2[K][C]; M3[1][C] = Sum; } Assume the matrix entries. Only the result of the multiplication is shown. MULTIPLY MATRICES FUNCTION DISPLAY PRODUCT FUNCTION 1 2 3 100 100 100

36 void MultiplyMatrices(const TwoDArray &M1, const TwoDArray &M2, TwoDArray &M3) { cout << endl << endl; cout << "MULTIPLY MATRICES FUNCTION" << endl << endl; int R1 = M1.numrows(); int C1 = M1.numcols(); int R2 = M2.numrows(); int C2 = M2.numcols(); int R3 = M3.numrows(); int C3 = M3.numcols(); int K,R,C; int Sum; for (R = 1; R < R1; R++) for (C = 1; C < C2; C++) { Sum = 0; for (K = 1; K < C1; K++) Sum = Sum + M1[R][K] * M2[K][C]; M3[R][C] = Sum; }

37 MATMULT7.CPP OUTPUT ENTER DIMENSIONS FUNCTION Enter Matrix 1 row dimension ===>> 3 Enter Matrix 1 col dimension ===>> 3 Matrix 2 row dimension must be 3 Enter Matrix 2 col dimension ===>> 3 ENTER MATRIX FUNCTION Enter Row 1, Column 1 ===>> 1 Enter Row 1, Column 2 ===>> 2 Enter Row 1, Column 3 ===>> 3 Enter Row 2, Column 1 ===>> 4 Enter Row 2, Column 2 ===>> 5 Enter Row 2, Column 3 ===>> 6 Enter Row 3, Column 1 ===>> 7 Enter Row 3, Column 2 ===>> 8 Enter Row 3, Column 3 ===>> 9 ENTER MATRIX FUNCTION Enter Row 1, Column 1 ===>> 1 Enter Row 1, Column 2 ===>> 0 Enter Row 1, Column 3 ===>> 0 Enter Row 2, Column 1 ===>> 0 Enter Row 2, Column 2 ===>> 1 Enter Row 2, Column 3 ===>> 0 Enter Row 3, Column 1 ===>> 0 Enter Row 3, Column 2 ===>> 0 Enter Row 3, Column 3 ===>> 1 MULTIPLY MATRICES FUNCTION DISPLAY PRODUCT FUNCTION 1 2 3 4 5 6 7 8 9


Download ppt "Exposure C++ Chapter XXI C++ Data Structures, the 2D Array apmatrix Implementation."

Similar presentations


Ads by Google