#include<stdio.h>
#include<math.h>
#define max 100
double f(double y);
int k, i;
double y[max];
double x[max];
void copyarray(double[], double[]);

main()
{
	FILE *file;
	double h, x, y;
	
	printf("this program does a first order ODE for dy/dx=-y+1 with initial condition of y(0)=0 from x=0 to x=0.9\n");
	printf("input the step size (h>0):\n");
	scanf("%lf",&h);

      
k=(int)((0.9-0)/h);
x[0]=0;
y[0]=0;
file = fopen("euler.data","w");

for(i=0;i<k;i++){
       
       x[i+1]=x[i]+h;
       y[i+1]=y[i]+h*f(y[i]);
       fprintf(file,"4.2%lf\t4.2%lf\n",x[i],y[i]);
       }

fclose(file);

}
double f(double y){
       int i;
       double sum, ysum;
       copyarray(x,y);                 
       for(i=0;i<k;i++){
       
       ysum=-y[i];
       }    
       sum=ysum+1;
       
       return(sum);

}

anything wrong with the code???? cos it keeps on getting error for array...can someone correct me?? and regarding coding for euler righ???

really need help....plsss...thankyou

What's a wonderful old-fashioned Fortran-like code on C++ Forum! ;)
1. int main() - don't write main() only.
2. You declare local variables x, y in the main body, so you can't access x, y arrays from outer scope (in C++ you can with :: qualifier but it's another story). Erase these senseless declarations.
3. Better ask the number of intervals then calculate step h value.
4. The f(double y) function must return 1.0 - y value - that's all. Why its body contains another statements?
5. What for copyarray() undefined function declared and used?
6. Are you sure you need x and y arrays at all? Why? You can declare x0, y0 then calculate new x and y then assign x0 = x, y0 = y and so on.
7. fprintf(file,"4.2%lf\t4.2%lf\n",x[i],y[i]); - what for 4.2? It's not Fortran format. Use "%lf\t%lf\n", it's enough.
8. Why k, i global variables? Avoid this bad practice, declare local k, i in the main body.
Well, correct sources and try again...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.