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
1
Expert's answer
2019-12-11T10:59:03-0500
#include<iostream>#include<cstdlib>#include<list>usingnamespace std;
classFact
{
public:
list<Fact *> pointers;
int ID;
voidsetPointers(list<Fact> facts);
Fact(): ID(0){};
~Fact(){};
};
voidfactGenerator(int N, list<Fact> facts);
intmain(){
int N;
cout << "Enter number of Facts (N): ";
cin >> N;
list<Fact *> pointers;
list<Fact> facts;
factGenerator(N, facts);
return0;
}
voidfactGenerator(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]);
}
}
voidFact::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
}
}
}
The expert did excellent work as usual and was extremely helpful for me.
"Assignmentexpert.com" has experienced experts and professional in the market. Thanks.
Comments