Simple mistake with static cast

I am writing a program using binary tree. I have an average to display, but each time I call it, the error produces "conversion from int to float, possible loss of data"

I know i'm likely using static cast incorrectly, just hoping someone could properly fix my mistake. Here's what I have

1
2
3
4
5
6
int treeSum, numNodes;
float avg;
     cout << "E. Sum and Average of the tree: " << endl;
     treeSum = root.sum();
     avg = static_cast<float>(treeSum / numNodes);
     cout << "\tSum: " << treeSum << " Average: " << fixed << setprecision(1) <<avg << endl << endl;
You lost accuracy through integer division before using the ugly static_cast<>(). Try
avg = static_cast<float>(treeSum) / numNodes;
or (asking for trouble here!)
avg = (treeSum + 0.0) / numNodes;

I recommend you use double rather than float as well.
Last edited on
Another question.

Why is the ';' needed at the end of the "method"? This snippet is from within a class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 void insert(node* cp, int target)  //inserting numbers into the tree
     {
          node* p;
          p = NULL;

          while (cp != NULL)
          {
               p = cp;
               if (target < cp -> num)
                    cp = cp -> LC;
               else
                    cp = cp -> RC;
          }

          cp = new node();
          cp -> num = target;
          cp -> cnt = 1;

          if (p == NULL)
               root = cp;
          if (p != NULL)
               if (target < p->num)
                    p -> LC = cp;
               else
                    p -> RC = cp;
     }

     void insert(int target)
     {
          insert(root, target);
     };
It's not needed.
Whats the point of having the small method of the same thing? If I delete the smaller one, the code fails
That's called function overloading, when two functions have the same name, but different numbers of parameters or types of parameters.

The purpose, in this case, is to separate the interface of the code from its implementation.

A user of the class is meant to call the 1-arg insert, because the user should not have to care about the fact that a tree is implemented with a root node, or that it has "nodes" or pointers at all. The user just cares that they have a tree, and can insert values (ints, in this case) into the tree.

The author of the code felt it was convenient to have the parent node passed as a parameter to the 2-arg insert function. Perhaps there were other parts of the code where insert(node*, int) was called without the node pointer being the root.

Ideally, the first insert function you show should be private to the class, and the second insert function is the public-facing one that the user can directly call.
Last edited on
Another inquiry

If i have a function that prints a list "in-order" can I add an 'if' statement to check for a delete function and incorporate 25 values instead of 20?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
     void printInorder(node* root, int& cnt)  //function to print the tree in order
     {
          if (root == NULL)
               return;
        //// Can I add something like this?
          if(deleteMethod)
              cnt < 25;
       //// or should i just create a second printInOrder method?
          if (cnt < 20)
          {
               cnt++;
               printInorder(root -> LC, cnt);
               cout << root -> num << " ";
               printInorder(root -> RC, cnt);
          }
     }

     void printInorder(int& cnt)
     {
          printInorder(root, cnt);
     }
          

To add: the program has me print the first 20 values of my tree, then when the delete method is called, i'm to reprint the first 25 values. and print the in order
Last edited on
I don't understand what you want, rephrase, show examples, draw a diagram
learn to communicate.
On my assignment there is a requirement to delete nodes that add up to be less than 10.

My question is, within my printInOrder method, can I add an 'IF' statement or any other statement that will allow the method to check IF the delete method has been used/called and then change its output quantity to 25 from 20, so i don't have to make another method just for the 25 numbers?

I'm a little slow, thats why my communcation is so awful. Apologies! I'm not quite sure how else to explain and or ask what i'm trying to convey.

Here is the current delete method. The PrintInOrder mehtod is above.
I'm wanting my code to verify that the delete has been called and jump to the in-order method and change its quantity from 20 to 25.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
     node* leafDelete(struct node* root) //deletes nodes in the tree <10
     {
          if (root == NULL)
               return NULL;
          if (root->LC == NULL && root->RC == NULL)
          {
               if (sumDigits(root->num) < 10)
               {
                    delete(root);
                    return NULL;
               }
          }
          root->LC = leafDelete(root->LC);
          root->RC = leafDelete(root->RC);

          return root;
     }
> check IF the delete method has been used/called
1
2
3
4
5
6
7
8
class tree{
	//...
	bool delete_method_was_called = false;
	node* leafDelete(struct node* root){
		this->delete_method_was_called = true; //¿when to reset?
		//...
	}
};
but that is awful, consider instead
1
2
3
4
5
void printInorder(node* root, int& cnt, int limit){
	if (cnt < limit)
		//recurse
}
printInorder(this->root, cnt, 42);



> Apologies! I'm not quite sure how else to explain and or ask what i'm trying to convey.
rephrase, show examples, draw a diagram
we are rubber ducks https://rubberduckdebugging.com/
I'm having a small issue with the digitsum within the leafdelete method.

Uncertain where or what is causing the issue. I believe the digit sum method works just fine. But when I call that method within the delete method, its not deleting the correct nodes, as 100 = 1 + 0 + 0 = 1 which is less than 10. and it's still being printed to the output.

The sumdigit is having issues with 3 digit numbers

Here is the LeafDelete method and the output I am getting from it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   node* leafDelete(struct node* root) //deletes nodes in the tree <10
     {
          if (root == NULL)
               return NULL;
          if (root->LC == NULL && root->RC == NULL)
          {
               if (sumDigits(root->num) < 10)
               {
                    delete(root);
                    return NULL;
               }
          }
          root->LC = leafDelete(root->LC);
          root->RC = leafDelete(root->RC);

          return root;
     }
OUTPUT:
L. Delete Nodes, sum is less than 10 (first 25) IN-ORDER:
100 107 111 115 116 117 123 127 130 134 136 138 139 140 141 161 208 328 369 500


I think the issue is the "(root -> num)" in the if statement.
Last edited on
ne555 wrote:
we are rubber ducks

ROFL! That link is definitely a keeper.

If no rubber duck is available try "Lucy 'splainin'" the code to a rock. A rock is a not-so distant relative to a CPU. Same steps apply as with a rubber duckie.

https://www.youtube.com/watch?v=Mh85R-S-dh8
Topic archived. No new replies allowed.