/* AMAT 2120 Fall 2005, Assignment 2, Q.1 
 [Sum of arithmetic progression -31+(24)+... (999 terms, difference 7).]
 Instructor's solution
 Author: S.Sadov, Date: Oct.15 2005
 URL: http://www.math.mun.ca/~m2120#sol2 
*/  
#include <stdio.h>
/* To simplify changes in data, I use symbolic constants */
#define _A0   -31  /* first term */
#define _DIFF 7    /* difference */
#define _N    999  /* number of terms */
        /*    2    -- used when debugging */

int main()
{
  int a0; /* first term */
  int a;  /* current term in summation process */
  int d; /* difference */
  int N; /* number of terms in the sum */
  int i; /* loop counter */
  int sum; /* accumulator */
  int sum_theor; /* the sum found by theoretical formula 
                    [a(first)+a(last)]*N/2 
		 */
  			   
  /* initialization */
  a0=_A0;
  d=_DIFF;
  N=_N;

  /* In a potential subsequent version, 
     we could initialize a,d,N via interactive input */

  /* Printing information about program and data */ 
    
  printf("This program finds the sum of arithmetic progression\n");
  printf("beginning with %d, having difference %d and containing %d terms\n",
                         a0,                    d,                N);

  /* Computation */
  
  a=a0;  /* initialize current term */
  sum=0; /* initialize accumulator */
  
  for (i=1; i<=N; i++)
  {
    sum+=a; /* add term to sum */
    a+=d;   /* update the term */  
  }   
  printf("Computed sum=%d\n", sum);
  
  /* Comparing to theoretical sum */
  sum_theor=(a0+ (a0+(N-1)*d)) *N/2.0;
  
  printf ("Theoretical sum=%d\n", sum_theor);
  
  printf("Done\n");    
}


