#include %26lt;iostream.h%26gt;
#include %26lt;fstream.h%26gt;
#include %26lt;stdlib.h%26gt;
int main()
{
double npts;
int order( int x[], int npts )
{
cin %26gt;%26gt;npts;
int duck = 0;
//check for ascending first
for( duck = 0; duck %26lt; npts - 1; duck++ )
{
if( x[ duck + 1 ] %26lt; x[ duck ] ) break;
}
if( duck == npts - 1 )//i.e., if the for loop ran till the end
return 1; //yes, it is in ascending order
//check for descending next
duck = 0;
for( duck = 0; duck %26lt; npts - 1; duck++ )
{
if( x[ duck + 1 ] %26gt; x[ duck ] ) break;
}
if( duck == npts - 1 )//i.e., if the for loop ran till the end
return -1; //yes, it is in descending order
return 0;//neither
}
getting :
error C2601: 'order' : local function definitions are illegal
I'm trying to write a program that checks wheter the values in an array are in ascending order=1, descending=-1, or neither=0. assuming the prototype;
int order(int x[], int npts);
please help me fix.
C++ error in writing a function?
try to define you fucntion int order(int[], int npts) outside main() and call this function in main() by supplying values(parameters).
for more information don't foget to visit my blog
http://codesbyshariq.blogspot.com.
and also STOP DRINKING BEER as this will paralyse your mind
Reply:The problem is that you're defining the function inside main.
Define it outside.
Try changing your code this way
#include %26lt;iostream.h%26gt;
#include %26lt;fstream.h%26gt;
#include %26lt;stdlib.h%26gt;
int order( int x[], int npts )
{
cin %26gt;%26gt;npts;
int duck = 0;
//check for ascending first
for( duck = 0; duck %26lt; npts - 1; duck++ )
{
if( x[ duck + 1 ] %26lt; x[ duck ] ) break;
}
if( duck == npts - 1 )//i.e., if the for loop ran till the end
return 1; //yes, it is in ascending order
//check for descending next
duck = 0;
for( duck = 0; duck %26lt; npts - 1; duck++ )
{
if( x[ duck + 1 ] %26gt; x[ duck ] ) break;
}
if( duck == npts - 1 )//i.e., if the for loop ran till the end
return -1; //yes, it is in descending order
return 0;//neither
}
int main()
{
//other code here
}
Reply:for the most part he people above me are correct. Your prototype : int order( int x[], int npts ) needs to be before main. The other part is the definition needs to be after main or else it won't execute right. In order to get the function to work right rename it int order( int x, int y).
Reply:your prototype is in the main function; It should be outside the main.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment