vladkomarr

acquaint

May 17th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <conio.h>
  4. #include "stdafx.h"
  5. #include <time.h>
  6. #pragma warning (disable : 4996)
  7. using namespace std;
  8.  
  9. class people {
  10. private:
  11.     int num;
  12.     int **matr;
  13.     int *visited;
  14. public:
  15.     void getNum();
  16.     void memory();
  17.     void showMatr();
  18.     void randomFill();
  19.     void result();
  20.     void dfs(int);
  21. };
  22.  
  23. void people::getNum() {
  24.     cout << "Enter the number of people: ";
  25.     cin >> num;
  26. }
  27.  
  28. void people::memory() {
  29.     matr = new int*[num];
  30.     for (int i = 0; i < num; i++)
  31.         matr[i] = new int[num];
  32.     visited = new int[num];
  33. }
  34.  
  35. void people::randomFill() {
  36.     srand((unsigned)time(NULL));
  37.     for (int i = 0; i < num; i++)
  38.     for (int j = 0; j < num; j++)
  39.     if (i < j)
  40.         matr[i][j] = rand() % 2;
  41.     else if (i == j)
  42.         matr[i][j] = 1;
  43.     for (int i = 0; i < num; i++)
  44.     for (int j = 0; j < num; j++)
  45.     if (i > j)
  46.         matr[i][j] = matr[j][i];
  47.  
  48.     for (int i = 0; i < num; i++)
  49.         visited[i] = 0;
  50. }
  51.  
  52. void people::showMatr() {
  53.     for (int i = 0; i < num; i++) {
  54.         for (int j = 0; j < num; j++)
  55.             cout << matr[i][j] << " ";
  56.         cout << endl;
  57.     }
  58. }
  59.  
  60. void people::dfs(int v) {
  61.     int i;
  62.     visited[v] = 1;
  63.     for (int i = 0; i < num; i++)
  64.     if (matr[i][v] && (!visited[i]))
  65.         dfs(i);
  66. }
  67.  
  68. void people::result() {
  69.     bool temp = true;
  70.     for (int i = 0; i < num; i++)
  71.     if (!visited[i])
  72.         temp = false;
  73.     if (temp)
  74.         cout << "We can acquaint these people.";
  75.     else cout << "We can't acquaint these people.";
  76. }
  77.  
  78. int main() {
  79.     people People;
  80.     People.getNum();
  81.     People.memory();
  82.  
  83.     People.randomFill();
  84.     People.showMatr();
  85.    
  86.     People.dfs(0);
  87.     People.result();
  88.  
  89.     getch();
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment