Advertisement
themlgyo

Дз по массивам

Apr 30th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. ############################################
  2. ЧЕТНЫЕ ЧИСЛА ИЗ МАССИВА
  3.  
  4. #include <iostream>
  5. using namespace std;
  6. void Input(int arr[], int size) {
  7.     for (int i = 0; i < size; i++) {
  8.         cout << "arr[" << i << "] = ";
  9.         cin >> arr[i];
  10.     }
  11. }
  12. void Output(int mass[], int size, int arr[]){
  13.     for (int i = 0; i < size; i++) {
  14.         if (arr[i] % 2 == 0) {
  15.             mass[i] = arr[i];
  16.             cout << mass[i] << " ";
  17.         }
  18.     }
  19.     cout << "}" << endl;
  20. }
  21.     int main()
  22.     {
  23.         setlocale(LC_ALL, "RUSSIAN");
  24.         int *arr;
  25.         int *mass;
  26.         int size;
  27.         cout << "Введите размер массива." << endl;
  28.         cout << "n= ";
  29.         cin >> size;
  30.         if (size <= 0) {
  31.             cout << "Размер массива должен быть положительным" << endl;
  32.             return 1;
  33.         }
  34.         arr = new int[size];
  35.         int n = 0;
  36.         Input(arr, size);
  37.         for (int i = 0; i < size; i++) {
  38.             if (arr[i] % 2 == 0) {
  39.                     n++;
  40.                 }
  41.             }
  42.         cout << " " << endl;
  43.         cout << "Массив с четными числами: { ";
  44.         mass = new int[n];
  45.         Output(mass, size, arr);
  46.         cout << " " << endl;
  47.         return 0;
  48.     }
  49.  
  50. ##########################################
  51.  
  52. МАСКИМАЛЬНЫЙ ЧЛЕН МАССИВА
  53.  
  54. #include <iostream>
  55. using namespace std;
  56. int main()
  57. {
  58.     setlocale(LC_ALL, "RUSSIAN");
  59.     int *arr;
  60.     int size;
  61.     cout << "Введите размер массива." << endl;
  62.     cout << "n= ";
  63.     cin >> size;
  64.     if (size <= 0) {
  65.         cerr << "Размер массива должен быть положительным" << endl;
  66.         return 1;
  67.     }
  68.     arr = new int[size];
  69.     for (int i = 0; i < size; i++) {
  70.         cout << "arr[" << i << "] = ";
  71.         cin >> arr[i];
  72.     }
  73.     int max = arr[0];
  74.     for (int i = 1; i < size; i++) {
  75.         if (arr[i] > max) {
  76.             max = arr[i];
  77.         }
  78.     }
  79.     cout << " " << endl;
  80.     cout << "Максимальный элемент массива:  " << max << endl;
  81.     cout << " " << endl;
  82.     delete[] arr;
  83.     return 0;
  84. }
  85.  
  86. ###############
  87. 3 числа
  88.  
  89. #include <iostream>
  90. using namespace std;
  91. double Max(double x, double y){
  92.     if (x > y){
  93.         return x;
  94.     }
  95.     else return y;
  96. }
  97. double Min(double x, double y){
  98.     if (x < y){
  99.         return x;
  100.     }
  101.     else return y;
  102. }
  103. int main()
  104. {
  105.     setlocale(LC_ALL, "RUSSIAN");
  106.     int A, B, C, max, min;
  107.     cout << "Vvedite 1 chislo" << endl;
  108.     cin >> A;
  109.     cout << "Vvedite 2 chislo" << endl;
  110.     cin >> B;
  111.     cout << "Vvedite 3 chislo" << endl;
  112.     cin >> C;
  113.     max = Max(A, Max(B, C));
  114.     min = Min(C, Min(A, B));
  115.     cout << "MAX = " << max << endl;
  116.     cout << "MIN = " << min << endl;
  117.     return 0;
  118. }
  119.  
  120. #######################
  121.  
  122. #include <iostream>
  123. using namespace std;
  124. double distance(double a, double b, double c, double d){
  125.     return sqrt(pow((c - a), 2) + pow((d - b), 2));
  126. }
  127.  
  128. int main()
  129. {
  130.     setlocale(LC_ALL, "RUSSIAN");
  131.     double x1, y1, x2, y2, x3, y3, P, p, S;
  132.     cout << "Please enter A (x1,y1) = " << endl;
  133.     cin >> x1;
  134.     cin >> y1;
  135.     cout << "Please enter B (x2,y2) = " << endl;
  136.     cin >> x2;
  137.     cin >> y2;
  138.     cout << "Please enter C (x3,y3) = " << endl;
  139.     cin >> x3;
  140.     cin >> y3;
  141.     P = distance(x1, y1, x2, y2) + distance(x2, y2, x3, y3) + distance(x1, y1, x3, y3);
  142.     p = P / 2;
  143.     S = sqrt(p*(p - distance(x1, y1, x2, y2))*(p - distance(x2, y2, x3, y3))*(p - distance(x1, y1, x3, y3)));
  144.     cout << " "<< endl;
  145.     cout << p << endl;
  146.     cout << S << endl;
  147.     return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement