Question #100130

6. Create a class Fact. Each Fact has an integer ID. Ever Fact also has a list of Fact pointers. Ask the user for an integer N which is the number of Facts to be generated. After all of them are generated and added to a list, go back through and add a random number 0 to 5 Fact pointers to EACH of them. This creates a rather complex mesh of Facts. Display your mesh, then choose one of them at random and display all reachable facts. Do not report loops. For example



If the user enters N = 8


And the following Facts are created randomly:

#1 -> 2, 5

#2 -> none

#3 -> 4, 5, 7

#4 ->3, 6

#5 ->7

#6 ->1

#7 ->2


IF

#4 is chosen the result would be

#4 ->3, 6, 5, 7, 1, 2


since

4 connects to 3 and 6

3 connects to 4 , 5, 7

6 connects to 1

5 connects to 7

7 connects to 2

1 connects to 2, 5

2 connects to none


IF

#1 is chosen

The result would be

#1 ->2, 5, 7


Since

1 connects to 2, 5

2->none

5 connects to 7

7 connects to 2

Expert's answer

#include <iostream>

#include <cstdlib>

#include <list>




using namespace std;




class Fact

{

public:

  list<Fact *> pointers;

  int ID;

  void setPointers(list<Fact> facts);

  Fact(): ID(0){};

  ~Fact(){};

};







void factGenerator(int N, list<Fact> facts);




int main()

{

  int N;

  cout << "Enter number of Facts (N): ";

  cin >> N;

  list<Fact *> pointers;

  list<Fact> facts;

  factGenerator(N, facts);

  return 0;

}




void factGenerator(int N, list<Fact> facts)

{

  int i;

  Fact fact[N];

  for(i = 0; i <= N; i++)

  {

    cout << "Enter the fact #" << i+1 << ": ";

    cin >> fact[i].ID;

    facts.push_back(fact[i]);

  }

}







void Fact::setPointers(list<Fact> facts)

{

  int i;

  for (list<Fact>::iterator it = facts.begin(); it != facts.end(); ++it)

  {

    for(i = 0; i < (rand() % 6); i++)

    {

      *it.facts

    }

  }




}
LATEST TUTORIALS
APPROVED BY CLIENTS