Advertisement
RobertDeMilo

WB3.5 Инициализация переменных

Sep 4th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iomanip>
  6.  
  7. using namespace std;
  8.  
  9. void PrintParity(int x)
  10. {
  11.     string parity = (x % 2 == 0) ? "even" : "odd";
  12.     cout << x << " is " << parity << endl;
  13. }
  14.  
  15. string GetPositivity(int x)
  16. {
  17.     if (x > 0)
  18.     {
  19.         return "positive";
  20.     }
  21.     else if (x < 0)
  22.     {
  23.         return "negative";
  24.     }
  25.     else
  26.     {
  27.         return "zero";
  28.     }
  29. }
  30.  
  31. void PrintPositivity(int x)
  32. {
  33.     string positivity = GetPositivity(x);
  34.     cout << x << " is " << positivity << endl;
  35. }
  36.  
  37. int main()
  38. {
  39.     PrintParity(6);
  40.     PrintParity(7);
  41.     PrintPositivity(-1);
  42.     PrintPositivity(0);
  43.     PrintPositivity(1);
  44.  
  45.     return 0;
  46. }
  47.  
  48. Значение неинициализированной переменной неопределено!!!
  49.    
  50. Zero overhead principle не плати за то, что не используешь
  51.  
  52. int value;      // мне все равно, что будет в переменной value
  53. int value = 0;  // мне необходимо, чтобы в value был 0
  54.  
  55. Инициализируйте переменные при объявлении
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement