11 Replies - 21890 Views - Last Post: 06 September 2011 - 11:24 AM Rate Topic: -----

#1 doug_byrne   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 15-August 11

error C2601: local function definitions are illegal

Posted 06 September 2011 - 01:20 AM

im getting the error, "error C2601: local function definitions are illegal", for both my functions and i cant figure out why. Any help would be much appreciated.


The code is supposed to approximate a root of a cubic function where coefficient 'a' is always 1 using newtons method of approximation after reading and storing the info from a .dat file.



// Assignment 1.cpp : Defines the entry point for the console application.
// Use Newton/Raphson's method to approximate the roots of a cubic function

#include "stdafx.h"
#define a 1        /*   coefficients                                               */

double b;          /*   coefficients                                               */
double c;          /*   coefficients                                             */
double d;          /*   coefficients                                               */
double x;          /*   intial guess                                             */
double root_x ;    /*   root of x                                                */
int count_rep; 

/* function prototype  */
double newtons_method(double, double, double, double);
double first_derivative(double, double, double);


int
main(void)
{
	/* the input file - alternate method  */
	
	printf("input data as follows; a, b, c, d, guess ->");
	scanf("%lf %lf %lf %lf", &b, &c, &d, &x);
	
	/* pointer to the file  */
	FILE *ptr;
	
	/* creating the file and pointing to it */
	ptr = fopen("input1.dat", "r");

	/* if theres an error in reading file or the files is empty */
	if( ptr == NULL)
		printf("File read error!");
	
	/* store the data on the file to memory  */
	fscanf(ptr, "%lf %lf %lf %lf", &b, &c, &d, &x); 


	/* See if the first derivative equals zero as this would give an error  */
	if ( first_derivative(b, c, x) == 0 ) {
		printf("Error, first derivative equals zero. Make a new initial guess");
        printf("Enter new inital guess>");
		scanf("lf", &x);
    
	/* checking the first derivative is far enough away from zero for a result */
	} else if ( first_derivative(b, c, x) <= 0.5 && first_derivative(b, c, x) >= -0.5 ) {
		printf("Error, first derivative is too close to zero to cause convergence. Make a new initial guess");
	    printf("Enter new inital guess>");
		scanf("lf", &x);
	
	/* if no error is found, execute function 'newtons_method' and loop until a result is found  */
	} else {
		count_rep = 0;
		while (count_rep <=20 && (root_x - x <= -0.000005 && root_x - x >= 0.000005)) {
			newtons_method(b, c, d, x);
			count_rep = count_rep + 1;
	}
	
		printf("The cubic polynomial; a equals %f, b equals%f, c equals%f, d equals%f\n, With an intital guess of%f.\n", a, b, c, d, x);
	    printf("Gives F(x)=0 at x=%f..... root found after %d iterations", count_rep);

	return 0;
}


double
newtons_method(double b, double c, double d, double x)
{
	root_x = (x - (( a * (x * x * x) + b * (x * x) + c * x + d ) / (3 * (x * x) + 2 * b * x + c)));

	return(root_x);
}

/* finds the first derivative of a function with an assumed 'a' coefficients of one
 * input - b and c coefficients and x value
 * outputs and replaces the x value with the first derivative of the input value
 */

double 
first_derivative(double b, double c, double x)
{
	return(3 * (x * x) + 2 * b * x + c);
}




Is This A Good Question/Topic? 0
  • +

Replies To: error C2601: local function definitions are illegal

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: error C2601: local function definitions are illegal

Posted 06 September 2011 - 01:31 AM

You are missing the closing brace to main().

This post has been edited by n8wxs: 06 September 2011 - 01:38 AM

Was This Post Helpful? 1
  • +
  • -

#3 aaa111   User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: error C2601: local function definitions are illegal

Posted 06 September 2011 - 01:34 AM

Your else is missing a closing brace '}'
 else { 
	        count_rep = 0;
		while (count_rep <=20 && (root_x - x <= -0.000005 && root_x - x >= 0.000005)) {
			newtons_method(b, c, d, x);
			count_rep = count_rep + 1;
	}
	<---  
	printf("The cubic polynomial; a equals %f, b equals%f, c equals%f, d equals%f\n, With an intital guess of%f.\n", a, b, c, d, x);
	printf("Gives F(x)=0 at x=%f..... root found after %d iterations", count_rep);

	return 0;
}



This post has been edited by aaa111: 06 September 2011 - 01:41 AM

Was This Post Helpful? 1
  • +
  • -

#4 doug_byrne   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 15-August 11

Re: error C2601: local function definitions are illegal

Posted 06 September 2011 - 01:50 AM

Thanks for finding that, I've got to get used to looking for little mistakes like that. Another question, im getting the wrong final results, can you see anything else that I've done wrong?
I'm guessing that its in;

	/* if no error is found, execute function 'newtons_method' and loop until a result is found  */
	} else {
		count_rep = 0;
		while (count_rep <=20 && (root_x - x <= -0.000005 && root_x - x >= 0.000005)) {
			newtons_method(b, c, d, x);
			count_rep = count_rep + 1;
	}

This post has been edited by doug_byrne: 06 September 2011 - 01:54 AM

Was This Post Helpful? 0
  • +
  • -

#5 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: error C2601: local function definitions are illegal

Posted 06 September 2011 - 01:52 AM

What is your output? What output are you expecting? Have you tried your debugger yet?
Was This Post Helpful? 0
  • +
  • -

#6 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: error C2601: local function definitions are illegal

Posted 06 September 2011 - 01:54 AM

What coefficients are you using?
Was This Post Helpful? 0
  • +
  • -

#7 doug_byrne   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 15-August 11

Re: error C2601: local function definitions are illegal

Posted 06 September 2011 - 02:01 AM

The output should read
The cubic polynomial;
'a' equals 1
'b' equals %f
'c' equals %f
'd' equals %f
With an intital guess of %f.
Has a root at x = %f..... Root found after %d iterations.

But im not getting anything as a root or number of iterations.

No coefficients in particular but id imagine that it would return a result, I was going to test it after I get a result.
Was This Post Helpful? 0
  • +
  • -

#8 aaa111   User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: error C2601: local function definitions are illegal

Posted 06 September 2011 - 02:52 AM

There are so many problem with your program.To start with here:
	printf("input data as follows; a, b, c, d, guess ->");
	scanf("%lf %lf %lf %lf", &b, &c, &d, &x);


Here you are taking input from console,then you are overwriting the contents of b,c,d,x with this:
fscanf(ptr, "%lf %lf %lf %lf", &b, &c, &d, &x); 

Which read the value from file.So your result will be based on the value inside of your file,not from the value you provide from console.
And look some of your scanf:
scanf("lf", &x);

What's missing here?

This post has been edited by aaa111: 06 September 2011 - 02:56 AM

Was This Post Helpful? 0
  • +
  • -

#9 doug_byrne   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 15-August 11

Re: error C2601: local function definitions are illegal

Posted 06 September 2011 - 03:22 AM

Yeah i know there is, i wont be including the printf/scanf in the final program, i was just using it as a means of testing the program without having a .dat file as they didn't give us a .dat to test it with. Probably shouldn't have added the code containing the file input bit onto the thread, sorry for the confusion.

The trouble I'm having at the moment is replacing the 'x' value with 'root_x' after its first loop so it simulates a 'guess and check' approach to finding the roots of the equation

	} else {

// This loop esimates the root of the function using newtons method of approximation 		
// inputs are; b, c, d, x
// outputs are; count_rep and the final value of x

                count_rep = 0;

		while (count_rep < 20  || ( -0.000005 <= diff_rootx_x && diff_rootx_x <= 0.000005)) {
			
			root_x = (x - ((x * x * x) + b * (x * x) + c * x + d ) / (3 * (x * x) + 2 * b * x + c));


			diff_rootx_x = root_x - x ;

			x = root_x;

			count_rep = count_rep + 1;
	}

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is online

  • member icon

Reputation: 5647
  • View blog
  • Posts: 17,348
  • Joined: 25-December 09

Re: error C2601: local function definitions are illegal

Posted 06 September 2011 - 06:38 AM

One of you big problems is being caused by your use of the global variables. You are passing these variables into your functions so move the variable definitions to inside main.

You need to make sure your compiler is generating warnings, and that the maximum number of warnings are displayed. These are some of the warnings I received when I compiled your code:

Quote

main.c||In function ‘main’:|
main.c|45|warning: comparing floating point with == or != is unsafe|
main.c|65|warning: format ‘%f’ expects type ‘double’, but argument 2 has type ‘int’|
main.c|66|warning: format ‘%f’ expects type ‘double’, but argument 2 has type ‘int’|
main.c|66|warning: too few arguments for format|
main.c||In function ‘newtons_method’:|
main.c|73|warning: declaration of ‘b’ shadows a global declaration|
main.c|10|warning: shadowed declaration is here|
main.c|73|warning: declaration of ‘c’ shadows a global declaration|
main.c|11|warning: shadowed declaration is here|
main.c|73|warning: declaration of ‘d’ shadows a global declaration|
main.c|12|warning: shadowed declaration is here|
main.c|73|warning: declaration of ‘x’ shadows a global declaration|
main.c|13|warning: shadowed declaration is here|
main.c||In function ‘first_derivative’:|
main.c|86|warning: declaration of ‘b’ shadows a global declaration|
main.c|10|warning: shadowed declaration is here|
main.c|86|warning: declaration of ‘c’ shadows a global declaration|
main.c|11|warning: shadowed declaration is here|
main.c|86|warning: declaration of ‘x’ shadows a global declaration|
main.c|13|warning: shadowed declaration is here|
||=== Build finished: 0 errors, 18 warnings ===|

Look closely at the second and third warnings, you have two integer variables a, and count_rep yet in your printf() you are trying to print them as doubles.

Also you should never compare a floating point number with the == or != comparison operators because of floating point inaccuracies.

You should also verify that your stdafx.h header file does actually include stdlib.h and stdio.h. Relying on a header to include standard C includes is a bad practice.

Jim
Was This Post Helpful? 1
  • +
  • -

#11 doug_byrne   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 15-August 11

Re: error C2601: local function definitions are illegal

Posted 06 September 2011 - 10:16 AM

Thanks heaps for your help :) I've changed my code to whats below but I wasn't sure of an alternative to

