Advertisement
vencinachev

C++K3

Jan 15th, 2021
1,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. struct Rat
  7. {
  8.     string name;
  9.     int age;
  10.     bool isRatted;
  11. };
  12.  
  13. void readRat(Rat& r)
  14. {
  15.     cout << "Rat name: ";
  16.     cin.ignore();
  17.     getline(cin, r.name);
  18.     cout << "Rat age: ";
  19.     cin >> r.age;
  20.     cout << "Rat isRatted (0/1): ";
  21.     int num;
  22.     cin >> num;
  23.     if (num == 0)
  24.     {
  25.         r.isRatted = false;
  26.     }
  27.     else
  28.     {
  29.         r.isRatted = true;
  30.     }
  31. }
  32.  
  33. void printRat(Rat& r)
  34. {
  35.     cout << "Rat info: " << endl;
  36.     cout << "Rat name: " << r.name << endl;
  37.     cout << "Rat age: " << r.age << endl;
  38.     cout << boolalpha << "Rat isratted: " << r.isRatted << endl;
  39. }
  40. const int SIZE = 20;
  41.  
  42. void readRatArr(Rat ratarr[], int n)
  43. {
  44.     for (int i = 0; i < n; i++)
  45.     {
  46.         readRat(ratarr[i]);
  47.     }
  48. }
  49.  
  50. void printRatArr(Rat ratarr[], int n)
  51. {
  52.     for (int i = 0; i < n; i++)
  53.     {
  54.         printRat(ratarr[i]);
  55.     }
  56. }
  57.  
  58. void swapRats(Rat& r1, Rat& r2)
  59. {
  60.     Rat temp = r1;
  61.     r1 = r2;
  62.     r2 = temp;
  63. }
  64.  
  65.  
  66. void selectionSort(Rat rarr[], int n)
  67. {
  68.     int i, j, min_idx;
  69.  
  70.     // One by one move boundary of unsorted subarray
  71.     for (int i = 0; i < n-1; i++)
  72.     {
  73.         // Find the minimum element in unsorted array
  74.         min_idx = i;
  75.         for (int j = i+1; j < n; j++)
  76.         if (rarr[j].age < rarr[min_idx].age)
  77.             min_idx = j;
  78.  
  79.         // Swap the found minimum element with the first element
  80.         swapRats(rarr[min_idx], rarr[i]);
  81.     }
  82. }
  83.  
  84. void enterMatrix(int a[][SIZE], int n, int m)
  85. {
  86.     for (int i = 0; i < n; i++)
  87.     {
  88.         for (int j = 0; j < m; j++)
  89.         {
  90.             cin >> a[i][j];
  91.         }
  92.     }
  93. }
  94.  
  95. void printMatrix(int a[][SIZE], int n, int m)
  96. {
  97.     for (int i = 0; i < n; i++)
  98.     {
  99.         for (int j = 0; j < m; j++)
  100.         {
  101.             cout << setw(3) << a[i][j];
  102.         }
  103.         cout << endl;
  104.     }
  105. }
  106.  
  107. int sumMatrix(int a[][SIZE], int n, int m)
  108. {
  109.     int sum = 0;
  110.     for (int i = 0; i < n; i++)
  111.     {
  112.         for (int j = 0; j < m; j++)
  113.         {
  114.           sum += a[i][j];
  115.         }
  116.     }
  117.     return sum;
  118. }
  119.  
  120. int fib(int n)
  121. {
  122.     if (n == 0 || n == 1)
  123.     {
  124.         return 1;
  125.     }
  126.     return fib(n - 2) + fib(n - 1);
  127. }
  128.  
  129.  
  130. int digitSum (int number)
  131. {
  132.     if (number == 0)
  133.     {
  134.         return 0;
  135.     }
  136.     return (number % 10) + digitSum(number / 10);
  137. }
  138.  
  139. int digitRoot(int number)
  140. {
  141.     if (number < 10)
  142.     {
  143.         return number;
  144.     }
  145.     digitRoot(digitSum(number));
  146. }
  147.  
  148. int main()
  149. {
  150.    /* int n = 3;
  151.     int m = 4;
  152.     int matrix[SIZE][SIZE];
  153.     enterMatrix(matrix, n, m);
  154.     printMatrix(matrix, n, m);
  155.     cout << "Sum = " << sumMatrix(matrix, n, m) <<endl;
  156.     */
  157.     cout << digitRoot(31198) << endl;
  158.     return 0;
  159. }
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement