Advertisement
Petro_zzz

lesson9_gr2

Sep 11th, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using namespace std;
  5.  
  6. void test1(int arr[]) {
  7.     arr[0] = arr[0] + 1;
  8. }
  9.  
  10. void test2() {
  11.     int b[]{ 3, 56 };
  12.     cout << b[0] << endl;
  13.     test1(b);
  14.     cout << b[0] << endl;
  15.     cout << b << " " << b[0] << " " << *b << endl;
  16.     cout << b + 1 << " " << (b + 1)[0] << " " << *(b + 1) << endl;
  17.     // arr[id] ~ *(arr + id)
  18. }
  19.  
  20.  
  21. void showArr(const int sz, const int arr[]) {
  22.     for (int k = 0; k < sz; k++) {
  23.         cout << arr[k] << " ";
  24.     }
  25.     cout << endl;
  26. }
  27.  
  28. template<class Type>
  29. void showArr(const int sz, const Type arr[]) {
  30.     for (int k = 0; k < sz; k++) {
  31.         cout << arr[k] << " ";
  32.     }
  33.     cout << endl;
  34. }
  35.  
  36. void task1() {
  37.     const int sz1 = 5;
  38.     int arr[]{ 1, 2, 3, 4, 5 };
  39.     showArr(sz1, arr);
  40.     int arr2[sz1];
  41.     for (int k = 0; k < sz1; k++) {
  42.         arr2[k] = arr[sz1 - k - 1];
  43.     }
  44.     showArr(sz1, arr2);
  45. }
  46.  
  47. void test3() {
  48.     int a = 12;
  49.     int b = 23;
  50.     swap(a, b);
  51.     int b2 = 13;
  52.     int b3 = 7;
  53.     cout << b2 << " && "
  54.          << b3 << " = "
  55.          << (b2 & b3) << endl;
  56.  
  57.     cout << &b2 << endl;
  58.     int *c = &b2;
  59.  
  60.     double verylongnameforme = 12.4;
  61.     double& x = verylongnameforme;
  62.  
  63.     cout << x << " " << verylongnameforme << endl;
  64.     x++;
  65.     cout << x << " " << verylongnameforme << endl;
  66.     verylongnameforme = -1.34234342;
  67.     cout << x << " " << verylongnameforme << endl;
  68. }
  69.  
  70. void test4() {
  71.     srand(time(NULL));
  72.     int sz = 10;
  73.     int* pV = new int[sz];
  74.     const int min = -10;
  75.     const int max = 10;
  76.     for (int k = 0; k < sz; k++) {
  77.         pV[k] = min + rand() % (max - min + 1);
  78.     }
  79.     showArr(sz, pV);
  80.     // считаем количество
  81.     int sz_positive = 0, sz_negative = 0;
  82.     for (int k = 0; k < sz; k++) {
  83.         if (pV[k] > 0)
  84.             sz_positive++;
  85.         else if (pV[k] < 0) {
  86.             sz_negative++;
  87.         }
  88.     }
  89.     // выделяем память
  90.     int* pPositive = new int[sz_positive];
  91.     int* pNegative = new int[sz_negative]; 
  92.     // заполняем
  93.     int idPosit = 0, idNegat = 0;
  94.     for (int k = 0; k < sz; k++) {
  95.         if (pV[k] > 0)
  96.             pPositive[idPosit++] = pV[k];
  97.         else if (pV[k] < 0) {
  98.             pNegative[idNegat++] = pV[k];
  99.         }
  100.     }
  101.     cout << "Positive: ";
  102.     showArr(sz_positive, pPositive);
  103.     cout << "Negative: ";
  104.     showArr(sz_negative, pNegative);
  105.  
  106.    
  107.     /*
  108.     for (int k = 0; k < sz; k++) {
  109.         cout << pV[k] << " ";
  110.     }
  111.     */
  112.  
  113.  
  114.     cout << endl;
  115.     delete[] pPositive;
  116.     delete[] pNegative;
  117.     delete[] pV;
  118. }
  119.  
  120. void test5() {
  121.     const int sz = 5;
  122.     int arr_int[sz]{ 1, 2, 3, 4, 5 };
  123.     char arr_char[sz]{ 'a', 'b', 'c', 'd', 'e' };
  124.     double arr_double[sz]{ 1.5, 1.6, 1.7, 1.8, 1.9 };
  125.  
  126.     showArr(sz, arr_int);
  127.     showArr(sz, arr_char);
  128.     showArr(sz, arr_double);
  129.  
  130. }
  131.  
  132. void main() {
  133.     //task1();
  134.     //test2();
  135.     //task2();
  136.     //test3();
  137.     //test4();
  138.     test5();
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement