Second Week Examples: Summing Series

Following D. Milicic Second and Third Week Examples.

Pretty print.

Once your program is running, you may wish to automatically indent loops and if statements. Suppose that your source code is called foo.c To straighten out the indentation, you may run indent foo.c in an xterm window and then the indentation will be automatically adjusted in the file foo.c For example, to print out the file you may type cat foo.c

Cast.

A variable can be converted to another type by applying the operator called cast. For example if i is an integer, (float) i is the same number but considered as a floating point number.
/****************************************************************************************
Treibergs                                                                          1-16-6
Second Week First Example
Program to print running sums of the harmonic series using a floating point accumulator 
sum.c
****************************************************************************************/

#include <stdio.h>
#include <stdlib.h>

int 
main(void) 
{
    int  i, n;
    float sum;

    n = 10;
    sum = 0;

    printf("n\t summand \t running total\n");
    printf("--\t-------- \t -------------\n");
    for (i = 1; i <= n; i=i+1)
          {
           sum = sum + (float)1/(float)i;
           printf("%d \t %f\t   %f\n",i,(float)1/(float)i,sum);
           }

    printf("\n Sum of %d terms is %f\n",n,sum);

    return EXIT_SUCCESS;

}



/*********************************************************************
Treibergs                                                       1-16-6
Second Week Second Example
Program to sum of the harmonic series using a
floating point accumulator 
sum2.c
*********************************************************************/

#include <stdio.h>
#include <stdlib.h>


int 
main(void) 
{
    int  i, n;
    float sum;

    n = 1000;
    sum = 0;

    for (i = 1; i <= n; i=i+1)
          {
           sum = sum + (float)1/(float)i;
           }

    printf("\n Sum of %d terms is %f\n",n,sum);

    return EXIT_SUCCESS;

}


Essentially the same program as above (for a different series), but with doubles (double-precision floats) instead of floats:
/********************************************************************
Treibergs                                                      1-16-6
Second Week Third Example
Program to sum the inverse squares series using
a double precision accumulator 
sum3.c
********************************************************************/

#include <stdio.h>
#include <stdlib.h>

int 
main(void) 
{

    int  i, n;
    double sum;

    n = 1000;
    sum = 0.0;

    for (i = 1; i <= n; i=i+1)
          sum = sum + 1.0/((double)i*(double)i);

    printf("Sum of %d terms is %lf\n",n,sum);

    return EXIT_SUCCESS;

}

Linking math header files

It may happen that the linker fails to find the math.h header file and your mathematical functions don't work. Suppose that your source code is in a text file called table.c. To guarantee that the math files are correctly linked, invoke the compiler with the call cc table.c -lm in an xterm window.

Tables of functions


/*******************************************************************
Treibergs                                1-16-6
Second Week Fourth Example
Program to print a table of sines
table.c
*******************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define Pi M_PI

int 
main(void) 
{
    int i;
    
    printf("\n\n Angle(deg.)\t   Sine\n");
    printf("------------    ---------\n");

    for( i=5; i <= 90; i=i+5 )
    {
        printf("%6d. \t %f\n",i,sin((i/90.0)*(Pi/2)));
    }
    return EXIT_SUCCESS;
}



if - else Construction


/*********************************************************************
Treibergs                                                       1-16-6
Second Week Fifth Example
Even or odd
par.c
*********************************************************************/

#include <stdio.h>
#include <stdlib.h>

int 
main(void) 
{

    int i;

    for (i = 1; i <= 10 ; i=i+1) 
    {
        if (i % 2 == 0)
            printf("The number %d is even\n",i);
        else
            printf("The number %d is odd\n",i);
    }
    return EXIT_SUCCESS;
}

/*******************************************************************
Treibergs                                1-16-6
Second Week Sixth Example
Even or odd, 2nd version
par2.c
*******************************************************************/

#include <stdio.h>
#include <stdlib.h>

int
main(void) 
{

    int i;

    for (i = 1; i <= 10 ; i=i+1) 
    {
        if (i % 2 != 1)
            printf("The number %d is even\n",i);
        else
            printf("The number %d is odd\n",i);
    }
    return EXIT_SUCCESS;
}

Alternating Harmonic Series


/*******************************************************************
Treibergs                                                     1-16-6
Second Week Seventh Example
Partial Sum of Alternating Harmonic Series
sum4.c
*******************************************************************/


#include <stdio.h>
#include <stdlib.h>

int
main(void) 
{

   int i,n;
   double x, sum;
    
   sum = 0.0;
   n =  1000;

   for (i = 1; i <= n ; i=i+1) 
   {
        x = (double) i;
        if (i % 2 == 0)
           sum = sum - 1.0/x ;
        else
           sum = sum + 1.0/x;
    }

   printf("Sum of %d terms of alternating harmonic series is %lf\n", n, sum);

   return EXIT_SUCCESS;
}



/*******************************************************************
Treibergs                                                     1-16-6
Second Week Eighth Example
Partial Sum of Alternating Harmonic Series -- Second version
sum5.c
*******************************************************************/


#include <stdio.h>
#include <stdlib.h>

int
main(void) 
{

   int i,n;
   double sum, termsign;
    
   sum = 0.0;
   termsign = 1.0;
   n =  1000;

   for (i = 1; i <= n ; i=i+1) 
   {
        sum = sum + termsign/i;
        termsign = -termsign;
    }

   printf("Sum of %d terms of alternating harmonic series is %lf\n", n, sum);

   return EXIT_SUCCESS;
}