/* Program to check whether a sum converges well */ #include #include double sumfunc(int N1, int N2, int increment,double (*function)(double)) { int i; double tally,current_val; tally=0.0; for (i=N1;i!=(N2+increment);i=i+increment) /*This condition allows us to go forward or backwards. We stop when we get to the point where we have gone past the target value*/ { current_val=i*1.0; tally=tally+(*function)(current_val); /*The *function expression here allows us to change the program easily to consider other functions.*/ } return(tally); } double one_over_x(double x) { double value; value=1.0/x; } main() { int imin,imax; double x,sum1,sum2; printf("What is the minimum value for the variable?\n"); scanf("%i",&imin); printf("What is the maximum value for the variable?\n"); scanf("%i",&imax); sum1=sumfunc(imin,imax,+1,&one_over_x); printf("Going forward the sum is %lf \n", sum1); sum2=sumfunc(imax,imin,-1,&one_over_x); printf("Going backward the sum is\n %lf \n", sum2); printf("The difference is %lf\n",fabs(sum1-sum2)); printf("The difference times N is %lf\n",(imax-imin+1)*fabs(sum1-sum2)); }