COMP 1917 Computing 1
Session 2, 2016

Lab Solutions - Week 2


Note: This code should be appropriately commented.

  /*
     heron.c

     Use Heron's formula to compute the area of a triangle,
     given the side lengths.
  */

  #include <stdio.h>
  #include <math.h>
  // remember to compile with -lm

  int main( void )
  {
    float a,b,c; // sidelengths of a triangle
    float s;     // semi-perimeter
    float area;

    printf( "Enter sidelengths of a triangle:\n" );

    scanf( "%f", &a );
    scanf( "%f", &b );
    scanf( "%f", &c );

    // compute semi-perimeter
    s = ( a + b + c )/ 2;

    // check the triangle inequality
    if( a > b+c || b > a+c || c > a+b ) {
      printf( "Error: triangle inequality violated.\n" );
    }
    else {
      // compute area using Heron's formula
      area = sqrt(s*(s-a)*(s-b)*(s-c));

      printf( "Area = %1.2f\n", area );
    }

    return 0;
  }
  /*
    easter.c

    code for Butcher's Formula copied from
    http://smart.net/~mmontes/nature1876.html
  */

  #include <stdio.h>

  int main( void )
  {
    int a,b,c,d,e,f,g,h,i,k,l,m,p;
    int year,month,date;

    printf("Enter Year: ");
    scanf("%d",&year);

    printf("in %d, Easter is ",year);

    a=year%19;
    b=year/100;
    c=year%100;
    d=b/4;
    e=b%4;
    f=(b+8)/25;
    g=(b-f+1)/3;
    h=(19*a+b-d-g+15)%30;
    i=c/4;
    k=c%4;
    l=(32+2*e+2*i-h-k)%7;
    m=(a+11*h+22*l)/451;
    month =(h+l-7*m+114)/31; // [3=March, 4=April]

    p=(h+l-7*m+114)%31;
    date=p+1; //    (date in Easter Month)

    if( month == 3 ) {
      printf("March");
    }
    else {
      printf("April");
    }
    printf(" %d\n",date);
    return 0;
  }