Question

In: Computer Science

Create a family tree with a 3D vector in C++. Then, determine the worst, average and...

Create a family tree with a 3D vector in C++. Then, determine the worst, average and best time complexities of the program.

Solutions

Expert Solution

  1. 
    #include <stdlib.h>
    #include <string>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class Person
    {
        protected:
            string name;
            int ID;
            string gender;
        public:
            Person (string name, int ID, string gender)
            {
                this-> name = name;
                this-> ID = ID;
                this-> gender = gender;
            }
    
            const string getName ()
            {
                return name;
            }
    
            const int getID ()
            {
                return ID;
            }
    
            const string getGender ()
            {
                return gender;
            }
    
            vector <string> children;
    
    };
    class Male: public Person
    {
        public:
            Male (string name, int ID, string gender) : Person(name, ID, gender)
            {
            }
            void addWife (Person *arr [], string p1, string p2);
    
            void displayWifeChildren (Person *arr[], string p1)
            {
                int SIZE = 10;
                int i = 0;
                int j = 0;
    
                cout << "Children of " << p1 << "\n";
    
                while (i < wife.size () )
                {
                    cout << i + 1 << " " << wife [i] << "\n";
    
                                    j = 0;
                                    for(int k = 0; k < SIZE; k++)
                                    {
                                            if(arr [k] -> getName() == wife[i])
                                            {
                                                    while (j < arr [k] -> children.size () )
                                                    {
                                                            cout << "\t" << arr [k] -> children [j] << "\n";
                                                            j++;
                                                    }
                                            }
                                    }
    
                  i++;
                }
            }
    
    
        private:
            vector <string> wife;
    };
    
    
    class Female: public Person
    {
        public:
            Female (string name, int ID, string gender) : Person(name, ID, gender)
            {
            }
            void addChild (Person *arr [], string p1, string p2, string p3);
        private:
            vector <string> husband;
    };
    
    void displayAll (Person *arr[])
    {
        int SIZE = 10;
    
        cout  << "\nThe people in the system are...\n";
        for(int i = 0; i < SIZE; i++)
        {
            cout << arr[i] -> getName () << " " << arr [i] -> getID () <<  " " << arr [i] -> getGender () << "\n";
        }
        cout << "\n";
    }
    
    void Male:: addWife (Person *arr [], string p1, string p2)
    {
        int SIZE = 10;
    
        for (int i = 0; i < SIZE; i++)
        {
            if(arr[i]-> getName () == p1)
            {
                 wife. push_back (p2);
            }
        }
    }
    
    void Female:: addChild (Person *arr[], string p1, string p2, string p3)
    {
        int SIZE  = 10;
        for (int i = 0; i < SIZE; i++)
        {
            if(arr[i]-> getName () == p2)
            {
                children. push_back (p3);
            }
        }
    }
    
    /*
    void displayChildren (Person *arr[], string p1)
    {
        int SIZE = 10;
        for (int i = 0; i < SIZE; i++)
        {
            if (arr [i] -> getName () == p1)
            {
                cout << "Children of " << arr [i] -> getName () << " are ";
                ((Male *) arr [i]) -> displayWifeChildren (arr);
            }
        }
        cout << "\n";
    }
    */
    
    int main()
    {
    
        const int SIZE = 10;
        Person *arr[SIZE];
    
        arr [0] = new Male ("Abraham", 1, "M");
        arr [1] = new Female ("Sarah", 2, "F");
        arr [2] = new Female ("Keturah", 3, "F");
        arr [3] = new Female ("Hagar", 4, "F");
        arr [4] = new Male ("Issac", 5, "M") ;
        arr [5] = new Male ("Jokshan", 6, "M");
        arr [6] = new Male ("Midian", 7, "M");
        arr [7] = new Male ("Ishmael", 8, "M");
        arr [8] = new Female ("Rebekah", 9, "F");
        arr [9] = new Male ("Esau", 10, "M");
    
        displayAll(arr);
    
        Male *m = (Male *) arr [0];
        m -> addWife(arr, "Abraham", "Sarah"); /// Sarah is the wife of Abraham
        m -> addWife (arr, "Abraham", "Keturah");
        m -> addWife (arr, " Abraham ","Hagar");
    
        Female *f1 =  (Female *) arr [1];
        f1 -> addChild(arr, "Abraham", "Sarah", "Issac"); /// Issac is the child of Abraham and Sarah
    
        Female *f2 = (Female *) arr [2];
        f2 -> addChild(arr, "Abraham", "Keturah", "Jokshan");
        f2 -> addChild(arr, "Abraham", "Keturah", "Midian");
    
        Female *f3 = (Female *) arr [3];
        f3 -> addChild(arr, "Abraham", " Hagar", " Ishmael");
    
        m-> displayWifeChildren(arr, "Abraham");
    
        /*
        displayChildren(arr, "Abraham"); /// Displays all children of Abraham and groups each children with a wife
        */
        cout << "\nPress enter (or Ctrl-C) to quit ... " << endl;
            cin.get();
    
            for (int i = 0; i < 10; i++)
        {
            if ( arr [i] != NULL)
            {
                 delete arr[i];
            }
        }
    
        return 0;
    }








































































Related Solutions

C++ Instantiate a binary search tree object and create such tree using elements of the sequence...
C++ Instantiate a binary search tree object and create such tree using elements of the sequence 8,3,10, 1,6,9, 14, 4,7, 13 with 8 as root of the tree. Find maximum and minimum elements of the tree, successor(10) and predecessor(13), print the inorder, postorder and preorder traversal of the tree.
doing family tree
doing family tree
C++ Create a vector of student records (minimum 15+ records) which is unsorted (For Vector, refer...
C++ Create a vector of student records (minimum 15+ records) which is unsorted (For Vector, refer to Chapter 7.11 or 17.3, or you may use Array of structures) - You should read student records from a file into the vector in C++ or in Java program. - Display the unsorted 10+ student records (entire record, not just ID) and associated test scores. - Each Student record should include Student ID, Name, GPA, Student Address, and a pointer which points to...
Subject: Homework - Family Trees please give me C codes, thanks FAMILY TREE In a Binary...
Subject: Homework - Family Trees please give me C codes, thanks FAMILY TREE In a Binary Tree, each node has two children at most. To eliminate the restriction by linking the children together to form a list. In this design, each node in the tree needs to contain only two pointers: one to its eldest child and one to its next younger sibling. Using this representation, in each node, the pointer on the left always points down to child; the...
In basic C++ Create a function that takes in a vector, sorts it, then outputs to...
In basic C++ Create a function that takes in a vector, sorts it, then outputs to the console the result.
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t specify a size and don’t initialize with values. Starting with 2, pushing these into the back of the vector:   2, 4, 6, 8, 10 vector capacity (array size) changes and is dependent on the compiler. The size of the list is now 5. The vector capacity (array size) is 6. The back element is: 10 The front element is: 2 Now deleting the value at...
In c++ how do I search a vector for a name and then determine if the...
In c++ how do I search a vector for a name and then determine if the name is found?
The orbital angular momentum is a 3D vector. For an electron at a state associated with...
The orbital angular momentum is a 3D vector. For an electron at a state associated with quantum numbers (?, ?, ??), the magnitude of the orbital angular momentum is √?(? + 1) ℏ. The orbital angular momentum measured along the z axis, however, will be exactly ??ℏ. For a real orbital that combines two complex orbitals associated with two different ?? values, a measurement along the z axis will result in either of the two ?? values with an equal...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT