Thread: Please help, 4 lines of errors i just cant solve!!!

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    3

    Please help, 4 lines of errors i just cant solve!!!

    Hi, first post on this board so please be kind. Basically i'm writing a C code which is a modified version of the Newton method of root finding. It involves a few loops and looks like it should wortk but i keep getting these errors:
    Code:
    phy-pc3195:Desktop guest$ gcc Newton2.c -o Newton
    Newton2.c: In function ‘main’:
    Newton2.c:30: error: incompatible type for argument 3 of ‘f’
    Newton2.c:35: error: incompatible type for argument 3 of ‘f’
    Newton2.c: In function ‘newton’:
    Newton2.c:53: error: incompatible type for argument 3 of ‘f’
    Newton2.c:53: error: incompatible type for argument 3 of ‘f_prime’
    
    Here's my code:
    
    #include <stdio.h> 
    #include <math.h> 
    
    double newton(double x_0, double tol, int max_iters, 
              int* iters_p, int i, int* converged_p);
    double f(double n, int i, double x[i]);
    double f_prime(double n, int i, double x[i]);
    
    int main() {
    	int    i;        /* One of four temperature ranges*/
       double x_0;       /* Initial guess                 */
       double x[3];      /* Approximate solution          */
       double tol;       /* Maximum error                 */
       int    max_iters; /* Maximum number of iterations  */
       int    iters;     /* Actual number of iterations   */
       int    converged; /* Whether iteration converged   */
       float n;         /* Number density                */
    
       printf("Enter x_0, tol, and max_iters\n");
       scanf("%lf %lf %d", &x_0, &tol, &max_iters);
       for(n=0.01;n<=100;n=n*1.05) {
       	for(i=0;i<4;i++) {
       		
       x[i] = newton(x_0, tol, max_iters, &iters, i, &converged);
    
       if (converged) {
        printf("Newton algorithm converged after %d steps.\n", 
             iters);
        printf("The approximate solution is %19.16e\n", x[i]);
        printf("f(%19.16e) = %19.16e\n", x[i], f(n, i, x[i]));
       } else {
        printf("Newton algorithm didn't converge after %d steps.\n", 
              iters);
        printf("The final estimate was %19.16e\n", x[i]);
        printf("f(%19.16e) = %19.16e\n", x[i], f(n, i, x[i]));
      }
       	}}
       return 0;
    }  /* main */
    
    
    double newton(double x_0, double tol, int max_iters, 
              int* iters_p, int i, int* converged_p) {
       double x;
       double x_prev;
       int    iter = 0;
       float n;
       x=x_0;
    
       do {
          iter++;
          x_prev = x;
          x = x_prev - f(n, i, x_prev)/f_prime(n, i, x_prev);
       } while (fabs(x - x_prev) > tol && iter < max_iters);
    
       if (fabs(x - x_prev) <= tol)
          *converged_p = 1;
       else
          *converged_p = 0;
       *iters_p = iter;
       
       return x;
    }  /* newton algorithm */
    
    
    double f(double n, int i, double x[i]) {
    	if (i=0) {
    		return 1-((3.42e+16*pow(x[i],2.12)*n)/0.015);
    	}
       else if (i=1) {
       	return 1-((9.10e+18*x[i]*n)/0.015);
       }
       else if (i=2) {
       	return 1-((1.11e+20*pow(x[i],0.56)*n)/0.015);
       }
       else if (i=3) {
       	return 1-((2.00e+8*pow(x[i],3.67)*n)/0.015);
       }
       else printf("Temperature outside the given range");
       
    }  /* f */
    
    double f_prime(double n,int i, double x[i]) {
       if (i=0) {
       	return (-7.2504e+16*pow(x[i],1.12)*n)/0.015;
       }
       else if (i=1) {
       	return (-9.10e+18*n)/0.015;
       }
       else if (i=2) {
       	return (-6.216e+19*pow(x[i],-0.44)*n)/0.015;
       }
       else if (i=3) {
       	return (-7.34e+8*pow(x[i],2.67)*n)/0.015;
       }
       else printf("Temperature outside given range");
    }  /* f_prime */
    Any help would be appreciated, thanks a lot.
    Aarran
    Last edited by Salem; 07-22-2009 at 11:08 AM. Reason: Added [code][/code] tags, learn to use them yourself

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    phy-pc3195esktop guest$ gcc Newton2.c -o Newton
    Newton2.c: In function ‘main’:
    Newton2.c:30: error: incompatible type for argument 3 of ‘f’
    Newton2.c:35: error: incompatible type for argument 3 of ‘f’
    Newton2.c: In function ‘newton’:
    Newton2.c:53: error: incompatible type for argument 3 of ‘f’
    Newton2.c:53: error: incompatible type for argument 3 of ‘f_prime’
    Firstly, Please use code tags.
    << !! Posting Code? Read this First !! >>

    Second, All your errors are related to passing the wrong types to your functions, your prototype for the function f is defined as -
    Code:
    double f(double n, int i, double x[])
    which means it expects a double, an int and a pointer.

    and in lines 30 & 35 you are passing an element of the array instead of its address -

    Element :
    Code:
    printf("f(%19.16e) = %19.16e\n", x[i], f(n, i, x[i]));
    Address:
    Code:
    printf("f(%19.16e) = %19.16e\n", x[i], f(n, i, x));
    Your other errors on line 53 are similar,-

    Now, the same function expects an array however you are passing the simple double x_prev.
    Code:
    x = x_prev - f(n, i, x_prev)/f_prime(n, i, x_prev);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with DX9 Errors!!!
    By Tommaso in forum Game Programming
    Replies: 7
    Last Post: 06-28-2006, 02:51 PM
  2. Errors with header files in OpenGL using VisualC++
    By wile_spice in forum Game Programming
    Replies: 3
    Last Post: 06-22-2006, 08:56 AM
  3. Winsock compilation errors
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-03-2005, 08:00 AM
  4. Errors when including winsock2.h
    By skiingwiz in forum Windows Programming
    Replies: 2
    Last Post: 12-27-2002, 07:32 PM
  5. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 08:43 PM