Quote

you should never compare a floating point number with the == or != comparison operators because of floating point inaccuracies.
Also just a question about having the variables outside of main, Ive noticed my text books do the same thing.

Also i think my loop is really poor, its not consistently stopping at 20 iterations like its supposed to and sometimes it runs continuously. Any suggestions?

// Use Newton/Raphson's method to approximate the roots of a cubic function

#include "stdafx.h"
#include "stdlib.h"
#include "targetver.h"


/* function prototype  */
double newtons_method(double, double, double, double);
double first_derivative(double, double, double);


int
main(void)
{
	double diff_rootx_x;
    double b;             /*   coefficients                                               */
    double c;             /*   coefficients                                               */
    double d;             /*   coefficients                                               */
    double x;             /*   initial guess                                               */
    double root_x ;       /*   root of x                                                  */
    int count_rep; 

	/* the input file - alternate method                                                  */
	
	printf("input data as follows; b, c, d, guess(x) ->");
	scanf("%lf %lf %lf %lf", &b, &c, &d, &x);
	
	/* pointer to the file                                                                */
	//FILE *ptr;
	
	/* creating the file and pointing to it                                               */
	//ptr = fopen("input1.dat", "r");

	/* if theres an error in reading file or the files is empty                           */
	//if( ptr == NULL)
	//	printf("File read error!");
	
	/* store the data on the file to memory                                               */
	//fscanf(ptr, "%lf %lf %lf %lf", &b, &c, &d, &x); 
	
    
	/* checking the first derivative is far enough away from zero for a result            */
	  if ( first_derivative(b, c, x) <= 0.5 && first_derivative(b, c, x) >= -0.5 ) {
		printf("Error, first derivative is too close to zero to cause convergence. Make a new initial guess");
	    printf("Enter new inital guess>");
		scanf("lf", &x);
	
	/* if no error is found, execute function 'newtons_method' and loop until a result is found  */
	} else {
		count_rep = 0;

		while (count_rep < 20  || !( -0.000005 <= diff_rootx_x && diff_rootx_x <= 0.000005)) {
			
			root_x = (x - ((x * x * x) + b * (x * x) + c * x + d ) / (3 * (x * x) + 2 * b * x + c));

			diff_rootx_x = root_x - x ;

			printf(" %f     %f      %d  \n\n", root_x, x, diff_rootx_x);
			x = root_x;

			printf("\n\n %f \n\n", x);

			count_rep = count_rep + 1;
	}
	
		printf("The cubic polynomial;\n 'a' equals 1.000000\n 'b' equals %f\n 'c' equals %f\n 'd' equals %f\n With an intital guess of %f.\n", b, c, d, x);
	    printf("Gives F(x)=0 at x=%f..... root found after %d iterations\n", root_x, count_rep);

	return 0;
}
}

/* finds the first derivative of a function with an assumed 'a' coefficients of one
 * input - b and c coefficients and x value
 * outputs and replaces the x value with the first derivative of the input value
 */

double 
first_derivative(double b, double c, double x)
{
	return(3 * (x * x) + 2 * b * x + c);
}



Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg   User is online

  • member icon

Reputation: 5647
  • View blog
  • Posts: 17,348
  • Joined: 25-December 09

Re: error C2601: local function definitions are illegal

Posted 06 September 2011 - 11:24 AM

Have you changed your compiler settings to make it emit warnings? You should be getting a couple of warnings with your code:

Quote

main.c||In function ‘main’:|
main.c|48|warning: too many arguments for format|
main.c|60|warning: format ‘%d’ expects type ‘int’, but argument 4 has type ‘double’|
||=== Build finished: 0 errors, 2 warnings ===|


Quote

Also i think my loop is really poor, its not consistently stopping at 20 iterations like its supposed to and sometimes it runs continuously. Any suggestions?


Run your program through your debugger, watch your variables as you single step through your loop. If your sentinel value is exceeded and the loop does not end you should be able to see the problem.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1