Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <stdlib.h>
  4. using namespace std;
  5. void create_vector(double* V, int n)
  6. {
  7.     int i;
  8.     for (i=0; i<n; i++)
  9.     {
  10.         cout << "x[" << i<< "]= ";
  11.         cin >> V[i];
  12.     }
  13. }
  14. void show_vector(double* V, int n)
  15. {
  16.     int i; int j;
  17.     for (i = 0; i < n; i++)
  18.     {
  19.         for (j = 0; j < 1; j++)
  20.             cout << setw(11) << V[i];
  21.             cout << endl;
  22.     }
  23. }
  24. double sum_vector(double* V, int n)
  25. {
  26.     int s=0;
  27.     int i;
  28.     for(i=0;i<n;i++)
  29.         s=s+V[i];
  30.     return s;
  31. }
  32. void create_matrix(double** M, int n)
  33. {
  34.     int i; int j;
  35.     for (i = 0; i < n; i++)
  36.     for (j = 0; j < n; j++)
  37.     {
  38.             cout << "x[" << i << "][" << j << "] = ";
  39.             cin >> M[i][j];
  40.     }
  41. }
  42. void show_matrix(double** M, int n)
  43. {
  44.     int i; int j;
  45.     for (i = 0; i < n; i++)
  46.     {
  47.         for (j = 0; j < n; j++)
  48.             cout << setw(11) << M[i][j];
  49.             cout << endl;
  50.     }
  51. }
  52. void mult(double**M, double* V, double* C, int n)
  53. {
  54.     int i; int j;
  55.     for(i=0;i<n;i++)
  56.         {
  57.             double k=0;
  58.             for(j=0;j<n;j++)
  59.         {
  60.             k=k+(M[i][j]*V[j]);
  61.         }
  62.         C[i]=k;
  63.         }
  64. }
  65. int main()
  66. {
  67.     setlocale(LC_ALL,"Russian");
  68.    
  69.     int n; int i; int j;
  70.     cout << "Введите размерность вектора Х: ";
  71.     cin >> n;
  72.    
  73.     cout << endl;
  74.    
  75.     cout << "Введите элементы вектора Х:" << endl;
  76.     double* a = new double [n];
  77.     for(i=0;i<n;i++)
  78.     a[i]=i;
  79.     create_vector(a,n);
  80.    
  81.     cout << endl;
  82.    
  83.     cout << "Вектор Х:" << endl;
  84.     show_vector(a,n);
  85.    
  86.     cout << endl;
  87.     cout << endl;
  88.    
  89.     cout << "Сумма элементов вектора X: " << sum_vector(a,n) << endl;
  90.     cout << endl;
  91.     cout << endl;
  92.    
  93.     int m;
  94.     cout << "Введите размерность матриц А и В: ";
  95.     cin >> m;
  96.    
  97.     cout << endl;
  98.     if(m!=n)
  99.     {
  100.         cout << "Ошибка!" << endl;
  101.         cout << "Для осуществления умножения матрицы на вектор, необходимо, чтобы количество столбцов матрицы было равно количеству строк вектора!" << endl;
  102.         system("Pause");
  103.         return 0;
  104.     }
  105.    
  106.     double** A = new double* [n];
  107.     for(i=0;i<m;i++)
  108.     A[i] = new double [n];
  109.     cout << "Введите элементы матрицы А" << endl;
  110.     create_matrix(A,n);
  111.     cout << endl;
  112.    
  113.     double** B = new double* [n];
  114.     for(i=0;i<m;i++)
  115.     B[i] = new double [n];
  116.     cout << "Введите элементы матрицы В" << endl;
  117.     create_matrix(B,n);
  118.     cout << endl;
  119.    
  120.     cout << "Матрица А:" << endl;
  121.     show_matrix(A,n);
  122.     cout << endl;
  123.    
  124.     cout << "Матрица B:" << endl;
  125.     show_matrix(B,n);
  126.     cout << endl;
  127.  
  128.  
  129.     double c;
  130.     cout << "Введите число с: ";
  131.     cin >> c;
  132.     cout << endl;
  133.    
  134.     double* C = new double [n];
  135.     for(i=0; i<5; i++)
  136.     {
  137.         if(c >= sum_vector(a,n))
  138.         mult(A,a,C,n);
  139.         else
  140.         mult(B,a,C,n);
  141.         for(j=0; j<5; j++)
  142.         a[j]=C[j];
  143.     }
  144.        
  145.     cout << "Вектор Y:" << endl;
  146.     show_vector(a,n);
  147.    
  148.     system("pause");
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement