/* Title: Demonstration of computing x=j*h Two methods are used to compute x=j*h, using (3.40) and (3.41) in the text. see Section 3.3 for more information. */ #include const int n = 20; const float h = 0.1; main() { float xsum, xprod, errsum, errprd; double dx, dh; int j; /* Initialize */ xsum = 0.0; dh = h; printf("\nFollowing are the values of j, x=j*h, and"); printf("\nthe errors in the computation of x using"); printf("\n(3.40) [product] and (3.41) [sum], respectively.\n\n"); for(j = 1; j <= n; j++) { xsum = xsum + h; xprod = j*h; dx = j*dh; errsum = dx - xsum; errprd = dx - xprod; printf("\t%3d\t%13.10f\t%11.2e\t%11.2e\n", j,dx,errprd,errsum); } return 0; }