• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

How to calculate the total sales in the given title??

The title is like this: "A" is the sales manager and he has 2 downline, which is "B" and "C". "B" has 2 downline,which is "D" &"E"...

So..the question is ..I want to know the total sales result of "A" downline, which is B,C,D and E...but can't used for loop because it will be very messy if there are lots of downline...

0
fishopps
Asked:
fishopps
  • 9
  • 5
  • 5
  • +6
3 Solutions
 
honey_hamsterCommented:
Is this a homework question
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Please try to put some efforts on coding.... and if you face some problems/ errors, then ask us.

Mayank.
0
 
ecwCommented:
What do these guys sell?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ExceterCommented:
>> What do these guys sell?

Does it really matter?
0
 
grg99Commented:
The cleanest way is to use recursion-- now has your teacher mentioned recursion?   Were you listening?


So if you want to visit each node in this hierarchy, and assuming there are two subordinates to each level, you want to do a:


Traverse(  Boss );

where Traverse is:

procedure Traverse(  Here:  Person );
begin
    if Here <> NIL then
    begin
      TotSales := TotSales + Here.Sales;
      Traverse( Here.Left );
      Traverse( Here.Right );
    end;
end;

Person = record Sales: integer;  Left, Right: ^ Person; end;

0
 
KocilCommented:
#define DOWN_LINE_MAX

typedef struct Salesman {
  char name;
  int sales;
  TSalesman* downline[DOWN_LINE_MAX+1];
} TSalesman;

TSalesman se={'E', 40};
TSalesman sd={'D', 30};
TSalesman sc={'C', 20};
TSalesman sb={'B', 10, {sd,se}};
TSalesman sa={'A', 0, {sc,sd}};

int CalcRecursive(TSalesman *s)
{
   TSalesman *dl;
   int total = s->sales;
   for (dl=s->downline; dl != NULL; dl++) {
      total = total + CalcRecursive(dl);
   }
   return total;
}
   
0
 
vamshi_pavanCommented:
I think so you can do a pretty neat job,by using a data structure like a linked list or tree.
0
 
grg99Commented:
If you can't use recursion, then you need to keep a stack to keep track of what is "up" from where you are:

node * stack[ 10000 ];

every time you go "down", push your current place on the stack, then add in the node you've gone down to, then try to go down further.  ifyou can't go any further down, pop the stack and inspect the next node on that level.


0
 
fishoppsAuthor Commented:
sorry, This is not homework question!

This is actually a tough question, which need expertise to think about it...coz usually a programmer will use for loop to do it...but I dont want a for loop coz this it is tree...
0
 
fishoppsAuthor Commented:
ok..now if the question changed...there is a lot of level in the tree..and you do not know how many level there are...

so, wherever I point to the person..I want to know all of his/her downline's sales result..

remember no for loop!
0
 
fishoppsAuthor Commented:
Thanks a lot grg99 and locil...I need sometime to do the testing...
0
 
fishoppsAuthor Commented:
kocil...you suggest to use for loop right?
But if there are lots of subordinates then I have to do lots of for loop???!!
0
 
KocilCommented:
Yes man, you need recursive only for down to next level.
For direct subordinates, which are in the same level, use loop.

Please notice that I assumed the downline array stores the direct subordinates and terminated by NULL.



0
 
