I have 2 Function : void AddElements(TreeType& tree, int info[], int fromIndex,
ID: 3803778 • Letter: I
Question
I have 2 Function :
void AddElements(TreeType& tree, int info[], int fromIndex, int toIndex)
{
int midIndex;
if (fromIndex <= toIndex)
{
midIndex = (fromIndex + toIndex) / 2;
tree.PutItem(info[midIndex]);
AddElements(tree, info, fromIndex, midIndex - 1);
// Complete the left subtree.
AddElements(tree, info, midIndex+1, toIndex);
// Complete the right subtree.
}
}
void MakeTree(TreeType& tree, int info[], int length)
// Creates a binary tree from a sorted array.
{
tree.MakeEmpty();
AddElements(tree, info, 0, length-1);
}
So I want to read int array from text file and put to info[] like this:
content text file:
IsEmpty
PutItem P
PutItem F
PutItem S
PrintTree
MakeTree -100, -97 -23 -44 -69 -67 -55 -67 -9 -13 -9 -3 8 32 59 70 22 28 33 41 89 79 24 39 85
invoke in main.CPP :
else if (command == "MakeTree")
{
int length=25;
int ar[length];
for(int i=0;i inFile>>ar[i];
}
tree.MakeTree(tree,ar,length);
}
But I get an error, anybody can help me for doing this assignment?
152 153 154 else if command "Print Ancestors") 155 E 156 157 158 outFileExplanation / Answer
a)
The problem here is with the array ar we can resolve this using the below 2 solutions
1) Define the second parameter in the function MakeTree as int * type like below
void MakeTree(TreeType& tree, int *info, int length)
2) Pass the array ar as ar[25] in the function call MakeTree
tree.MakeTree(tree,ar[25],25)
in this case there will be no changes in void MakeTree(TreeType& tree, int info[], int length).
b)
Error may also occur if object to the class is not defined properly, please check in which class MakeTree function
is defined and create an object accord to that and call the MakeTree function.
error may be in tree.MakeTree(tree,ar,25);
check for the object tree is properly defined to the class where the function MakeTree is present.
c)
If you are calling the function in the same class please try MakeTree(tree,ar,25); dont put tree.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.