avr39ripe

cppFunctionBasics

Aug 2nd, 2021
593
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. enum Colors {red, yellow, green};
  4.  
  5. int generateColor()
  6. {
  7.     return rand() % 3;
  8. }
  9.  
  10.  
  11. void decodeColor(int color)
  12. {
  13.     if (color == red) { std::cout << "red"; }
  14.     else if (color == yellow) { std::cout << "yellow"; }
  15.     else if (color == green) { std::cout << "green"; }
  16.     else { std::cout << "error! no such color!\n"; }
  17. }
  18.  
  19. void printArr(int arr[], int arrSize)
  20. {
  21.     for (int i{ 0 }; i < arrSize; ++i)
  22.     {
  23.         std::cout << arr[i] << ' ';
  24.     }
  25.     std::cout << '\n';
  26. }
  27.  
  28. void sayHello(bool glad, bool die)
  29. {
  30.     std::cout << "Hello!!! :)\n";
  31.     if (die) { return; }
  32.     if (glad) { std::cout << "Glad to see you!\n"; }
  33.     else { std::cout << "Oh no! You are unwelcome!\n"; }
  34.     return;
  35. }
  36.  
  37. int max(int a, int b)
  38. {
  39.     return a > b ? a : b;
  40. }
  41.  
  42. void maxBad()
  43. {
  44.     int a{};
  45.     int b{};
  46.  
  47.     std::cout << "Enter first val\n";
  48.     std::cin >> a;
  49.  
  50.     std::cout << "Enter second val\n";
  51.     std::cin >> b;
  52.  
  53.     std::cout << "MAX = " << (a > b ? a : b) << '\n';
  54. }
  55.  
  56. int retTwo()
  57. {
  58.     int x{ 10 };
  59.     int y{ 20 };
  60.     return x, y, ++y , x + y, x++;
  61. }
  62.  
  63. //greetUser(3,true);
  64. //greetUser(usersCount);
  65.  
  66. void swap(int a, int b)
  67. {
  68.     std::cout << "swap start: a = " << a << " b = " << b << '\n';
  69.     int copy{ a };
  70.     a = b;
  71.     b = copy;
  72.     std::cout << "swap finish: a = " << a << " b = " << b << '\n';
  73. }
  74.  
  75. void greetUser(int numUsers)
  76. {
  77.     for (int i{ 0 }; i < numUsers; ++i)
  78.     {
  79.         std::cout << "Hello dear, user! :)\n";
  80.         std::cout << "Have a nice day!\n";
  81.     }
  82. }
  83.  
  84. int main()
  85. {
  86.     const int arrSize{ 5 };
  87.     int values[arrSize]{ 1,2,4,7,0 };
  88.     printArr(values, arrSize);
  89.  
  90.     printArr(values, arrSize);
  91.  
  92.     const int arrSize1{ 10 };
  93.     int values1[arrSize1]{ 1,2,3,4,5,6,7,8,9,10 };
  94.     printArr(values1, arrSize1);
  95.  
  96.     return 0;
  97.    // sayHello(true, true);
  98.  
  99.     int mainVar{ 42 };
  100.     int usersCount{3};
  101.  
  102.     int numX{ 110 };
  103.     int numY{ 20 };
  104.  
  105.  
  106.     std::cout << "Enter first val\n";
  107.     std::cin >> numX;
  108.  
  109.     std::cout << "Enter second val\n";
  110.     std::cin >> numY;
  111.  
  112.     std::cout << max(numX, numY) << '\n';
  113.  
  114.  
  115.     //maxBad();
  116.  
  117.     int res{};
  118.  
  119.     res = max(numX , 45);
  120.  
  121.     //int res{ max(23, (max(numX,numY) + 56 / 4)) };
  122.     //std::cout << "MAX is: " << max(numX, numY) << '\n';;
  123.     //std::cout << "MAX is: " << res << '\n';
  124.     return 0;
  125.     //std::cout << "inside main start: numX = " << numX << " numY = " << numY << '\n';
  126.     //swap(numX,40);
  127.     //std::cout << "inside main after swap: numX = " << numX << " numY = " << numY << '\n';
  128.    
  129.  
  130.  
  131.     greetUser(usersCount);
  132.  
  133.     //std::cout << numUsers;
  134.     // do some initial work
  135.     std::cout << "Initial work\n";
  136.  
  137.     // do another pice of work...
  138.      // and more work
  139.     greetUser(2);
  140.  
  141.     std::cout << "Main work\n";
  142.     std::cout << "We try hard..\n\n";
  143.  
  144.     greetUser(4);
  145.  
  146.     return 0;
  147. }
  148.  
  149.  
  150.  
  151.  
RAW Paste Data