Sunday, August 2, 2009

What is the reason for declaration syntax error or declaration terminated incorrectly in c++ in this code?

#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;process.h%26gt;


int findpos(int[] ,int,int);


main()


{


int a[50] ,item,n,index;


clrscr();


cout%26lt;%26lt;"How many elements do u want ot insertmax 50 ";


cin%26gt;%26gt;n;


cout%26lt;%26lt;"Enter the elements";


for( int i=0;i%26lt;n;i++)


{


cin%26gt;%26gt;a[i];


}


char ch = 'y';


while(ch=='y'||ch=='Y')


{


cout%26lt;%26lt;"Enter element to be inserted";


cin%26gt;%26gt;item;


if(n==50)


{


cout%26lt;%26lt;"Overflow";


exit(1);


}


index=findpos(a,n,item);


for(i=n;i%26gt;index;i--)


{


a[i]=a[i-1];


a[index]=item;


n+=1;


cout%26lt;%26lt;"Want to insert more element ";


cin%26gt;%26gt;ch;


}


cout%26lt;%26lt;"The array now is as shown below \n";


for(i=0;i%26lt;n;i++)


cout%26lt;%26lt;a[i]%26lt;%26lt;" ";


cout%26lt;%26lt;endl;


getch();


}





int findpos(int a[],int size,int item)


{


int pos;


if(item%26lt;a[0])


pos=0;


else


{


for(int i=0;i%26lt;size;i++)


{


if(a[i]%26lt;=item%26amp;%26amp;item%26lt;a[i+1])


pos=i+1;


break;


}


}


if(i==size-1)


pos=size;


}


return pos;


}

What is the reason for declaration syntax error or declaration terminated incorrectly in c++ in this code?
#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;process.h%26gt;


int findpos(int[] ,int,int);


main()


{


int a[50] ,item,n,index;


clrscr();


cout%26lt;%26lt;"How many elements do u want ot insertmax 50 ";


cin%26gt;%26gt;n;


cout%26lt;%26lt;"Enter the elements";


for( int i=0;i%26lt;n;i++)


{


cin%26gt;%26gt;a[i];


}


char ch = 'y';


while(ch=='y'||ch=='Y')


{


cout%26lt;%26lt;"Enter element to be inserted";


cin%26gt;%26gt;item;


if(n==50)


{


cout%26lt;%26lt;"Overflow";


exit(1);


}


index=findpos(a,n,item);


for(i=n;i%26gt;index;i--)


{


a[i]=a[i-1];


}


a[index]=item;





n+=1;


cout%26lt;%26lt;"Want to insert more element ";


cin%26gt;%26gt;ch;


}


cout%26lt;%26lt;"The array now is as shown below \n";


for(i=0;i%26lt;n;i++)


cout%26lt;%26lt;a[i]%26lt;%26lt;" ";


cout%26lt;%26lt;endl;


getch();


}





int findpos(int a[],int size,int item)


{


int pos;


int i;


for(i=0;i%26lt;size;i++)


{


if(a[i]%26gt;item)


{


pos=i;


break;


}


}





if(i==size)


pos=size;





return pos;


}








Now I have corrected all the problems. Also u r function logic was incorrect. It was not working for if number was to be inserted in last position.


if(a[i]%26lt;=item%26amp;%26amp;item%26lt;a[i+1]) // if i==n-1 how to get a[i+1]


pos=i+1;





Rest it was fine , there were yping mistakes regarding the braces.





OM NAMAH SHIVAY


No comments:

Post a Comment