I have been writing a program that is supposed to determine how long that the process has taken, however, it fails and always returns a value of 0.0000 seconds.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <time.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *tree;
struct node *InsertElement(struct node *, int); //declaration//
struct node *findLargestElement(struct node *);
void create_tree(struct node *);
void create_tree(struct node *tree)
{
tree=NULL; //resets the tree//
}
struct node *findLargestElement(struct node *tree)
{
if((tree==NULL)|| (tree->right==NULL))
return tree;
else
return findLargestElement(tree->right);
}
struct node *InsertElement(struct node *tree, int val)
{
struct node *ptr, *nodeptr, *parentptr;
ptr = (struct node*)malloc(sizeof(struct node)); //memory allocation//
ptr->data = val;
ptr->left = NULL;
ptr->right = NULL;
if(tree==NULL) //for root node//
{
tree=ptr;
tree->left=NULL;
tree->right=NULL;
}
else
{
parentptr=NULL;
nodeptr=tree;
while(nodeptr!=NULL)
{
parentptr=nodeptr;
if(val<nodeptr->data)
nodeptr=nodeptr->left; //if value is less than root go left//
else
nodeptr = nodeptr->right;//if more go right//
}
if(val<parentptr->data)//if less than parent go left//
parentptr->left = ptr;
else
parentptr->right = ptr;
}
return tree;
}
int main()
{
int option, val;
struct node *ptr;
clock_t begin, end;
create_tree(tree);
do
{
printf("\n\n\n\t\t*****Main Menu****\n\n");
printf("\t\t 1. Add new nodes: \n");
printf("\t\t 2. Find the largest element\n");
printf("\t\t 11. Exit\n");
printf("Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: printf("\nEnter the value of the new node:");
scanf("%d", &val);
tree= InsertElement(tree,val);
break;
case 2: begin=clock(); //timer begin//
ptr = findLargestElement(tree);
end=clock(); //timer ends//
double time_spent=(double)(end-begin)/CLOCKS_PER_SEC; //time elapsed//
printf("\nThe largest element is:%d\n", ptr->data);
printf("The time taken is %f end time is", time_spent);
break;
}
}while(option!=11);
return 0;
}
This is part of a program which will find the largest value in a binary search tree. (EDIT: I have added additional code for clarification. This program would have the user input the nodes for the tree and then rearrange the nodes accordingly, there is theoretically no limit for a number of nodes. My main question is once again is my implementation of the timer is correct? or is there another way to do it?) This is just part of the code that I have written and I would like to know whether there are any alternatives to time the process has taken or did I code it wrongly. I would like the time taken to be the elapsed time. Is this method only viable for loops only?
I have tried something like
begin=clock();
ptr = findLargestElement(tree);
end=clock();
printf("The start time %f and the end time is %f", begin,end)
it returns a start time of 0 and a large number as the end time, however converting it to seconds does not seem to work for me.
(Additional info: I have gone through the time.h documentation and it seems like the clock() should work, I have been trying other methods mentioned on StackOverflow but none of them seem to be working. Is it because I use a struct instead?) Thanks