<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C++ 程序:通過將矩陣傳遞給函數將兩個矩陣相乘 > 原文: [https://www.programiz.com/cpp-programming/examples/matrix-multiplication-function](https://www.programiz.com/cpp-programming/examples/matrix-multiplication-function) #### 在此示例中,您將學習將兩個矩陣相乘并使用用戶定義的函數進行顯示。 要理解此示例,您應該了解以下 [C++ 編程](/cpp-programming "C++ tutorial")主題: * [C++ 數組](/cpp-programming/arrays) * [C++ 多維數組](/cpp-programming/multidimensional-arrays) * [在 C++ 編程中將數組傳遞給函數](/cpp-programming/passing-arrays-function) * * * 該程序要求用戶輸入矩陣的大小(行和列)。 然后,它要求用戶輸入兩個矩陣的元素,最后將兩個矩陣相乘并顯示結果。 要執行此任務,需要執行三個步驟: 1. 從用戶那里獲取矩陣元素 2. 乘以兩個矩陣 3. 在乘法后顯示結果矩陣 ### 示例:通過將矩陣傳遞給函數來相乘 ```cpp #include <iostream> using namespace std; void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond); void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond); void display(int mult[][10], int rowFirst, int columnSecond); int main() { int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k; cout << "Enter rows and column for first matrix: "; cin >> rowFirst >> columnFirst; cout << "Enter rows and column for second matrix: "; cin >> rowSecond >> columnSecond; // If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. while (columnFirst != rowSecond) { cout << "Error! column of first matrix not equal to row of second." << endl; cout << "Enter rows and column for first matrix: "; cin >> rowFirst >> columnFirst; cout << "Enter rows and column for second matrix: "; cin >> rowSecond >> columnSecond; } // Function to take matrices data enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond); // Function to multiply two matrices. multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond); // Function to display resultant matrix after multiplication. display(mult, rowFirst, columnSecond); return 0; } void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond) { int i, j; cout << endl << "Enter elements of matrix 1:" << endl; for(i = 0; i < rowFirst; ++i) { for(j = 0; j < columnFirst; ++j) { cout << "Enter elements a"<< i + 1 << j + 1 << ": "; cin >> firstMatrix[i][j]; } } cout << endl << "Enter elements of matrix 2:" << endl; for(i = 0; i < rowSecond; ++i) { for(j = 0; j < columnSecond; ++j) { cout << "Enter elements b" << i + 1 << j + 1 << ": "; cin >> secondMatrix[i][j]; } } } void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond) { int i, j, k; // Initializing elements of matrix mult to 0. for(i = 0; i < rowFirst; ++i) { for(j = 0; j < columnSecond; ++j) { mult[i][j] = 0; } } // Multiplying matrix firstMatrix and secondMatrix and storing in array mult. for(i = 0; i < rowFirst; ++i) { for(j = 0; j < columnSecond; ++j) { for(k=0; k<columnFirst; ++k) { mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j]; } } } } void display(int mult[][10], int rowFirst, int columnSecond) { int i, j; cout << "Output Matrix:" << endl; for(i = 0; i < rowFirst; ++i) { for(j = 0; j < columnSecond; ++j) { cout << mult[i][j] << " "; if(j == columnSecond - 1) cout << endl << endl; } } } ``` **輸出** ```cpp Enter rows and column for first matrix: 3 2 Enter rows and column for second matrix: 3 2 Error! column of first matrix not equal to row of second. Enter rows and column for first matrix: 2 3 Enter rows and column for second matrix: 3 2 Enter elements of matrix 1: Enter elements a11: 3 Enter elements a12: -2 Enter elements a13: 5 Enter elements a21: 3 Enter elements a22: 0 Enter elements a23: 4 Enter elements of matrix 2: Enter elements b11: 2 Enter elements b12: 3 Enter elements b21: -9 Enter elements b22: 0 Enter elements b31: 0 Enter elements b32: 4 Output Matrix: 24 29 6 25 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看