vamshi_pavanCommented:
/*
The title is like this: "A" is the sales manager and he has 2 downline, which is "B" and "C". "B" has 2 downline,which is "D" &"E"...

So..the question is ..I want to know the total sales result of "A" downline, which is B,C,D and E...but can't used for loop because it will be very messy if there are lots of downline...

*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node{
     int empno;
     int sales;
     struct node *r;
     struct node *l;
}root;

void lAdd(struct node*);
void rAdd(struct node*);
void display(struct node*);
struct node* search(int ,struct node*);
int totsales(struct node*);

void main()
{
     char ch;
         struct node *temp;
int eno;
printf("Enter the empno of Root\n");
scanf("%d",&root.empno);
printf("Do you have any people under you(y/n)\n");
ch=getch();
if(ch=='y')
{
     lAdd(&root);
     rAdd(&root);
}
else
{
          root.sales=0;
          root.r=NULL;
          root.l=NULL;
          exit(EXIT_SUCCESS);
}
display(&root);

printf("Enter the empolyee number for who to cal sales\n");
scanf("%d",&eno);
temp=search(eno,&root);
printf("%d",totsales(temp));
}

void lAdd(struct node *p)
{
          p->l=(struct node*)malloc(sizeof(struct node));
          printf("Enter the empno of \n");
          scanf("%d",&(p->l->empno));
          printf("Do you have any people under you(y/n)\n");
          if(getch()=='y')
          {
          lAdd(p->l);    
          rAdd(p->l);
          }
          else
          {
               p->l->l=NULL;
               p->l->r=NULL;
               printf("enter the sales amount\n");
               scanf("%d",&(p->l->sales));
               return ;
          }

}
void rAdd(struct node *p)
{
          p->r=(struct node*)malloc(sizeof(struct node));
          printf("Enter the empno of \n");
          scanf("%d",&(p->r->empno));
          printf("Do you have any people under you(y/n)\n");
          if(getch()=='y')
          {
          lAdd(p->r);    
          rAdd(p->r);
          }
          else
          {
               p->r->l=NULL;
               p->r->r=NULL;
               printf("enter the sales amount\n");
               scanf("%d",&(p->r->sales));
               return ;
          }

}


void display(struct node *p)
{    
printf("%d",p->empno);
if(p->l!=NULL)display(p->l);
if(p->r!=NULL)display(p->r);
}


struct node* search(int no,struct node *p)
{
     if(p->empno==no)
          return p;
     if(p->l!=NULL)search(no,p->l);
     if(p->r!=NULL)search(no,p->r);

}

int totsales(struct node*p)
{
     if(p->l==NULL && p->r==NULL)
          return p->sales;
     if(p->l!=NULL)
     p->sales+=totsales(p->l);
     if(p->r!=NULL)
          p->sales+=totsales(p->r);
     return p->sales;
}
0
 
vamshi_pavanCommented:
hope the above code is of some help to you
i have assumed each person has 2 people or nobody under him
0
 
fishoppsAuthor Commented:
Thanks vamshi pavan the code helps a lot,I will need sometime to verify it and is there any other way to do the search??

coz this is a large database, which not allow to use for loop to go node by node to search for data--time consuming...

And please do me a favor--add 2 more functions which is delete and modify employee info...

anyway, I heard someone said try doubly link list, is it true?!!can doubly link list do that? and how?!

0
 
fishoppsAuthor Commented:
Thanks vamshi pavan the code helps a lot,I will need sometime to verify it and is there any other way to do the search??

coz this is a large database, which not allow to use for loop to go node by node to search for data--time consuming...

And please do me a favor--add 2 more functions which is delete and modify employee info...

anyway, I heard someone said try doubly link list, is it true?!!can doubly link list do that? and how?!

0
 
fishoppsAuthor Commented:
Sorry for asking so much...I will raise the point if I get a really satisfy answer.
0
 
fishoppsAuthor Commented:
*For the people who do not know what I am asking for:

I want to know the most efficient way to do data search/add/delete and modify in a really huge size of database, which is in the form of tree.

for example: I want to know total sales of all the people below John and you do not know which level/node he is currently located.

obviously we can use for loop, start from first node to search for John until last node and then display the totsales of all the member below him. But what if the amount of database is unlimited...
0
 
vamshi_pavanCommented:
If you use a binary tree(like my above code) each employee has 2 or nobody under him.
If you use a doubly linked list (it is much easier than tree) each employee will have 1 or nobody under him.
0
 
vamshi_pavanCommented:
sorry fishopps,
i won't be able to help you regarding those 2 functions
0
 
Mayank SAssociate Director - Product EngineeringCommented:
If you're using a binary tree to hold the data, and the sum at a node will be equal to the total  of the sums of the left and right subtrees of this node, and the value at that node node):

The structure is something like:

typedef struct node
{
  struct node * left ;
  int data ; // holds the value to be summed
  struct node * right ;

} tree ;

int total ( tree * head ) // 'head' is a pointer pointing to the node at which the sum is to be calculated
{
    tree * stack[100] ;
    int top = -1, sum = 0 ;

    if ( head == NULL )
         return 0 ; // end if

    stack[++top] = head ;

    do
    {
         if ( head != NULL )
         {
              head = head -> left ;

              if ( head != NULL )
                   stack[++top] = head ; // end nested if

         } // end if

         if ( head == NULL )
         {
              head = stack[top--] ;
              sum += head -> data ;
              head = head -> right ;

              if ( head != NULL )
                   stack[++top] = head ; // end nested if

         } // end if

    } while ( top != -1 ) ; // end do-while

    return sum ;

} // end of total ()


Hope that helps! Please do ask if you face any problems.

Mayank.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
How about rating it now? If you are having some more problems, then please ask..

Mayank.
0
 
fishoppsAuthor Commented:
Please give me some time...
0
 
Mayank SAssociate Director - Product EngineeringCommented:
How much more time, buddy??
0
 
jmcgOwnerCommented:
Nothing has happened on this question in more than 6 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points [grade B] between grg99, kocil, and vamshi_pavan (partial answers).

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Actually, fishopps said: >> I want to know total sales of all the people below John and you do not know which level/node he is currently located.

And vamshi_pavan said: >> sorry fishopps, i won't be able to help you regarding those 2 functions

If you look at mayankeagle's code, it pretty much accomplishes the task of searching through the tree at any node and summing up the values beneath. But anyways, I guess I am posting this comment a little late.

Mayank.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 5
  • 5
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now