Starting Out With C++: Early Objects, Student Value Edition (9th Edition)
9th Edition
ISBN: 9780134379319
Author: Tony Gaddis, Judy Walters, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 14, Problem 12PC
Program Plan Intro
Ancestral Trees
Program Plan:
- Include required header files.
- Declare and initialize string arrays “people []”, “mother []”, and “father []”.
- Declare and initialize positions for the integer arrays “mom []”, and “pop []”.
- Give function prototype “ancestors ()”.
- Given function definition for “ancestors ()”.
- Print the name of the person.
- Check if mom’s position is not equal to -1.
- Call the function ““ancestors ()”.
- Check if pop’s position is not equal to -1.
- Call the function ““ancestors ()”.
- Define the function “main ()”. Inside this function,
- Print the ancestors of the person containing index “0” from “people []” array by calling the function “ancestors ()” by passing “0” as the argument.
- Print the ancestors of the person containing index “6” from “people []” array by calling the function “ancestors ()” by passing “6” as the argument.
- Return the statement.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
C programming I need help writing a code that uses a struct pointer into a binary tree and using the same pointer into an array
i) Create a Node class that will hold three fields i.e an integer element and a reference to the next Node along with a reference to the previous Node.
ii) Create a Dummy Headed Doubly Circular Linked list Abstract Data Type (ADT)named DoublyList.The elements in the list are Nodes consisting of an integer type key (all keys are unique) and a reference to the next node and a reference to the previous Node.
Use Python. Do not use Numpy
C++ Coding Assignment: Redo the 8 queens 1-dimensional array program with backtracking by REMOVING ALL GOTO's, but implementing the same algorithm.
GIVEN C++ CODE - WITH GOTO's:
#include <iostream>using namespace std;
int main() {
int q[8], c=0; q[0] = 0; // place a queen in row 0 of column 0 int counter = 0; // to count solutions // c: column as q[c]: row in column c
next_col: ++c; // or c++; // next column if(c==8) goto print; q[c]=-1; // 0
next_row: ++q[c]; // or q[c]++; // next row if(q[c]==8) goto backtrack; for(int i=0; i<c; ++i) if(q[i]==q[c] || (c-i)== abs(q[c]-q[i])) // || = row test OR diagonal tests goto next_row; goto next_col;
backtrack: --c; // or c-- if(c==-1) // once we backtrack to column -1, we have all the solutions. return 0; // ends the program goto next_row;
print: counter++; // new solutions cout << counter << ":" << endl; for(int…
Chapter 14 Solutions
Starting Out With C++: Early Objects, Student Value Edition (9th Edition)
Ch. 14.1 - What is a recursive functions base case?Ch. 14.1 - What happens if a recursive function does not...Ch. 14.1 - Prob. 14.3CPCh. 14.1 - What is the difference between direct and indirect...Ch. 14 - What type of recursive function do you think would...Ch. 14 - Which repetition approach is less efficient; a...Ch. 14 - When should you choose a recursive algorithm over...Ch. 14 - Prob. 4RQECh. 14 - Prob. 5RQECh. 14 - Prob. 6RQE
Ch. 14 - Predict the Output 7. What is the output of the...Ch. 14 - Soft Skills 8. Programming is communication; the...Ch. 14 - Prob. 1PCCh. 14 - Recursive Conversion Convert the following...Ch. 14 - Prob. 3PCCh. 14 - Recursive Array Sum Write a function that accepts...Ch. 14 - Prob. 5PCCh. 14 - Recursive Member Test Write a recursive Boolean...Ch. 14 - Prob. 7PCCh. 14 - Prob. 8PCCh. 14 - Ackermanns Function Ackermanns function is a...Ch. 14 - Prefix to Postfix Write a program that reads...Ch. 14 - Prob. 11PCCh. 14 - Prob. 12PC
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- creating tree containers: one that uses a vector to hold your trees (class VectorContainer). Each of these container classes should be able to hold any amount of different expressions each of which can be of any size.see example CODE GIVEN class Container {protected:Sort* sort_function; public:/* Constructors */Container() : sort_function(nullptr) { };Container(Sort* function) : sort_function(function) { }; /* Non Virtual Functions */void set_sort_function(Sort* sort_function); // set the type of sorting algorithm /* Pure Virtual Functions */// push the top pointer of the tree into containervirtual void add_element(Base* element) = 0;// iterate through trees and output the expressions (use stringify())virtual void print() = 0;// calls on the previously set sorting-algorithm. Checks if sort_function is not null, throw exception if otherwisevirtual void sort() = 0; /* Essentially the only functions needed to sort *///switch tree locationsvirtual void swap(int i, int j) = 0;// get top ptr…arrow_forwardcreating tree containers: one that uses a vector to hold your trees (class VectorContainer). Each of these container classes should be able to hold any amount of different expressions each of which can be of any size.see example CODE GIVEN class Container {protected:Sort* sort_function; public:/* Constructors */Container() : sort_function(nullptr) { };Container(Sort* function) : sort_function(function) { }; /* Non Virtual Functions */void set_sort_function(Sort* sort_function); // set the type of sorting algorithm /* Pure Virtual Functions */// push the top pointer of the tree into containervirtual void add_element(Base* element) = 0;// iterate through trees and output the expressions (use stringify())virtual void print() = 0;// calls on the previously set sorting-algorithm. Checks if sort_function is not null, throw exception if otherwisevirtual void sort() = 0; /* Essentially the only functions needed to sort *///switch tree locationsvirtual void swap(int i, int j) = 0;// get top ptr…arrow_forwardImplements the clone_node function that duplicates. If the node has a next it also duplicates it Hint: implement the following recursive pseudocode function clone_node(node) if node is NULL returns NULL returns new_node(node.value, clone_node(node.next)) Complete the code: def clone_node(node): r = <... YOUR CODE HERE ...> return rarrow_forward
- 13.14 LAB: Library book sorting Two sorted lists have been created, one implemented using a linked list (LinkedListLibrary linkedListLibrary) and the other implemented using the built-in ArrayList class (ArrayListLibrary arrayListLibrary). Each list contains 100 books (title, ISBN number, author), sorted in ascending order by ISBN number. Complete main() by inserting a new book into each list using the respective LinkedListLibrary and ArrayListLibrary insertSorted() methods and outputting the number of operations the computer must perform to insert the new book. Each insertSorted() returns the number of operations the computer performs. Ex: If the input is: The Catcher in the Rye 9787543321724 J.D. Salinger the output is: Number of linked list operations: 1 Number of ArrayList operations: 1 Which list do you think will require the most operations? Why?arrow_forward13.14 LAB: Library book sorting Two sorted lists have been created, one implemented using a linked list (LinkedListLibrary linkedListLibrary) and the other implemented using the built-in ArrayList class (ArrayListLibrary arrayListLibrary). Each list contains 100 books (title, ISBN number, author), sorted in ascending order by ISBN number. Complete main() by inserting a new book into each list using the respective LinkedListLibrary and ArrayListLibrary insertSorted() methods and outputting the number of operations the computer must perform to insert the new book. Each insertSorted() returns the number of operations the computer performs. Ex: If the input is: The Catcher in the Rye 9787543321724 J.D. Salinger the output is: Number of linked list operations: 1 Number of ArrayList operations: 1 Which list do you think will require the most operations? Why?…arrow_forwardDefine the 3 bolded functions for the following DynIntStack (linked list): class DynIntStack {private: struct Node { int value; // Value in the node Node *next; // Pointer to the next node }; Node *top; // Pointer to the stack toppublic: DynIntStack() { head = nullptr; } void push(int); //assume this is already defined void removeTop(); // removes the top element without returning it int topValue(); // returns the top element without removing it bool isEmpty() { return head == nullptr; } bool isFull() { return false; } void pushMany(int values[], int n); //add n values from the array}; Hints: void removeTop() (hint 3 lines of code) int topValue() (hint 1 line of code) void pushMany(int values[], int n) (hint 2 lines of code, use a for loop, call another function)arrow_forward
- C programming I need a code that asks the user for how many times it'll run and run that many time and ask the user for a number and it puts that number into a binary search tree and an array using diffrent functionsarrow_forwardLanguage/Type: C++ binary trees pointers recursion Write a function named hasPath that interacts with a tree of BinaryTreeNode structures representing an unordered binary tree. The function accepts three parameters: a pointer to the root of the tree, and two integers start and end, and returns true if a path can be found in the tree from start down to end. In other words, both start and end must be element data values that are found in the tree, and end must be below start, in one of start's subtrees; otherwise the function returns false. If start and end are the same, you are simply checking whether a single node exists in the tree with that data value. If the tree is empty, your function should return false. For example, suppose a BinaryTreeNode pointer named tree points to the root of a tree storing the following elements. The table below shows the results of several various calls to your function: 67 88 52 1 21 16 99 45 Call Result Reason hasPath(tree, 67, 99) true path exists…arrow_forward1.) Design a Binary Search Tree ADT by using following functions. Include the function definitions & run the program (Kindly include header files as well). Paste the output as a screenshot and write the code in your answer sheet. insert(int val)find(int x)************************************************************************************ class btNode { public: int info; btNode *lLink; btNode *rLink; btNode(int e, btNode *l = NULL, btNode *r = NULL) { info = e; lLink = l; rLink = r; } btNode() { lLink = NULL; rLink = NULL; } }; class binarySTADT { private: btNode *root; int count = 0; public: binarySTADT() { root = NULL; } void insert(int val); // function to insert a given value in the tree.…arrow_forward
- Please convert the code in C language #include <bits/stdc++.h>using namespace std; class Node{ public: int data; //value Node *left; //pointer to left child Node *right; //pointer to right child}; // creating new nodeNode* newnode(int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } Node* LCA(Node *root, int n1, int n2){while(true){ if((root->data>=n1 && root->data<=n2)||(root->data<=n1 && root->data>=n2)) return root; if(n1<root->data) root=root->left; else root=root->right; }} int main(){ cout<<"tree is built as per 1st example\n"; Node *root=newnode(8); root->left= newnode(4); root->right= newnode(10); root->right->right=newnode(11); root->right->left=newnode(9); root->left->left=newnode(3);…arrow_forward1. Do some research about binary search trees. 2. Create a structure in C for the nodes of this tree. Which variables do the structure have? 3. Write a function that builds a balanced binary search tree. The input argument of this function is a sorted array of integers. 4. Write a recursive function that inserts a new element to the tree. The input argument of this function is a pointer to the head node of the tree. 5. What is the time complexity of this insertion function?arrow_forwardC++ PROGRAMMINGTopic: Binary Search Trees Explain the c++ code below.: SEE ATTACHED PHOTO FOR THE PROBLEM INSTRUCTIONS It doesn't have to be long, as long as you explain what the important parts of the code do. (The code is already implemented and correct, only the explanation needed) #include "node.h" #include <iostream> using namespace std; class BTree { node* root; int size; node* create_node(int num, node* parent) { node* n = (node*) malloc( sizeof(node) ); n->element = num; n->parent = parent; n->right = NULL; n->left = NULL; return n; } public: BTree() { root = NULL; size = 0; } node* left(node* p) { return p->left; } node* right(node* p) { return p->right; } node* sibling(node* p){ if(p != root){ node* P = p->parent; if(P->left != NULL && P->right != NULL){…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education