Petro_zzz

lesson322_22

Oct 11th, 2023
1,064
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int summ(int a, int b) {
  6.     return a + b;
  7. }
  8.  
  9. void test_reference() {
  10.     int very_long_name = 18;
  11.     int *pv = &very_long_name;
  12.     int &ref = very_long_name;
  13.    
  14.     cout << very_long_name << " "
  15.         << *pv << " " << ref << endl;
  16.  
  17.     ++(*pv);
  18.     cout << very_long_name << " "
  19.         << *pv << " " << ref << endl;
  20.  
  21.     cout << &ref << " " << pv << " "
  22.         << &very_long_name << endl;
  23.  
  24.     int y = 222;
  25.     ref = y;
  26.  
  27.     cout << very_long_name << " "
  28.         << *pv << " " << ref << endl;
  29.  
  30.     int (*ref2)(int, int) = summ;
  31.     cout << ref2(13, 344) << endl;
  32.  
  33.     int arr[]{ 3,4,5,6,7,8,9 };
  34.     int& favorite = arr[4];
  35. }
  36.  
  37. void task1() {
  38.     int x = 4, y = 3;
  39.     int* px = &x, * py = &y;
  40.     cout << "max: " <<
  41.         ((*px > *py)? *px : *py);
  42. }
  43.  
  44. void task_pasport() {
  45.     int index[4]{ 6,0,2,1 };
  46.     int number[6]{ 7,3,2,7,7,7 };
  47.  
  48.     int* pas[]{ index, number };
  49.  
  50.     cout << pas[0][3] << endl;
  51.     cout << pas[1][2] << endl;
  52.  
  53.     for (int k = 0; k < 10; k++) {
  54.         cout << k % 2 << " ";
  55.     }
  56.     // pas[0][1];
  57. }
  58.  
  59. // BAD
  60. void my_swap1(int a, int b) {
  61.     int temp = a;
  62.     a = b;
  63.     b = temp;
  64.     cout << "In: ";
  65.     cout << a << " " << b << endl;
  66. }
  67.  
  68. // BAD
  69. void my_swap2(int *pa, int *pb) {
  70.     int* ptemp = pa;
  71.     pa = pb;
  72.     pb = ptemp;
  73.     cout << "In: ";
  74.     cout << *pa << " " << *pb << endl;
  75. }
  76.  
  77. void my_swap3(int *pa, int *pb) {
  78.     int temp = *pa;
  79.     *pa = *pb;
  80.     *pb = temp;
  81.     cout << "In: ";
  82.     cout << *pa << " " << *pb << endl;
  83. }
  84. void my_swap(int &a, int &b) {
  85.     int temp = a;
  86.     a = b;
  87.     b = temp;
  88.     cout << "In: ";
  89.     cout << a << " " << b << endl;
  90. }
  91.  
  92.  
  93.  
  94. void test_swap() {
  95.     int x = 6;
  96.     int y = 4;    
  97.     my_swap(x, y);
  98.     //my_swap3(&x, &y);
  99.    
  100.     cout << "Out: ";
  101.     cout << x << " " << y << endl;
  102.     // Output: 4 6
  103. }
  104.  
  105. int main(){
  106.     //test_reference();
  107.     //task1();
  108.     //task_pasport();
  109.     //test_swap();
  110. }
Advertisement
Add Comment
Please, Sign In to add comment