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

# 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
• 9
• 5
• 5
• +6
3 Solutions

Commented:
Is this a homework question
0

Associate Director - Product EngineeringCommented:
Please try to put some efforts on coding.... and if you face some problems/ errors, then ask us.

Mayank.
0

Commented:
What do these guys sell?
0

Commented:
>> What do these guys sell?

Does it really matter?
0

Commented:
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

Commented:
#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);
}
}

0

Commented:
I think so you can do a pretty neat job,by using a data structure like a linked list or tree.
0

Commented:
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

Author 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

Author 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

Author Commented:
Thanks a lot grg99 and locil...I need sometime to do the testing...
0

Author 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

Commented:
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

Commented:
/*
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 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')
{
}
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));
}

{
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')
{
}
else
{
p->l->l=NULL;
p->l->r=NULL;
printf("enter the sales amount\n");
scanf("%d",&(p->l->sales));
return ;
}

}
{
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')
{
}
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

Commented:
hope the above code is of some help to you
i have assumed each person has 2 people or nobody under him
0

Author 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

Author 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

Author Commented:
Sorry for asking so much...I will raise the point if I get a really satisfy answer.
0

Author 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

Commented:
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

Commented:
sorry fishopps,
0

Associate 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

do
{
if ( head != NULL )
{

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

} // end if

if ( head == NULL )
{
sum += head -> data ;

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

} // end if

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

return sum ;

} // end of total ()

Mayank.
0

Associate Director - Product EngineeringCommented:

Mayank.
0

Author Commented:
0

Associate Director - Product EngineeringCommented:
How much more time, buddy??
0

OwnerCommented:
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

jmcg
EE Cleanup Volunteer
0

Associate 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

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