Solved

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

Posted on 2003-03-28
Medium Priority
269 Views
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
Question by:fishopps
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 9
• 5
• 5
• +6

LVL 2

Expert Comment

ID: 8224952
Is this a homework question
0

LVL 30

Expert Comment

ID: 8225351
Please try to put some efforts on coding.... and if you face some problems/ errors, then ask us.

Mayank.
0

LVL 5

Expert Comment

ID: 8225662
What do these guys sell?
0

LVL 8

Expert Comment

ID: 8226082
>> What do these guys sell?

Does it really matter?
0

LVL 22

Assisted Solution

grg99 earned 450 total points
ID: 8227117
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

LVL 5

Assisted Solution

Kocil earned 450 total points
ID: 8228402
#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

LVL 1

Expert Comment

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

LVL 22

Expert Comment

ID: 8230393
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 Comment

ID: 8231324
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 Comment

ID: 8231343
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 Comment

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

Author Comment

ID: 8231371
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

LVL 5

Expert Comment

ID: 8240112
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

LVL 1

Accepted Solution

vamshi_pavan earned 600 total points
ID: 8243571
/*
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

LVL 1

Expert Comment

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

Author Comment

ID: 8244193
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 Comment

ID: 8244214
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 Comment

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

Author Comment

ID: 8244279
*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

LVL 1

Expert Comment

ID: 8244516
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

LVL 1

Expert Comment

ID: 8244561
sorry fishopps,
0

LVL 30

Expert Comment

ID: 8259702
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

LVL 30

Expert Comment

ID: 8417523

Mayank.
0

Author Comment

ID: 8435666
0

LVL 30

Expert Comment

ID: 8974397
How much more time, buddy??
0

LVL 20

Expert Comment

ID: 10199387
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

LVL 30

Expert Comment

ID: 10208216
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ouâ€¦
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
###### Suggested Courses
Course of the Month13 days, 19 hours left to enroll