| | |
C++ complete binary tree using an array. Unexpected end file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 2
Reputation:
Solved Threads: 0
I am getting errors when trying to compile my program, can anyone help?
Errors are
error C2143: syntax error : missing ')' before 'constant'
error C2143: syntax error : missing ';' before 'constant'
fatal error C1004: unexpected end of file found
Here is my code:
Errors are
error C2143: syntax error : missing ')' before 'constant'
error C2143: syntax error : missing ';' before 'constant'
fatal error C1004: unexpected end of file found
Here is my code:
C++ Syntax (Toggle Plain Text)
// declaration of a binary tree with pointers to left and right children// #include <assert.h> #include <stdio.h> #include <stdlib.h> #define SIZE 9 typedef int DATA; struct node { DATA dat; struct node *left; struct node *right; }; typedef struct node bNODE; typedef node *BTREE; void inorder(const BTREE root); void preorder(const BTREE root); void postorder(const BTREE root); BTREE makeTree(int a[], int i, int SIZE); BTREE new_node(); BTREE init_node(DATA dat1, BTREE par1, BTREE par2); BTREE insert(BTREE parent, BTREE root, int item); int count_nodes(BTREE root, int cnt); int count_Lnodes(BTREE root, int cnt); int *makeArray(BTREE root, int val[]); int *inorder_traversal(int i, BTREE b_tree, int *valPtr); int *order_array(int a[]); void divide_array(int a[], int b[], int c[], int SIZE); int find_center(int a[], int SIZE); int main() { // int command; BTREE b_tree; int array_of_ints[] = {5, 10, 2, 5, 7, 12, 3, 9, 8}; int val[9] = {0}; int *valPtr = 0; b_tree = makeTree)array_of_ints, 0, SIZE); printf("Tree printed 'in-order'\n"); preorder (b_tree); putchar('\n'); printf("Number of nodes = %d\n", count_nodes(b_tree, 0)); printf("Number of Leaf nodes = %d\n", count_Lnodes(b_tree, 0)); printf("Here is the array made from the tree:\n"); valPtr = makeArray(b_tree, val); return 0; } // put an array in alphabetical order int *order_array(int a[]) { int j, i, temp; int *b = a; for (i = 0; i < SIZE-1; ++i) for (j = i+1; j < SIZE; ++j) if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } return b; } // put an array in alphabetical order int *order_array(int a[]) { int j, i, temp; int *b = a; for (i = 0; i < SIZE-1; ++i) for (j = i+1; j < SIZE; ++j) if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } return b; } // send function a, the array to be divided, and b and c, the 2 array pointers void divide_array(int a[], int b[], int c[], int SIZE) { int i, j; for (i = 0; i < SIZE/2; ++i) b[i] = a[i]; i++; // removing middle item, the one being added for (j = 0; i < SIZE; ++i, ++j) c[j] = a[i]; } // find center of an array int find_center(int a[], int SIZE) { int i = 0, center; while (i < SIZE) ++i; center = i / 2; printf("%d.. %d .. %d\n", i, center, a[center]); return a[center]; } // create ordered binary tree using recursion, divide array, then insert center int BTREE makeTree(int a[], int i, int SIZE) { int b[SIZE] = {0}, c[SIZE] = {0}; if(SIZE == 0) return NULL; if(SIZE == 1) init_node(a[0], NULL, NULL); if(i <= SIZE) { divide_array(a, b, c, SIZE); return init_node(find_center(a, SIZE), makeTree(b, 2 * i + 1, SIZE / 2), makeTree(c, 2 * i + 2, SIZE - SIZE / 2 - 1)); } } // create array using inorder binary tree traversal and print int *makeArray(BTREE root, int val[]) { int i = 0, *valPtr; valPtr = val; valPtr = inorder_traversal(i, root, val); putchar('\n'); return valPtr; } // create array from binary tree int *inorder_traversal(int i, BTREE parent, int val[]) { int *valPtr; valPtr = val; if (parent != NULL) { inorder_traversal(i, parent->left, val); val[i] = parent->dat; printf("%d ", val[i]); ++i; inorder_traversal(i, parent->right, val); } return valPtr; } // inorder binary tree traversal and print void inorder(const BTREE parent) { if (parent != NULL) { inorder(parent->left); printf("%d ", parent->dat); inorder(parent->right); } } // preorder binary tree traversal and print void preorder(const BTREE root) { if (root != NULL) { printf("%d ", root->dat); preorder(root->left); preorder(root->right); } } // creating binary trees BTREE new_node() { return (malloc(sizeof(bNODE))); } BTREE init_node(dat1, BTREE par1, BTREE par2) { BTREE t; t = new_node(); t->dat = dat1; t->left = par1; t->right = par2; return t; } // count total number of nodes int count_nodes(BTREE root, int cnt) { if (root != NULL) { cnt = count_nodes(root->left, cnt); /* recur left */ cnt = count_nodes(root->right, ++cnt); /* recur right */ } return cnt; } // count number of leaf nodes int count_Lnodes(BTREE root, int cnt) { if (root != NULL) { cnt = count_Lnodes(root->left, cnt); cnt = count_Lnodes(root->right, cnt); if (root->left == NULL && root->right == NULL) ++cnt; } return cnt; }
Last edited by alc6379; Feb 16th, 2005 at 12:37 am.
•
•
Join Date: Feb 2005
Posts: 2
Reputation:
Solved Threads: 0
The place where I am getting the errors is in bold.
•
•
•
•
Originally Posted by spitfire
I am getting errors when trying to compile my program, can anyone help?
Errors are
error C2143: syntax error : missing ')' before 'constant'
error C2143: syntax error : missing ';' before 'constant'
fatal error C1004: unexpected end of file found
Here is my code:
// declaration of a binary tree with pointers to left and right children//
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define SIZE 9
typedef int DATA;
struct node
{
DATA dat;
struct node *left;
struct node *right;
};
typedef struct node bNODE;
typedef node *BTREE;
void inorder(const BTREE root);
void preorder(const BTREE root);
void postorder(const BTREE root);
BTREE makeTree(int a[], int i, int SIZE);
BTREE new_node();
BTREE init_node(DATA dat1, BTREE par1, BTREE par2);
BTREE insert(BTREE parent, BTREE root, int item);
int count_nodes(BTREE root, int cnt);
int count_Lnodes(BTREE root, int cnt);
int *makeArray(BTREE root, int val[]);
int *inorder_traversal(int i, BTREE b_tree, int *valPtr);
int *order_array(int a[]);
void divide_array(int a[], int b[], int c[], int SIZE);
int find_center(int a[], int SIZE);
int main()
{
// int command;
BTREE b_tree;
int array_of_ints[] = {5, 10, 2, 5, 7, 12, 3, 9, 8};
int val[9] = {0};
int *valPtr = 0;
b_tree = makeTree)array_of_ints, 0, SIZE);
printf("Tree printed 'in-order'\n");
preorder (b_tree);
putchar('\n');
printf("Number of nodes = %d\n", count_nodes(b_tree, 0));
printf("Number of Leaf nodes = %d\n", count_Lnodes(b_tree, 0));
printf("Here is the array made from the tree:\n");
valPtr = makeArray(b_tree, val);
return 0;
}
// put an array in alphabetical order
int *order_array(int a[])
{
int j, i, temp;
int *b = a;
for (i = 0; i < SIZE-1; ++i)
for (j = i+1; j < SIZE; ++j)
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
return b;
}
// put an array in alphabetical order
int *order_array(int a[])
{
int j, i, temp;
int *b = a;
for (i = 0; i < SIZE-1; ++i)
for (j = i+1; j < SIZE; ++j)
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
return b;
}
// send function a, the array to be divided, and b and c, the 2 array pointers
void divide_array(int a[], int b[], int c[], int SIZE)
{
int i, j;
for (i = 0; i < SIZE/2; ++i) b[i] = a[i];
i++; // removing middle item, the one being added
for (j = 0; i < SIZE; ++i, ++j) c[j] = a[i];
}
// find center of an array
int find_center(int a[], int SIZE)
{
int i = 0, center;
while (i < SIZE) ++i;
center = i / 2;
printf("%d.. %d .. %d\n", i, center, a[center]);
return a[center];
}
// create ordered binary tree using recursion, divide array, then insert center int
BTREE makeTree(int a[], int i, int SIZE)
{
int b[SIZE] = {0}, c[SIZE] = {0};
if(SIZE == 0)
return NULL;
if(SIZE == 1)
init_node(a[0], NULL, NULL);
if(i <= SIZE)
{
divide_array(a, b, c, SIZE);
return init_node(find_center(a, SIZE), makeTree(b, 2 * i + 1, SIZE / 2),
makeTree(c, 2 * i + 2, SIZE - SIZE / 2 - 1));
}
}
// create array using inorder binary tree traversal and print
int *makeArray(BTREE root, int val[])
{
int i = 0, *valPtr;
valPtr = val;
valPtr = inorder_traversal(i, root, val);
putchar('\n');
return valPtr;
}
// create array from binary tree
int *inorder_traversal(int i, BTREE parent, int val[])
{
int *valPtr;
valPtr = val;
if (parent != NULL)
{
inorder_traversal(i, parent->left, val);
val[i] = parent->dat;
printf("%d ", val[i]);
++i;
inorder_traversal(i, parent->right, val);
}
return valPtr;
}
// inorder binary tree traversal and print
void inorder(const BTREE parent)
{
if (parent != NULL)
{
inorder(parent->left);
printf("%d ", parent->dat);
inorder(parent->right);
}
}
// preorder binary tree traversal and print
void preorder(const BTREE root)
{
if (root != NULL)
{
printf("%d ", root->dat);
preorder(root->left);
preorder(root->right);
}
}
// creating binary trees
BTREE new_node()
{
return (malloc(sizeof(bNODE)));
}
BTREE init_node(dat1, BTREE par1, BTREE par2)
{
BTREE t;
t = new_node();
t->dat = dat1;
t->left = par1;
t->right = par2;
return t;
}
// count total number of nodes
int count_nodes(BTREE root, int cnt)
{
if (root != NULL)
{
cnt = count_nodes(root->left, cnt); /* recur left */
cnt = count_nodes(root->right, ++cnt); /* recur right */
}
return cnt;
}
// count number of leaf nodes
int count_Lnodes(BTREE root, int cnt)
{
if (root != NULL)
{
cnt = count_Lnodes(root->left, cnt);
cnt = count_Lnodes(root->right, cnt);
if (root->left == NULL && root->right == NULL) ++cnt;
}
return cnt;
}
Don't use preprocessor macros unless you know how the preprocessor works. It's all to easy to forget that it's textual replacement before anything else. So this:
Becomes this:
Which is a syntax error. Therefore, you can't use SIZE anywhere except where you want it replaced with 9.
C++ Syntax (Toggle Plain Text)
#define SIZE 9 BTREE makeTree(int a[], int i, int SIZE);
C++ Syntax (Toggle Plain Text)
#define SIZE 9 BTREE makeTree(int a[], int i, int 9);
I'm here to prove you wrong.
![]() |
Similar Threads
- [TurboPascal, Dev-Pas, Free-Pascal] problem with longint and unexpected end of file (Pascal and Delphi)
- Binary Tree help! (C++)
- ERROR--Unexpected end to file (C++)
- Coding a Complete Binary Tree (C)
- complete binary tree using an array help (C++)
- Question about binary tree & heaps (Computer Science)
- coding a complete binary tree with Dev-C++ (C++)
Other Threads in the C++ Forum
- Previous Thread: Simple C++ program terminate prematurely
- Next Thread: classes in C++
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






