/******************************* 
 M2120-Fall'05-Lab 5 
 Author: Sergey Sadov 
 Date: Thursday Oct.6 2005 
 Summation of a geometric progression                         
********************************/

#include<stdio.h>
#include<math.h>
#define _B0 3  /*initial term of the progresion */
#define _Q  2   /*the ratio */
#define _N  10  /* number of terms */
/*#define _N  2*/   /* number of terms */

int main()
{
  double b, q, n;  /* the data */
  double s;        /* the result-to-be (accumulator) */
  
  double qi;       /* the i-th power of q in the loop */
  double analytic_result;
  
  int i;           /* loop counter */
  
  b=_B0;   /* initialization of b */
  q=_Q;   /* initialization of q */
  n=_N;   /* initialization of n */
  
  s=b;
  qi=1; /* initialization of qi: q^0=1 */
  
  for (i=1; i<n; i++)
  {
    qi=qi*q;   /*here qi becomes q^0* q = q^1 */
    s=s+b*qi;
  
    printf("q^%d=%lf, s=%lf\n",i,qi,s);
  }
  
  analytic_result=b*(pow(q,n)-1)/(q-1);
  
  printf("analytic_result=%lf\n", analytic_result);
  
  printf("Done\n");
  
  
  return(0);
}

