Hi everyone, I have a couple of questions?
1.this is part of my main function - //////////////////////////////
-
int main()
-
{
-
FILE *f1;
-
-
int i,j;
-
j=100000000;
-
int intData[1000];
-
f1=fopen("out3.txt","wt");
-
-
/* Generating 1000 integers from 100,000,001 to 100,001,000 */
-
for (i =0; i <1000; i++)
-
{
-
intData[i] = ++j;
-
}
-
/////////////////////////////
when i try to compile it i get syntax error before j as well as f1?
Can anyone provide clues on why is that happening?
2. - //////////////////////////
-
struct t_node
-
{
-
void *item;
-
struct t_node *left;
-
struct t_node *right;
-
} node;
-
//////////////////////////
i get an warning : structure defined inside inside parms on compiling - //////////////////////////////
-
struct t_collection
-
{
-
/* Note that size is not needed any longer! */
-
int (*ItemCmp)( void *, void * );
-
struct t_node *node;
-
};
-
////////////////////////////////////////
even this one gives 2 warning saying that
warning : structure defined inside parms
warning: empty declarations
Any help is appreciated. Thanks in advance.
18 3752
need you a comma between i and j as you declare them?
i also believe that open() is a function . . . use
try that, then recompile.
well, i guess the comma shows up now with the code tags . . .
youre trying to store a value in j that is too big for an int. (may or may not be the problem) use a long int.
also, when you embed a t_node into your other structs, you need not use the keyword struct again . . . it tells the compiler you are defining the struct again.
also, when you embed a t_node into your other structs, you need not use the keyword struct again . . . it tells the compiler you are defining the struct again.
This may or may not be true...I think in C you always have to use the keyword struct in front of a variable declaration (for types of that struct) - in other words, he has to use struct t_node *left;. If this is in C++ then I agree with you; if not I'm not so sure.
This may or may not be true...I think in C you always have to use the keyword struct in front of a variable declaration (for types of that struct) - in other words, he has to use struct t_node *left;. If this is in C++ then I agree with you; if not I'm not so sure.
Thanks for replying. My program is in C. Those warning show up only when I am compiling in linux platform, not in cygwin for some reason.
changing int to long int did not help
basically it says a syntax error before this line
j=10000000;
even f1.open does not cause any diff. it is still showing the same errors.
thanks
My program is in C.
sorry, i was simply assuming.
i know far less about c than i do about c++, but i compiled almost exactly what you posted: -
#include <stdio.h>
-
-
int main()
-
{
-
FILE *f1;
-
-
int i, j;
-
j=100000000;
-
int intData[1000];
-
f1=fopen("out3.txt","wt");
-
-
/* Generating 1000 integers from 100,000,001 to 100,001,000 */
-
for (i =0; i <1000; i++)
-
{
-
intData[i] = ++j;
-
fprintf(f1,"%d",intData[i]); //added this
-
}
-
}
-
-
struct t_node
-
{
-
void *item;
-
struct t_node *left;
-
struct t_node *right;
-
} node;
-
-
struct t_collection
-
{
-
/* Note that size is not needed any longer! */
-
int (*ItemCmp)( void *, void * );
-
struct t_node *node;
-
};
-
and i got an output file with the appropriate numbers all over it. funny thing is that im pretty sure my compiler thought i was writing in c++ . . . which, needless to say, is odd . . .
so is my code correct, should i try to compile it with g++ then?
. . . might be worth a shot. is there any particular reason youre using c?
. . . might be worth a shot. is there any particular reason youre using c?
ya i am using c because i have been asked to write it in c
well, ive been fooling around with your code. i got my compiler thinking in c and found the errors you reported . . . and for some reason, initializing j on the same line you declare it seemed to fix it: - #include <stdio.h>
-
-
int main()
-
{
-
FILE *f1;
-
-
int i, j = 100000000;
-
int intData[1000];
-
f1=fopen("out3.txt","wt");
-
-
/* Generating 1000 integers from 100,000,001 to 100,001,000 */
-
for (i =0; i <1000; i++)
-
{
-
intData[i] = ++j;
-
fprintf(f1,"%d",intData[i]); //added this
-
}
-
}
-
-
struct t_node
-
{
-
void *item;
-
struct t_node *left;
-
struct t_node *right;
-
} node;
-
-
struct t_collection
-
{
-
/* Note that size is not needed any longer! */
-
int (*ItemCmp)( void *, void * );
-
struct t_node *node;
-
};
which seems really wierd to me since, theoretically, both ways should be valid. (if anyone else knows something here, please jump right in . . .)
well dude its still not working for me.
I think its time i tell you more about my code and the compiling errors which I am getting. I have been asked to implement a binary tree structure with the foll requirements
1) Generate 1000 integers from 100,000,001 to 100,001,000
2) Create a collection and add first 500 integers
3) Delete 400 integers from 101st to 500th.
4) Add the rest of 500 integers
5) Find all the odd numbers in the collection and delete all of them.
6) Your inordertraversal function writes all the items in the collection into a file named output3.txt as below.
100000002
100000004
100000006
....
i have written the code with 3 files which are as follows:
////////////////////////////////////////
main.c
/////////////////////////////////
#include <assert.h>
#include <string.h>
#include <math.h>
#include "coll_a.h" /* import the specification */
collection myCollection;
int max_items;
int private_print(void *item);
int ItemCmp(void *a, void *b);
int main()
{
FILE *f1;
int i,j=100000000;
int intData[1000];
f1=fopen("out3.txt","wt");
/* Generating 1000 integers from 100,000,001 to 100,001,000 */
for (i =0; i <1000; i++)
{
intData[i] = ++j;
}
max_items = 1000;
j = 0;
myCollection = ConsCollection(max_items,ItemCmp);
printf("adding 500 items\n");
for (i = 0; i <500; i++)
{
AddToCollection(myCollection, (void *)&intData[j ++]);
}
printf("deleting 400 items\n");
for (i = 0; i <400; i++)
{
DeleteFromCollection(myCollection, (void *)&intData[--j]);
}
printf("adding the rest of 500 items\n");
j+=400;
for (i = 0; i <500; i++)
{
AddToCollection(myCollection, (void *)&intData[++j]);
}
for (i=0;i <100;i++)
{
if((intData[i]%2)!=0)
{DeleteFromCollection(myCollection, (void *)&intData[i]);
}
for (i=500;i<1000;i++)
{
if((intData[i]%2)!=0)
{DeleteFromCollection(myCollection, (void *)&intData[i]);
}
}
InOrderTraversal(c,private_print);
}
int ItemCmp(void *a, void *b)
{long *d1,*d2;
*d1=(long *)a;
*d2=(long *)b;
if(*d1>*d2)
return -1;
else if(*d1<*d2)
return 1;
else
return 0;
}
int private_print(void *item)
{long *t;
*t=(long *)item;
if(t!=NULL)
{printf("%d",*t );
fprintf (f1, "\n%d",*t);
}
else
{
return 0;
}
}
///////////////////////////////////////////
/////////////////////////////////////////////////////////
coll_a.h
///////////////////////////////////////
/* Specification for collection */
typedef struct t_collection *collection;
collection ConsCollection( int max_items,
int (*ItemCmp)(void *, void *) );
/* Construct a new collection
Pre-condition: max_items > 0
Post-condition: returns a pointer to an empty collection
*/
void AddToCollection( collection c, void *item );
/* Add an item to a collection
Pre-condition: (c is a collection created by a call to
ConsCollection) &&
(existing item count < max_items) &&
(item != NULL)
Post-condition: item has been added to c
*/
void DeleteFromCollection( collection c, void *item );
/* Delete an item from a collection
Pre-condition: (c is a collection created by a call to
ConsCollection) &&
(existing item count >= 1) &&
(item != NULL)
Post-condition: item has been deleted from c
*/
void *FindInCollection( collection c, void *key );
/* Find an item in a collection
Pre-condition: c is a collection created by a call to
ConsCollection
key != NULL
Post-condition: returns an item identified by key if one
exists, otherwise returns NULL
*/
void InOrderTraversal(collection c, int(*private_print)(void *item))
//////////////////////////////////////////////////////////////
Here are the errors which I get on compiling.
ont> gcc -c main.c coll_at.c
main.c: In function `InOrderTraversal':
main.c:17: error: syntax error before '{' token
main.c:20: error: parameter `j' is initialized
main.c:22: error: syntax error before "f1"
coll_at.c: In function `InOrderTraversal':
coll_at.c:13: warning: structure defined inside parms
coll_at.c:20: warning: structure defined inside parms
coll_at.c:20: warning: empty declaration
coll_at.c:23: error: syntax error before ')' token
coll_at.c:92: error: syntax error before '*' token
Plz help because I need to submit this by tom. Thanks a lot
the file coll_at.c
//////////////////
coll_at.c
///////////////////////////////////////
#include <stdlib.h> /* calloc */
#include <stdio.h> /* NULL */
#include <assert.h> /* Needed for assertions */
#include "coll_a.h" /* import the specification */
struct t_node
{
void *item;
struct t_node *left;
struct t_node *right;
} node;
struct t_collection
{
/* Note that size is not needed any longer! */
int (*ItemCmp)( void *, void * );
struct t_node *node;
};
collection ConsCollection(int max_items,
int (*ItemCmp)(void *,void *))
/* Construct a data collection
Pre-condition: (max_items > 0) && (item_size > 0)
Post-condition: returns a pointer to an empty
collection
*/
{
collection c;
/* Although redundant, this assertion should be
retained as it tests compliance with the formal
specification */
assert( max_items > 0 );
c = (collection)calloc( 1, sizeof(struct t_collection) );
c->node = (struct t_node *)0;
c->ItemCmp = ItemCmp;
return c;
}
static void AddToTree( struct t_node **t, struct t_node *data,
int (*ItemCmp)(void *, void *) )
{
struct t_node *base;
base = *t;
/* If it's a null tree, just add it here */
if ( base == NULL )
{
*t = data;
return;
}
else
{
if ( ItemCmp( base->item, data ) < 0 )
{
AddToTree( &(base->left), data, ItemCmp );
}
else
AddToTree( &(base->right), data, ItemCmp );
}
}
void AddToCollection( collection c, void *item )
/* Add an item to a collection
Pre-condition: (c is a collection created by a call to
ConsCollection) &&
(existing item count < max_items) &&
(item != NULL)
Post-condition: item has been added to c
*/
{
struct t_node *data, *node_p;
assert( c != NULL );
assert( item != NULL );
/* Allocate space for a node for the data item */
data = (struct t_node *)malloc(sizeof(struct t_node));
/* Attach the item to the node */
data->item = item;
data->left = data->right = (struct t_node *)0;
node_p = c->node;
AddToTree( &node_p, data, c->ItemCmp );
c->node=node_p;
}
void DeleteFromTree( struct t_node **t, void *item )
{
struct t_node *temp, *prev, *succ;
prev=temp=*t;
void *val;
*val=(void*)item;
//val=item;
if(*t==NULL)
printf("NO NODES TO BE DELETED");
else
{
while(temp->item!=val && temp!=NULL) //define val
{
prev=temp;
if(temp->item >val)
temp=temp->left;
else
temp=temp->right;
}
if(temp==NULL)
{
printf("the value could not be found\n");
return;
}
if(temp->left!=NULL && temp->right!=NULL)
{
prev=temp;
succ=temp->right;
while(succ->left!=NULL)
{
prev=succ;
succ=succ->left;
}
temp->item=succ->item;
temp=succ;
}
if(temp->left!=NULL && temp->right==NULL)
{
if(prev->left==temp)
prev->left=temp->left;
else
prev->right=temp->left;
}
if(temp->left==NULL && temp->right!=NULL)
{
if(prev->left==temp)
prev->left=temp->right;
else
prev->right=temp->right;
}
if(temp->left==NULL && temp->right==NULL)
{
if(prev->left==temp)
prev->left=NULL;
else
prev->right=NULL;
}
/* if(temp==*root) // define root
{
if(temp->left!=NULL)
*root=temp->left;
else
*root=temp->right;
}*/
free(temp);
}
void DeleteFromCollection( collection c, void *item )
/* Delete an item from a collection
Pre-condition: (c is a collection created by a call to
ConsCollection) &&
(existing item count >= 1) &&
(item != NULL)
Post-condition: item has been deleted from c
*/
{
struct t_node *node;
assert( c != NULL );
/* The requirement that the collection has at least
one item is expressed a little differently */
assert( c->node != NULL );
assert( item != NULL);
/* Select node at head of list */
node = c->node;
DeleteFromTree( &node, item );
c->node=node;
}
void *FindInTree( struct t_node *t, void *key )
{
if(t==NULL)
return;
else if(t->item==key)
return t;
else if ( ItemCmp( t->item, key ) < 0 )
return FindInTree( (t->left), key );
else
return FindInTree( (t->right), key );
}
void *FindInCollection( collection c, void *key )
/* Find an item in a collection
Pre-condition: (c is a collection created by a call to
ConsCollection) &&
(key != NULL)
Post-condition: returns an item identified by key if
one exists, otherwise returns NULL
*/
{
struct t_node *node;
assert( c != NULL );
assert( key != NULL );
/* Select node at head of list */
node = c->node;
return FindInTree( node, key );
}
}
void InOrderTreeTraversal(struct t_node *t, int(*private_print)(void *item))
{
if(t != NULL)
{
InOrderTreeTraversal(t->left,private_print);
private_print(t->item);
InOrderTreeTraversal(t->right, private_print);
}
}
void InOrderTraversal(collection c, int(*private_print)(void *item))
{
assert(c != NULL);
InOrderTreeTraversal(c->node,private_print);
}
One of the problems is in :
void InOrderTraversal(collection c, int(*private_print)(void *item))
int(*private_print)(void *item)) is not right,
It is not entirely clear what the intentiobn is here, but I suspect it's one of 3 things:
there should proably be a comma bwetween items,
I don';t think you can play games with casting in a method declaration (and if you can the casting you are doing needs some rearranging of parenthesis)
If you are trying to pass a method in, try declaring a pointer to the method somewhere.....
Have a look here for more information
Here are some ideas on the other issues (which may niot fix the problem, but we'll see)
main.c:17: error: syntax error before '{' token
try declaring main to take arguments: -
int main (int args, char argv[]);
-
main.c:20: error: parameter `j' is initialized
try either declaring i and j on seperate lines, or declare both here but set j later -
int i;
-
int j = 100,000,000;
-
OR
-
int i, j;
-
-
/* code in between */
-
-
/* before for loop */
-
j = 100,000,000;
-
main.c:22: error: syntax error before "f1"
Fixing the other two might get rid of this alkso
Of course, you should declare j as
rather than 100,000,000 since the commas will confuse the computer.
well after making the changes for main and declaration of j, i got some new compile errors.
The first error says that main.c In function 'InOrderTraversal'. Now I really dont understand how this function came into main.
Also, I have been asked in the question to define the prototype function as
The prototype function is
void InOrderTraversal (collection c, int (*private_print)(void *item))
So, what is exactly wrong with this?
gcc -c main.c coll_at.c
main.c: In function `InOrderTraversal':
main.c:15: error: old-style parameter declarations in prototyped function definition
main.c:37: error: `ItemCmp' undeclared (first use in this function)
main.c:37: error: (Each undeclared identifier is reported only once
main.c:37: error: for each function it appears in.)
main.c: In function `ItemCmp':
main.c:78: warning: assignment makes integer from pointer without a cast
main.c:79: warning: assignment makes integer from pointer without a cast
main.c: In function `InOrderTraversal':
main.c:89: error: 'private_print' redeclared as different kind of symbol
coll_a.h:42: error: previous definition of 'private_print' was here
main.c: In function `private_print':
main.c:90: warning: assignment makes integer from pointer without a cast
main.c: In function `InOrderTraversal':
main.c:99: error: syntax error at end of input
coll_at.c: In function `InOrderTraversal':
coll_at.c:13: warning: structure defined inside parms
coll_at.c:20: warning: structure defined inside parms
coll_at.c:20: warning: empty declaration
coll_at.c:29: error: syntax error before '{' token
coll_at.c:92: error: syntax error before '*' token
I suspect you should read up on function pointers. Most of these problems seem to be caused by passing pointers to functions around the place. This is not something I have done terribly often, so it is difficult to pinpoint the exact cause of the problem....
The link in my earlier post has a detailed explanation of how to use function pointers.
Can any other expert please help me on this?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: sara |
last post by:
Hi
I'm pretty new to Access here (using Access 2000), and appreciate the
help and instruction.
I gave myself 2.5 hours to research online and help and try to get this
one, and I am not getting...
|
by: dbuchanan |
last post by:
Hello,
Here is the error message;
----------------------------
Exception Message:
ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child
key values (5) to exist in the...
|
by: Brian |
last post by:
So I have this:
double x = .25;
double y = .5;
double z = .25;
double probability;
int index;
while(index < 30)
{
|
by: coolindienc |
last post by:
Today I am going nuts. This is another one that is not working. It keeps giving me syntax error at highlighted line. No matter what I do I still have that error at the same line. It seems that my...
|
by: aalmakto |
last post by:
Hi all,
Whats wrong with this syntax?
DELETE from fighterSponsor fs, sponsors s, imageData id where fs.sponsorId=s.sponsorId AND id.imageId=s.imageId AND (s.imageId=81 OR s.imageId=82)
I...
|
by: illuzion |
last post by:
ok I keep getting this error:
Parse error: syntax error, unexpected T_VARIABLE in /home/illuzion/public_html/BAMF/contactus.php on line 38
and this error is possibly on other lines could...
|
by: kigoobe |
last post by:
Hi friends, I'm having three queries that works perfectly ...
SELECT ib.id as id, ib.titre as title, ib.date_expire as date_fin, ib.created_at as date_creation, eb.content as content, ''
FROM...
|
by: cwfontan |
last post by:
I am uploading a file renaming it with text from input here is the sql insert code and error im getting. If you need more of the code let me know..
This exact code is working when inserting into...
|
by: imaloner |
last post by:
I am posting two threads because I have two different problems, but both have the same background information.
Common Background Information:
I am trying to rebuild code for a working,...
|
by: Yesurbius |
last post by:
I am receiving the following error when attempting to run my query. In my mind - this error should not be happening - its a straight-forward query with a subquery. I am using Access 2003 with all...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |