C++ help?

Discussion in 'Programming' started by jonhyhar, Oct 20, 2010.

  1. #1
    hey guys what's wrong with this codes? =/

    
    #include <stdio.h>
    #include <math.h>
    
    double f(double x);
    double dfdx(double x);
    int main(){
    	int i=0;
    	double x_eski = 10.0, // tahmini kok, x0
    		   x_yeni = 10.0,
    		   eps = 1.0e-3, // yakinsama kriteri
    		   hata = 100.0; // buyuk bir sayi
    		   
    	printf("%d %e\n", i, x_yeni);
    	
        while(hata > eps && i < 30){
                x_yeni=x_eski - f(x_eski)/dfdx(x_eski);
                i++;
                printf("%d %e\n", i, x_yeni);
    	    hata = fabs(x_yeni - x_eski) / fabs(x_eski);
    	    x_eski = x_yeni;
    	}
    
    	if(i == 30)
               printf("Raphson Newton yinelemesi yakinsamadý!\n");
    	return 0;
    }
    
    double f(double x){
    	return(x*x*x + x*x + x - 1.0);
    }
    
    double dfdx(double x){
    	return(3.0*x*x + 2.0*x + 1.0);
    }
    
    Code (markup):

     
    jonhyhar, Oct 20, 2010 IP
  2. papillon

    papillon Greenhorn

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    give us a clue - does it not compile or are we looking for a runtime error?
     
    papillon, Oct 22, 2010 IP
  3. CPACore

    CPACore Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If this is C++ code, I suggest you avoid C style output(printf), use C++ style output(cout<<), do not forget include<iostream>, and use english for comments please...XD, We can not even read it...
     
    CPACore, Oct 23, 2010 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    That is C style, not C++

    If it's just one main file, why bother defining prototypes, just do implementations at top, save a few lines.
     
    Kaizoku, Oct 29, 2010 IP
  5. k.mukesh

    k.mukesh Greenhorn

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    The above code is not c++.It is just c code.There is no class in it. There is no printf function in c++ instead it has cout function.If it is c++ their should be included include<iostream> library function at header. so it not a c++ code. i suggset you to rename it has c code.
     
    k.mukesh, Oct 31, 2010 IP