Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- struct Rat
- {
- string name;
- int age;
- bool isRatted;
- };
- void readRat(Rat& r)
- {
- cout << "Rat name: ";
- cin.ignore();
- getline(cin, r.name);
- cout << "Rat age: ";
- cin >> r.age;
- cout << "Rat isRatted (0/1): ";
- int num;
- cin >> num;
- if (num == 0)
- {
- r.isRatted = false;
- }
- else
- {
- r.isRatted = true;
- }
- }
- void printRat(Rat& r)
- {
- cout << "Rat info: " << endl;
- cout << "Rat name: " << r.name << endl;
- cout << "Rat age: " << r.age << endl;
- cout << boolalpha << "Rat isratted: " << r.isRatted << endl;
- }
- const int SIZE = 20;
- void readRatArr(Rat ratarr[], int n)
- {
- for (int i = 0; i < n; i++)
- {
- readRat(ratarr[i]);
- }
- }
- void printRatArr(Rat ratarr[], int n)
- {
- for (int i = 0; i < n; i++)
- {
- printRat(ratarr[i]);
- }
- }
- void swapRats(Rat& r1, Rat& r2)
- {
- Rat temp = r1;
- r1 = r2;
- r2 = temp;
- }
- void selectionSort(Rat rarr[], int n)
- {
- int i, j, min_idx;
- // One by one move boundary of unsorted subarray
- for (int i = 0; i < n-1; i++)
- {
- // Find the minimum element in unsorted array
- min_idx = i;
- for (int j = i+1; j < n; j++)
- if (rarr[j].age < rarr[min_idx].age)
- min_idx = j;
- // Swap the found minimum element with the first element
- swapRats(rarr[min_idx], rarr[i]);
- }
- }
- void enterMatrix(int a[][SIZE], int n, int m)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- cin >> a[i][j];
- }
- }
- }
- void printMatrix(int a[][SIZE], int n, int m)
- {
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- cout << setw(3) << a[i][j];
- }
- cout << endl;
- }
- }
- int sumMatrix(int a[][SIZE], int n, int m)
- {
- int sum = 0;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- sum += a[i][j];
- }
- }
- return sum;
- }
- int fib(int n)
- {
- if (n == 0 || n == 1)
- {
- return 1;
- }
- return fib(n - 2) + fib(n - 1);
- }
- int digitSum (int number)
- {
- if (number == 0)
- {
- return 0;
- }
- return (number % 10) + digitSum(number / 10);
- }
- int digitRoot(int number)
- {
- if (number < 10)
- {
- return number;
- }
- digitRoot(digitSum(number));
- }
- int main()
- {
- /* int n = 3;
- int m = 4;
- int matrix[SIZE][SIZE];
- enterMatrix(matrix, n, m);
- printMatrix(matrix, n, m);
- cout << "Sum = " << sumMatrix(matrix, n, m) <<endl;
- */
- cout << digitRoot(31198) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement