Solved

Declaration terminated incorrectly error during compile and also check the coding

Posted on 2003-10-28
10
3,736 Views
Last Modified: 2007-12-19
hy experts,
I have solved the following question in 'C' language but its show the error which is "declaration terminated incorrectly" during compile. I am not able to solve this error.Please help me and also check the coding of the question.The question is follows:-

Write a program in 'C' programming language to list the nodes of a binary tree in the following way:
List the root,then nodes at depth 1,followed by nodes at depth 2,and so  on.
Your program should have linear time complexity

And the coding which is written by me, please check the coding:--

#include<stdio.h>
struct NODE
      {
            char info;
            struct NODE *left_child;
            struct Node *right_child;
      };
struct NODE binary_tree(char*,int,int);
void output (struct NODE*,int);
void pre_order(struct NODE*);
/*function to create on binary tree*/
struct NODE binary_tree(char *list,int lower,int upper);
{
      struct NODE *node;
      int mid=(lower+upper)/2;
      node=(struct NODE*) malloc (sizeof(struct NODE));
      node->info=(list[**]);
      if(lower>=upper)
      {
            node->left_child=NULL;
            node->right_child=NULL;
            return(node);
      }
      if(lower<=child-1)
            node->left_child=binary_tree(list,lower,mid);
       else
            node->left_child=NULL;
            if(mid+1 <=upper)
                  node->right_child=binary_tree(list,mid+1,upper);
       else
       node->right_child=NULL;
      return(node);
      }
      /*output function*/
      void output(struct NODE *t,int level)
      {
      int i;
      if(t)
      {
            output(t->right_child,level+1);
            printf("In");
            for(i=0;i<level;i++)
                  printf(" ");
                  printf("%c",t->info);
                  output(t->left_child,level+1);
      }
      /*preorder traversal*/
      void preorder(struct NODE *NODE)
      {
            if(NODE)
            {
                  printf("%c",node->info);
                  preorder(node->left_child);
                  preorder(node->left_child);
            }
      }
      /*function main*/
      {
      char list[100];
      int number=0;
      char info;
      char choice;
      struef NODE *t=(struef node*)malloc(size of (struef NODE));
      t=NULL;
      printf("In Input choice 'b' to break:");
      choice=get char();
      ffclush (stdin);
      while(choice!='b')
      printf("In Input information of the node:");
      scanf("%c"<&info);
      list[number++]=info;
      ffclush(stdin);
      printf("In Input choice 'b' to break:");
      choice=getchar();
      ffclush(stdin);
      }
      number;
      printf("In number of elements in the list is %d",number);
      t=binary_tree(list,0,number);
      output(t,1);
      printf("in preorder travrsal in ");
      preorder(t,1);
      }

0
Comment
Question by:narku_narku
  • 4
  • 2
10 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9640015


Your syntax errors============

#include<stdio.h>
struct NODE
     {
          char info;
          struct NODE *left_child;
          struct Node *right_child;================== Node should be NODE
     };
struct NODE binary_tree(char*,int,int);
void output (struct NODE*,int);
void pre_order(struct NODE*);
/*function to create on binary tree*/
struct NODE binary_tree(char *list,int lower,int upper);============== semicolon not needed
{
     struct NODE *node;
     int mid=(lower+upper)/2;
     node=(struct NODE*) malloc (sizeof(struct NODE));
     node->info=(list[**]);==============================what is [**]....??
     if(lower>=upper)
     {
          node->left_child=NULL;
          node->right_child=NULL;
          return(node);============================= Your return type is a Node type but u are retuning a pointer to Node=================
     }
     if(lower<=child-1)============================child node not declared
          node->left_child=binary_tree(list,lower,mid);============= Again return type of this function should be Pointer to Node
      else
          node->left_child=NULL;
          if(mid+1 <=upper)
               node->right_child=binary_tree(list,mid+1,upper);============= Again return type of this function should be Pointer to Node
      else
      node->right_child=NULL;
     return(node);============================= Your return type is a Node type but u are retuning a pointer to Node=================

     }
     /*output function*/
     void output(struct NODE *t,int level)
     {
     int i;
     if(t)
     {
          output(t->right_child,level+1);
          printf("In");
          for(i=0;i<level;i++)
               printf(" ");
               printf("%c",t->info);
               output(t->left_child,level+1);
     }
     /*preorder traversal*/
     void preorder(struct NODE *NODE) =========== variable NODE shoudl be node
thus declaration shoudl be      void preorder(struct NODE *node)
     {
          if(NODE)=================== NODE should be node
          {
               printf("%c",node->info);
               preorder(node->left_child);
               preorder(node->left_child);
          }
     }
     /*function main*/
     {
     char list[100];
     int number=0;
     char info;
     char choice;
     struef NODE *t=(struef node*)malloc(size of (struef NODE));========== struct mispelled, sizeof shoudl be one word=================
     t=NULL;
     printf("In Input choice 'b' to break:");
     choice=get char();=============use getche() OR getch() instead
     ffclush (stdin);
     while(choice!='b')
     printf("In Input information of the node:");
     scanf("%c"<&info);==============< should be ,
     list[number++]=info;
     ffclush(stdin);
     printf("In Input choice 'b' to break:");
     choice=getchar();
     ffclush(stdin);
     }=========================This should be at the end your main gets closed here
     number;==============what is this=========
     printf("In number of elements in the list is %d",number);
     t=binary_tree(list,0,number);
     output(t,1);
     printf("in preorder travrsal in ");
     preorder(t,1);=================parameters incorrect
     }
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9640028
REctify the above errors and have a look at your logic, if not getting, let us know

Also As per your requirement, a preorder traversal won;t do, because u have to print the nodes at one level before going to the next level.

But preorder won;'t do this
0
 

Author Comment

by:narku_narku
ID: 9640089
hello sys_prog
thanks for help please give me right coding for this question in the complete manner. i am not able to understand the error that u have detect . futher thanks for help
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 10

Accepted Solution

by:
Sys_Prog earned 250 total points
ID: 9640122
HEy narku_narku

HEre's your snytax error free code

I have not seen at the logic, I think U should have a look at it and then if u face any problem we can help u

U should first Try it yourself


#include<stdio.h>
struct NODE
     {
          char info;
          struct NODE *left_child;
          struct NODE *right_child;
     };
struct NODE * binary_tree(char*,int,int);
void output (struct NODE*,int);
void pre_order(struct NODE*);
/*function to create on binary tree*/
struct NODE * binary_tree(char *list,int lower,int upper)
{
     struct NODE *node ;
     int child ;
     int mid=(lower+upper)/2;
     node=(struct NODE*) malloc (sizeof(struct NODE));
     node->info=(list[0]);
     if(lower>=upper)
     {
          node->left_child=NULL;
          node->right_child=NULL;
          //return(node);
     }
     if(lower<=child-1)
          node->left_child=binary_tree(list,lower,mid);
      else
          node->left_child=NULL;
          if(mid+1 <=upper)
              node->right_child = binary_tree(list,mid+1,upper);
      else
      node->right_child=NULL;
     return(node);
}

/*output function*/
void output(struct NODE *t,int level) {
 
     int i;
     if(t)
       {
          output(t->right_child,level+1);
          printf("In");
          for(i=0;i<level;i++)
               printf(" ");
               printf("%c",t->info);
               output(t->left_child,level+1);
       }
}      

     /*preorder traversal*/
     void preorder(struct NODE *node)
     {
          if(node)
          {
               printf("%c",node->info);
               preorder(node->left_child);
               preorder(node->left_child);
          }
     }


     /*function main*/
     int main ( void )
     {
     char list[100];
     int number=0;
     char info;
     char choice;
     struct NODE *t=(struct NODE*)malloc(sizeof (struct NODE));
     t=NULL;
     printf("In Input choice 'b' to break:");
     choice=getch();
     fflush (stdin);
     while(choice!='b')
     printf("In Input information of the node:");
     scanf("%c",&info);
     list[number++]=info;
     fflush(stdin);
     printf("In Input choice 'b' to break:");
     choice=getchar();
     fflush(stdin);
     
     
     printf("In number of elements in the list is %d",number);
     t=binary_tree(list,0,number);
     output(t,1);
     printf("in preorder travrsal in ");
     //preorder(t,1);
     }
0
 

Expert Comment

by:AllRounder
ID: 9640661
Hi narku_narku and Sys_Prog

I just came across this code...
I could not get the Significance  of the integer variable 'child'.
If  narku_narku or Sys_Prog can throw some light, it would be helpful.
If narku_narku can explain what he wants as output, may be we can rewrite the code in a simpler way.

Regards,
AR
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9640732
Hi AllRounder,

Actually I do not want to give the complete code. I think the questioner should build his logic of doing it and we can correct him if he is gone wrong somewhere

Actually his code does contain a lot of pitfalls which he need to discover

0
 

Author Comment

by:narku_narku
ID: 9662202
hy allrounder,
as u say what i want the output. the question is follows as :

Write a program in 'C' programming language to list the nodes of a binary tree in the following way:
List the root,then nodes at depth 1,followed by nodes at depth 2,and so  on.
Your program should have linear time complexity.

as u say me u give me the complete code. please see the question and give me complete code in a simple way. futher thanks for help.


0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now