Πρόγραμμα C ++ για πολλαπλασιασμό δύο πινάκων περνώντας το Matrix στη συνάρτηση

Σε αυτό το παράδειγμα, θα μάθετε να πολλαπλασιάζετε δύο πίνακες και να το εμφανίζετε χρησιμοποιώντας τη λειτουργία που ορίζεται από το χρήστη.

Για να κατανοήσετε αυτό το παράδειγμα, θα πρέπει να γνωρίζετε τις ακόλουθες θεματολογίες προγραμματισμού C ++:

  • Πίνακες C ++
  • Πολυδιάστατες συστοιχίες C ++
  • Μετάβαση σε μια λειτουργία στον προγραμματισμό C ++

Αυτό το πρόγραμμα ζητά από τον χρήστη να εισαγάγει το μέγεθος του πίνακα (σειρές και στήλες).

Στη συνέχεια, ζητά από το χρήστη να εισαγάγει τα στοιχεία δύο πινάκων και τελικά πολλαπλασιάζει δύο πίνακες και εμφανίζει το αποτέλεσμα.

Για την εκτέλεση αυτής της εργασίας πραγματοποιούνται τρεις λειτουργίες:

  1. Για λήψη στοιχείων matrix από το χρήστη
  2. Για να πολλαπλασιάσετε δύο πίνακες
  3. Για να εμφανιστεί η προκύπτουσα μήτρα μετά τον πολλαπλασιασμό

Παράδειγμα: Πολλαπλασιάστε το Matrix μεταβιβάζοντάς το σε μια συνάρτηση

 #include 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 <> rowFirst>> columnFirst; cout <> 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 <> rowFirst>> columnFirst; cout <> 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 <> 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 <> 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 

Output

 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 

ενδιαφέροντα άρθρα...