0

I am new to codeblocks. I was trying to find height of a BST and used max() function. But while compiling CodeBlock throws error.I am using Windows machine.I know linking maths.h manually will solve the issue.I know how to link math.h manually using gcc but i dont know gcc equivalent for windows.Basically i want to know is there any other solution besides linking??If manually linking is the only option left then how to do it on Windows on CodeBlocks.Thank you!!

undefined reference to max

I am pretty sure there is no error in logic all import statements are correct too. But still no output!! Here is the code:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

//Definition of Node for Binary search tree
struct BstNode {
    int data;
    struct BstNode* left;
    struct BstNode* right;
};
struct BstNode* GetNewNode(int);
struct BstNode* Insert(struct BstNode*,int );
int findHeight(struct BstNode *);
//struct BstNode* findMin(struct BstNode* root)
// Function to create a new Node in heap
struct BstNode* GetNewNode(int data) {
    struct BstNode* newNode = (struct BstNode*)malloc(sizeof(struct BstNode));
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;
}

// To insert data in BST, returns address of root node
struct BstNode* Insert(struct BstNode* root,int data) {//pass by value therefore * used and reurn stmt.otherwise pbarg ** no return
    if(root == NULL) { // empty tree
        root = GetNewNode(data);
    }
    // if data to be inserted is lesser, insert in left subtree.
    else if(data <= root->data) {
        root->left = Insert(root->left,data);
    }
    // else, insert in right subtree.
    else {
        root->right = Insert(root->right,data);
    }
    return root;
}
int findHeight(struct BstNode * root){
    if(root=NULL){
        return -1;
    }
    else{
        return max(findHeight(root->left),findHeight(root->right))+1;
    }

};
//BstNode* findMin(BstNode* root)
int main() {
    int a,b;
    struct BstNode* root = NULL;  // Creating an empty tree
    /*Code to test the logic*/
    root = Insert(root,15);
    root = Insert(root,10);
    root = Insert(root,20);
    root = Insert(root,25);
    root = Insert(root,8);
    root = Insert(root,12);
findHeight(root);
}
user3847870
  • 368
  • 1
  • 6
  • 21
  • what is the error it gives you? – ThunderWiring Sep 06 '15 at 12:31
  • 1
    possible duplicate of [C - undefined reference to sqrt (or other mathematical functions)](http://stackoverflow.com/questions/5248919/c-undefined-reference-to-sqrt-or-other-mathematical-functions) – aghidini Sep 06 '15 at 12:32
  • 1
    Where have you heard about a max() function? There's not such thing in `math.h` – jbm Sep 06 '15 at 12:38
  • i thought it was predefined inside math.h lib like sqrt()??Correct me if i am wrong :/ – user3847870 Sep 06 '15 at 12:39
  • `grep max /usr/include/math.h` yelds nothing. `apropos max` neither. You have to implement your max() func. – jbm Sep 06 '15 at 12:42
  • @user3847870 I haven't seen a standard `max` function. Just use an `if` statement to get the maximum instead or implement your own `max` function as @jbm suggests – Spikatrix Sep 06 '15 at 12:43
  • There is `fmax` only: http://port70.net/~nsz/c/c11/n1570.html#7.12.12.2 – too honest for this site Sep 06 '15 at 12:49
  • @jbm@Cool Guy sorry guys.You are right!!.There is not max() in math.h.Got confused jumping back to c after high lvl languages.Thank you for you time – user3847870 Sep 06 '15 at 12:51

1 Answers1

0
  1. You didn't declare max function.
  2. In findHeight function you did if(root=NULL) which should have been if(root==NULL). Do it like this:

    int findHeight(struct BstNode * root){
        if(root==NULL){
            return -1;
        } else {
            return max(findHeight(root->left),findHeight(root->right))+1;
        }
    }
    
  3. Don't forget to print the value of the findHeight.

surajs1n
  • 1,493
  • 6
  • 23
  • 34
  • 1
    Worked!!!!So silly mistake.I came back to C after working on python and it all got messed up.I thought max() was built in function in C too .Thanks man!! All this time i thought problem was in recursion .Never thought it would be an assignment operator.Thanks again!!!! – user3847870 Sep 06 '15 at 13:24
  • I can understand your feeling because it also happened to me once when i came back to **C++** after spending lot of my time with **R**. – surajs1n Sep 06 '15 at 15:20