/* Program to compute sin(x) + sin(2x) + ... sin(Nx) */ /* (AMAT 2120, Midterm, Q.2) */ /* Author, date...*/ /* Version 1 (23/30) */ #include <stdio.h> #include <math.h> /* need it because sin function is employed */ int main() { int N, i; double x, sum; x=1.2; N=10; sum = 0.0; for (i=1; i <= N; i++) { sum = sum + sin(i*x); } printf("The sum of the series is %lf\n", sum); return(0); }
Next version adds
the interactive input thus
making the program useful for non-programmers.
It would be worth 27.5/30 marks.
Please note the line
printf("This program calculates sin(x) + sin(2x) + ... + sin(Nx)\n");
The program should let users know what it does!
There is a little extra here: conversion from degrees to radians.
However it doesn't really add universality to the program.
More important (positively) is that the program explicitly tells
the user what angular units to use. No user-friendliness point is lost.
In the final version Shannon included validation (checking that N>0) and an options to switch between radian and degree angle units (for that, he introduced a new variable angleType -- a switch between the radian and degree cases -- and the corresponding scanf).
In fact, he went a bit further and included even the validation of the new switch. I would award 31/30 to this work.
On Friday, we had first encounter with Maple computer algebra system, click here for details.
As a supplementary material, here is the discussion of possible parallel (shared) development practices as applied to our fairly simple project.
Finally, this pretty innocently looking mathematical problem reveals interesting links with recurrence relations and complex numbers. It turns possible to accelerate summation and even to find the sum in a closed form!