0
/* < newton.c > */
#include "smp.h"

int main()
{
	void USERF(double *,double *,double);
	void S_NEWT(int *n,double *x0,double e,int NMAX,void (*FUNC)(double *,double *,double));
	double 	x0,e;
	int		n,NMAX;

/*** ステップ1 データの入力 ***/
	printf("\n初期値 x0 = ");
	scanf("%1f",&x0);
	printf("\n収束判別定数 p = ");
	scanf("%1f",&e);
	printf("\n最大反復回数 NMAX = ");
	scanf("%d",&NMAX);

/*** ステップ2 ニュートン・ラプソン法の計算 ***/
	S_NEWT(&n,&x0,e,NMAX,&USERF);
	return 1;

/*** ステップ3 計算結果の出力 ***/
	printf("\n反復回数:N=%d 解:X(N)=%10.71e",n,x0);
}

/* < ニュートン・ラプソン法の関数 > */
void S_NEWT(int *n,double *x0,double e,int NMAX,void (*FUNC)(double *,double *,double))
/*	n		: スカラー	反復回数		(Out)
	x0		: スカラー	初期値(解)		(In/Out)
	e		: スカラー	収束判別定数	(In)
	NMAX	: スカラー	最大反復回数	(In)
	FUNC	:			ユーザ定義関数	(In)	*/
{
     double fun,dfun,xn,ans;
     for(*n=0,xn=*x0;*n<NMAX;){
		(*FUNC)(&fun,&dfun,xn);
		*x0=xn-fun/dfun;
		if(fabs(xn-*x0)>e)
	    	xn=*x0;
	else
	    break;
      }
}

/* <ユーザ定義の関数とその微分の関数 > */
void USERF(double *fn,double *dfn,double x)
/*	fn	: スカラー	方程式			(Out)
	dfn	: スカラー	方程式の微分	(Out)
	x	: スカラー	入力値			(In)	*/
{
double x2;
x2=x*x;
*fn=x2*x-1.0;
*dfn=3.0*x2;
return;
}

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

0
int main()
{
void USERF(double *,double *,double);
void S_NEWT(int *n,double *x0,double e,int NMAX,void (*FUNC)(double *,double *,double));

I give up, it can go for hours with me trying to show you the way, all in vain. I thought you yourself would "get" the solution. Here goes:
remove these declarations:
void USERF(double *,double *,double);
void S_NEWT(int *n,double *x0,double e,int NMAX,void (*FUNC)(double *,double

from main() to global scope.

0

Still the same error, even after removing those declarations to above int main()?

Edited by nbaztec: n/a

1

Out of curiosity I compiled your code & this worked all fine. Don't know what you are doing wrong.

#include <stdio.h>
#include <math.h>
void USERF(double *,double *,double);
void S_NEWT(int *n,double *x0,double e,int NMAX,void (*FUNC)(double *,double *,double));

int main()
{
	double 	x0,e;
	int		n,NMAX;

	printf("\n??? x0 = ");
	scanf("%1f",&x0);
	printf("\n?????? p = ");
	scanf("%1f",&e);
	printf("\n?????? NMAX = ");
	scanf("%d",&NMAX);

	S_NEWT(&n,&x0,e,NMAX,&USERF);


	printf("\n????:N=%d ?:X(N)=%10.71e",n,x0);
   return 1;
}

void S_NEWT(int *n,double *x0,double e,int NMAX,void (*FUNC)(double *,double *,double))

{
     double fun,dfun,xn,ans;
     for(*n=0,xn=*x0;*n<NMAX;)
     {
		(*FUNC)(&fun,&dfun,xn);
		*x0=xn-fun/dfun;
		if(fabs(xn-*x0)>e)
	    	xn=*x0;
		else
	    break;
      }
}


void USERF(double *fn,double *dfn,double x)
{
double x2;
x2=x*x;
*fn=x2*x-1.0;
*dfn=3.0*x2;
return;
}
Votes + Comments
Rep for helping in this monster thread
1
main()
{
	void USERF(double *,double *,double);
.
.
}

You can't declare functions-in-functions in C, perhaps in JavaScript or Pascal but not in C/C++. Take that declaration outside main().

Actually you are wrong, you can declare functions inside functions like that. What you can't do is define functions inside other functions like this

int main()
{
	void USERF(double *,double *,double)
	{
	}
.
.
   return 0;
}
Votes + Comments
Really! I didn't know that. Thanks for the info.
0

Out of curiosity I compiled your code & this worked all fine. Don't know what you are doing wrong.

#include <stdio.h>
#include <math.h>
void USERF(double *,double *,double);
void S_NEWT(int *n,double *x0,double e,int NMAX,void (*FUNC)(double *,double *,double));

int main()
{
	double 	x0,e;
	int		n,NMAX;

	printf("\n??? x0 = ");
	scanf("%1f",&x0);
	printf("\n?????? p = ");
	scanf("%1f",&e);
	printf("\n?????? NMAX = ");
	scanf("%d",&NMAX);

	S_NEWT(&n,&x0,e,NMAX,&USERF);


	printf("\n????:N=%d ?:X(N)=%10.71e",n,x0);
   return 1;
}

void S_NEWT(int *n,double *x0,double e,int NMAX,void (*FUNC)(double *,double *,double))

{
     double fun,dfun,xn,ans;
     for(*n=0,xn=*x0;*n<NMAX;)
     {
		(*FUNC)(&fun,&dfun,xn);
		*x0=xn-fun/dfun;
		if(fabs(xn-*x0)>e)
	    	xn=*x0;
		else
	    break;
      }
}


void USERF(double *fn,double *dfn,double x)
{
double x2;
x2=x*x;
*fn=x2*x-1.0;
*dfn=3.0*x2;
return;
}

yes, it works fine but not working when #include smp.h ?

Edited by whattever: n/a

0

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>

#define pi 2.0 * acos(0) /* π */
#define e_eps 1.0e-15 /*ピボットの許容最小値、零判定値*/

void S_ADAM(double *,double ,double ,int ,int ,double ,double (*)());
void S_FDBS(double *,double *,int *,double *,int ,int );
void S_HAUS(double *,int ,double *,double *,double *);
void S_INMG(double *,int ,int ,char *);
void S_INMS(double *,int ,int ,char *);
void S_EULR(double *,double ,double ,int ,double (*)());
void S_NEWT(int *,double *,double ,int ,double (*)());
void S_OUTM(double *,int ,int ,char *);
void S_QR(double *,int ,double ,double *,double *);
void S_RNGK(double *,double ,double ,int ,double (*)());
void S_SPLN(double *,double *,double *,int ,double *,
double *,double *,double *);
int S_CHOL(double *,double *,double *,int );
int S_DOT(double *, int , int *);
int S_EIGN(double *,double *,int ,double ,double *,double *,
double *,double *,int *);
int S_GAUS(double *,double *,double *,int );
int S_INVM(double *,double *,int ,int *);
int S_INVG(double *,double *,int ,int ,int *,double *);
int S_JACB(double *,double *,int ,int ,double );
int S_LSM(double *,double *,double *,int ,int ,double *,double *);
int S_PIVO(double *, int *, int , int );
int S_RUNG(double *,int ,double ,double ,int ,int ,
void (*)(),double *,double *,double *);
double S_DAIK(double ,double ,int ,double (*)());
double S_LAGR(int ,double *,double *,double );
double S_ROMB(double ,double ,double ,int ,double (*)(),double *);
double S_SPFN(double *,double *,double *,int ,
double *,double *,double );

this is the smp.h codes

1

Honestly i think with your attitude not much people will try to help. You are not stupid, but you are really lazy. What is the purpose of studies if someone else do all the work for you? And your replies are really rude, people tried to help you but you just take it for granted.

1

Actually you are wrong, you can declare functions inside functions like that.

Thanks for the info Banfa. Appreciated. :)

more errors, why dont u test to compile these codes before posting it here

In case you missed my post, I already had. Quit cribbing about it.

yes, it works fine but not working when #include smp.h ?

Gracias, then your header file is to blame. It took me almost half a day to correct your single function, Sorry but you'll have to figure them out yourselves in the similar way. I wish you best of luck & a hint.

void S_EULR(double *,double ,double ,int ,double (*)());
void S_NEWT(int *,double *,double ,int ,double (*)());    //Change this & remove declaration in main.cpp
void S_OUTM(double *,int ,int ,char *);
void S_QR(double *,int ,double ,double *,double *);
void S_RNGK(double *,double ,double ,int ,double (*)())

Well so much for hint. But if you can't figure this one out, I'm sorry to say you don't deserve to get helped. After all DaniWeb helps those who helps themselves. (Let's not bring god into the picture, shall we.)

Edited by nbaztec: n/a

0

Firstly its a warning not an error, the program still compiled and possibly worked. However that said plenty of programmers use the rule of thumb that all warning should be dealt with and the program should compile without warnings.

You have declared S_NEWT as taking a pointer function with no parameters but supplied it with a pointer to a function that takes 3 parameters. Try fixing the prototype of S_NEWT to take account of the 3 parameters in USERF.

youre right, the program works! thanks to you,!!!

-2

Thanks for the info Banfa. Appreciated. :)

In case you missed my post, I already had. Quit cribbing about it.

Gracias, then your header file is to blame. It took me almost half a day to correct your single function, Sorry but you'll have to figure them out yourselves in the similar way. I wish you best of luck & a hint.

void S_EULR(double *,double ,double ,int ,double (*)());
void S_NEWT(int *,double *,double ,int ,double (*)());    //Change this & remove declaration in main.cpp
void S_OUTM(double *,int ,int ,char *);
void S_QR(double *,int ,double ,double *,double *);
void S_RNGK(double *,double ,double ,int ,double (*)())

Well so much for hint. But if you can't figure this one out, I'm sorry to say you don't deserve to get helped. After all DaniWeb helps those who helps themselves. (Let's not bring god into the picture, shall we.)

thanks for your help!! the program works, but not like youre saying, u just make the programs goes wrong,and more error comes out! haha,,thanks for that and thanks for wasting my time compiling your so called junk code.!

Votes + Comments
Thanks for the gratitude.
Show some appreciation and a better attitude
0

thanks for wasting my time compiling your so called junk code.!

It wasn't mine. It was yours.

but not like youre saying

Me and Banfa practically said the same things. What you interpreted of them is entirely your take.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.