Functions: Use ptrtest1.cpp to test part 1 Use ptrtest2.cpp to test part 2 Resul
ID: 3849593 • Letter: F
Question
Functions: Use ptrtest1.cpp to test part 1 Use ptrtest2.cpp to test part 2Results for part 1&2 should look exactly like this 1. (50 points] Complete ptrfuncs.cpp to implement the first four functions declared in ptrfuncs.h. All of the functions in ptrfuncs.h involve C++ pointers. Most of them also manipulate a collection of consecutively stored double values much like a double array. A pointer to the first value provides access. Hint/reminder: p is the same as pl 01, and plij is the same as (p i) Begin with this ptrfuncs.cpp skeleton and follow any instructions in that file. For this part, just implement the first four functions: double sum (double values int n) find and return the sum of the n values the pointer holds the address of the first one o double maxP tr (double values int n) find the maximum value, and return a pointer to its address. double *minPtr (double values int n) ditto the minimum value. double valueDiff(double left double right) find and return the difference between the two double values as left value minus right value Use ptrtest1.cpp to test your implementations. You can compile it and link to your functions, and then run it as follows cpp ptrfuncs.cpp gt -o ptrte Iptrtestl Your results must match our ptrtest1 results exactly. 2. (50 points] After your Part 1 testing is complete, implement the other two functions: o void printTable (double values int n, int perRow) print the values as a neatly-formatted table with the specified number of values per row, and each value in a field of width 10 showing 2 significant digits. void sortvalues (double *first double *last) sort the values from first through last, where these parameters are pointers into the same dynamic array. Write your own sorting routine, such as the selection sort procedure presented in Chapter 7-but remember you should use pointer notation, not array notation to do it. You may write helper functions if you want, but they should use pointer notation too Use ptrtest2.cpp to test your implementations. Compile it as you did above, subsituting ptrtest2 for ptrtest1 in both places. This program expects three command line arguments in this order: an input filename, the number of values to read from the file, and the number of values to print per row. Here are two sample runs using data1.bxt as the input file test2-out.txt. Your results must exactly match if we test your program the same way, but you should ensure the program works for different inputs too
Explanation / Answer
public category Node<T> worth gift within the node.
6
public int value;
7
8
//The respect to the left subtree.
9
public Node left;
10
11
//The respect to the correct subtree.
12
public Node right;
13
14
public Node(int value) price;
16
}
17
18
}
19
20
/**
21
* Represents the Binary Search Tree.
22
*/
23
public category BinarySearchTree the basis of the tree.
26
public Node root;
27
28
public BinarySearchTree insert(int value) come this;
34
}
35
36
insertRec(root, node);
37
come this;
38
}
39
40
non-public void insertRec(Node latestRoot, Node node) else
50
} else else
57
}
58
}
59
60
/**
61
* Returns the minimum worth within the Binary Search Tree.
62
*/
63
public int findMinimum()
67
Node currNode = root;
68
whereas (currNode.left != null)
71
come currNode.value;
72
}
73
74
/**
75
* Returns the utmost worth within the Binary Search Tree
76
*/
77
public int findMaximum()
81
82
Node currNode = root;
83
whereas (currNode.right != null)
86
come currNode.value;
87
}
88
89
/**
90
* Printing the contents of the tree in associate inorder approach.
91
*/
92
public void printInorder()
104
printInOrderRec(currRoot.left);
105
System.out.print(currRoot.value + ", ");
106
printInOrderRec(currRoot.right);
107
}
108
109
/**
110
* Printing the contents of the tree during a Preorder approach.
111
*/
112
public void printPreorder()
124
System.out.print(currRoot.value + ", ");
125
printPreOrderRec(currRoot.left);
126
printPreOrderRec(currRoot.right);
127
}
128
129
/**
130
* Printing the contents of the tree during a Postorder approach.
131
*/
132
public void printPostorder()
144
printPostOrderRec(currRoot.left);
145
printPostOrderRec(currRoot.right);
146
System.out.print(currRoot.value + ", ");
147
148
}
149
}
150
151
public category BinarySearchTreeDemo {
152
153
public static void main(String[] args) {
154
BinarySearchTree bst = new BinarySearchTree();
155
bst .insert(40)
156
.insert(25)
157
.insert(78)
158
.insert(10)
159
.insert(3)
160
.insert(17)
161
.insert(32)
162
.insert(30)
163
.insert(38)
164
.insert(78)
165
.insert(50)
166
.insert(93);
167
System.out.println("Inorder traversal");
168
bst.printInorder();
169
170
System.out.println("Preorder Traversal");
171
bst.printPreorder();
172
173
System.out.println("Postorder Traversal");
174
bst.printPostorder();
175
176
System.out.println("The minimum worth within the BST: " + bst.findMinimum());
177
System.out.println("The most worth within the BST: " + bst.findMaximum());
178
179
}
180
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.