#include /* Modifies the previous program sum.cpp. Now there is an outer loop in which sum goes from 0 to 10 by 2's. Notice that sum must be reset to 0 inside the outer loop before the inner loop. Also note the cout line commented out. This is a helpful debugging tool. */ /* computes the sum 1*(0*0) + 4*(1*1) + 2*(2*2) + 4*(3*3) + 2*(4*4) + ... + 2*(num-2)*(num-2) + 4*(num-1)*(num-1) + 1*(num*num), where num is a positive even integer. (This sort of sum comes up in Simpson's rule in calculus.) If i is a counter variable going from 0 to num, then the general term in the sum is of the form coeff*i*i, where the coefficient coeff is determined by this rule: if i is odd, then coeff = 4 if i is 0 or i is num, then coeff = 1 if i is even, and not 0 or num, then coeff = 2 */ void main() { int num, sum, i, coeff; for (num=0; num <= 10; num +=2){ sum = 0; for (i=0; i<=num; i++){ if (i%2) coeff = 4; else if (i==0 || i==num) coeff = 1; else coeff = 2; sum += coeff * i*i; /* cout << "i = " << i << ", sum = " << sum << ", coeff = " << coeff << ", num = " << num << endl; */ } cout << "num = " << num << ", sum = " << sum << endl; } }