Advertisement
tabvn

ma trận vuông cấp 3 c++

Sep 12th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  giải ma trận vuông cấp 3 đơn giản
  4. //
  5. //  Created by Toan on 9/12/18.
  6. //  Copyright © 2018 Toan. All rights reserved.
  7. //
  8.  
  9. /*
  10.  
  11.  ma trận cấp vuống 3 sẽ có dạng
  12.  A =
  13.  
  14.  a11 a12 a13
  15.  a21 a22 a23
  16.  a31 a32 a33
  17.  
  18.  
  19.  Tính Det A
  20.  
  21.  */
  22.  
  23.  
  24.  
  25. #include <iostream>
  26.  
  27. using namespace std;
  28.  
  29. float A[9] = {}; // ma trận 3 sẽ có 9 phần tử eg: {0,1,2,3,4,5,6,7,8}
  30.  
  31. int getNumRow(int i){
  32.    
  33.     /*
  34.      Phân tích:
  35.      ta có 9 phần tử độ dài hàng ma trận tối đa là 3
  36.      như vậy tính hàng của ma trận = vị trí / kích thước dài ma trận
  37.      row = i/3
  38.      */
  39.     int row;
  40.    
  41.     row = i/3;
  42.    
  43.     return row + 1; // Cộngh thêm 1 vì ta gọi hàng số 0 sẽ là 1
  44. }
  45.  
  46. int getNumColumn(int i){
  47.  
  48.    
  49.     /*
  50.      tinh cot ma tran col = index % width
  51.      */
  52.    
  53.     int col;
  54.    
  55.     col = i % 3;
  56.  
  57.     return col + 1;
  58. }
  59.  
  60. void displayMatrix(){
  61.    
  62.     for (int i = 0; i < 9; i++){
  63.         if(i %3 ==0 ){
  64.             cout << "\n";
  65.         }
  66.        
  67.         cout << A[i] << " ";
  68.     }
  69.  
  70. }
  71. void detA(){
  72.     float d;
  73.    
  74.     /*
  75.     A =
  76.      a11 a12 a13
  77.      a21 a22 a23
  78.      a31 a32 a33
  79.      
  80.      det A = [a11 *(a22* a33 - a23 * a32)] - [a12 * (a21* a33 - a23 * a31)] + [a13*(a21 * a32 - a22 * a31)]
  81.      */
  82.    
  83.     int a11, a22,a23,a32, a12,a21,a31,a13, a33;
  84.    
  85.     a11 = A[0];
  86.     a22 = A[4];
  87.     a23 = A[5];
  88.     a32 = A[7];
  89.     a12 = A[1];
  90.     a21 = A[3];
  91.     a33 = A[8];
  92.     a31 = A[6];
  93.     a13 = A[2];
  94.    
  95.     d = a11 *(a22* a33 - a23 * a32) - a12 * (a21* a33 - a23 * a31) + a13*(a21 * a32 - a22 * a31);
  96.    
  97.     cout << "Ma Trận A: \n";
  98.    
  99.     displayMatrix();
  100.    
  101.     cout << "\n";
  102.     cout << "Định thức ma trận A là:" << d;
  103.    
  104. }
  105.  
  106. int main(int argc, const char * argv[]) {
  107.  
  108.     // ma trận vuông cấp 3 sẽ có tổng số phần tử là 3*3 : 9
  109.     int x = 0;
  110.    
  111.     for (int i = 0; i < 9; i++){
  112.        
  113.         cout << "Nhập giá trị hàng " << getNumRow(i) << " Cột " << getNumColumn(i) << ":\n";
  114.    
  115.         cin >> x;
  116.    
  117.         A[i] = x;
  118.  
  119.     }
  120.    
  121.     // tinh det a
  122.     detA();
  123.    
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement