Lab 8: Midterm programming question

In this lab, Shannon wrote the program to compute the sum
     sin x + sin 2x + ... +  sin Nx
from scratch. The first version, which would be worth 23/30 on the test, is what you should practice your hand in writing. (Let your brain take a rest.)

/* 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);
}
Here is the evaluation breakdown:
1. Relevance and correctness of mathematics 11/11
2. C syntax and programming style 9/9
3. User friendliness 3/7 - 4/7
The user of this program can only be a programmer having access to the code. For such a person it is pretty easy to understand what the program is about (there is a comment on top). And it is clear perhaps even to a less experienced programmer how to control (change) the values of x and N. However, to secure 4/7 marks, I recommend to add a little comment: say, /* in-code initialization */ immediately before the initialization lines.
4. Scope and universality 0/3   (no extras)

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!