GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Jul-2009, 04:43
adibo adibo is offline
New Member
 
Join Date: Jul 2009
Posts: 2
adibo is on a distinguished road

void& binar trees


hi guys
i need to build a binar tree from a file.

i have a function that i must use
(Void initInventory(char *invFileName, double bal, chemTree *invTree)

the problem is whem i finish to build the tree and return to the menu the tree is empty again.

when i change the function to
chemTree* initInventory( char *invFileName, double bal, chemTree *invTree)
then it work ,
but its not allowed
thanks
  #2  
Old 11-Jul-2009, 10:13
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: void& binar trees


Quote:
Originally Posted by adibo
...
i have a function that i must use
(Void initInventory(char *invFileName, double bal, chemTree *invTree)
Were you given the function or were you given the function prototype and the assignment is to implement the function?

Quote:
Originally Posted by adibo
the problem is
How do you declare the variables are used in calling the function. Explain what each of the function parameters means. How do you call the function? (Show us some code.)
Quote:
Originally Posted by adibo
when i change the function...but its not allowed:

If it is not allowed, then don't do it.

Bottom line: It is my opinion that there is no way (that's no way) that someone can help you understand how to fix your code without seeing some code.

In other words: Show us some code. But I already said that.

Regards,

Dave
  #3  
Old 11-Jul-2009, 10:34
adibo adibo is offline
New Member
 
Join Date: Jul 2009
Posts: 2
adibo is on a distinguished road

Re: void& binar trees


CPP / C++ / C Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct buyInfo
{
char chem_name[41];
char chem_code[21];
long quantity;
long cost;
} buyInfo;

typedef struct invInfo
{
char chem_name[41];
char chem_code[21];
long quantity;
} invInfo;

typedef struct chemNode
{
invInfo data;
struct chemNode *left;
struct chemNode *right;
} chemNode;

typedef struct chemTree
{
double balance;
chemNode *root;
} chemTree;


int menu(chemTree *invTree,double bal);
chemTree* initInventory( char *invFileName, double bal, chemTree *invTree);
chemNode *insert(chemNode* Node,chemNode *head);
void free_tree(chemNode *head);
chemNode* newnode(char chem_name[41],char chem_code[21],long quantity,chemNode* left,chemNode* right)

void main()
{
int a;
chemTree*invtree=NULL;
double bal=0.0;

a=menu(invtree,bal);
getchar();
}
int menu(chemTree *invTree,double bal)
{	
	FILE*error=NULL;
	char newfile[200];
	char errfile[200];
	char invFileName[200],errFileName[200];
	char chem_name[41],chem_code[21];
	long quantity,cost;
	char choice=0;
	int x=1,saved=0,checker=0;

	while(x)
	{
		printf(MENU,bal);
		scanf("%1c",&choice);
		{
		switch(choice)
		{
			case'1':	
				{
					getchar();
				printf(FILE_NAME);
				scanf("%s",&newfile);
				printf("\n");
				printf(BALANCE);
				scanf("%lf",&bal);
				printf("\n");
				invTree=initInventory(newfile,bal,invTree);
		
				printf(ERROR);
				scanf("%s",&errfile);
				printf("\n");
				if(!(error))
					error= initErr(errfile, NULL);
				else
					{
					error= initErr(errfile, error);
					}
				break;
				}



chemTree* initInventory(char *invFileName,double bal, chemTree *invTree)
{
	
	chemNode*Node1;
	FILE*f1;
	int check;
	chemTree*thetree;
	long quant;
	char name[41],code[21];
	invInfo*inv;
	if(invTree)
	{
		free_tree(invTree->root);
	}
	invTree=(chemTree*)malloc (sizeof(chemTree));
	invTree->root=NULL;
	invTree->balance=bal;
	thetree=invTree;
f1=fopen(invFileName,"r");
if(!f1)
{
	printf(ERROR_OPEN);
	return NULL;
}
while(f1)
{
check=fscanf(f1,"%[^ ]%*c%[^ ]%*c%ld",&name,&code,&quant);
if(check==3)
{
Node1=newnode(name,code,quant,NULL,NULL);

inv=(invInfo*)malloc (sizeof(invInfo));
	strcpy(inv->chem_code,code);
	strcpy(inv->chem_name,name);
	inv->quantity=quant;
	thetree->root=insert(Node1,thetree->root);//ad[b]until here the tree is good//[/b]
}
else
{
	
	return ; 
}
}
}
i"m new i hope this is good..
thanks mate
Last edited by admin : 12-Jul-2009 at 04:21. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #4  
Old 11-Jul-2009, 11:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: void& binar trees


Quote:
Originally Posted by adibo
...i hope this is good...

Noit really

I hate to repeat myself but when you said you "have a function" that you must use, I asked:

Quote:
Originally Posted by davekw7x
Were you given the function or were you given the function prototype and the assignment is to implement the function?
I will now add: "Or what?" In other words, what, exactly, were you given?

Then I said
Quote:
Originally Posted by davekw7x
Explain what each of the function parameters means.

Now, as far as the nature of the problem: If you feed a pointer to a function, the function can not (that's not) affect the value of the pointer back in the calling function. It can affect whatever the pointer is pointing to. Therefore, if you are not allowed to return some new value of pointer, then you have to do something else:


Either

1.
You can pass the function a pointer to the pointer, and then have the function change the pointer. But when you say that you "have a function" that you must use, does that mean you can't change the function prototype or what?

or

2.
Another way might to make the pointer point to an "empty" node (representing the root of the tree) before calling the function. Then the function might add nodes or some such thing by linking to the root node. The pointer will still be pointing to the root, but the root itself has been changed.

Since we don't know what your assignment is and I am having a hard time trying to guess, what you have in mind, well...

Regards,

Dave
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Recommended reading for trees hira C Programming Language 1 05-Nov-2008 11:28
Implementing id3 algorithm/decision trees spyrax Miscellaneous Programming Forum 0 30-Oct-2007 23:05
Trees ajohn C Programming Language 2 14-Nov-2006 13:40
Trees and Keys (not for the faint-hearted!) greymalkin C++ Forum 11 08-Nov-2005 09:04
folder problem in trees zuzupus MySQL / PHP Forum 23 26-Sep-2003 08:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:44.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.