CS 2073, Spring 2006
 Program 1
 Copy and Extend a C Program
    Week 1: Jan 18-20
 Due (on time): 2006-01-27  23:59:59
 Due (late):        2006-01-30  23:59:59

Program 1 must be emailed to: nrwagner@cs.utsa.edu
following directions for: running and submitting a C program, with deadlines:
  • 2006-01-27  23:59:59 (that's Friday, 27 January 2006, 11:59:59 pm) for full credit.
  • 2006-01-30  23:59:59 (that's Monday, 30 January 2006, 11:59:59 pm) for 75% credit.


Introduction: This assignment asks you to start with a simple C program. Then download it, and use a C development system to execute it. Next you should modify the program as indicated below, and execute again.


The "copy" program, dist.c: Here is the program for you to work on:

C Program to Copy and Run: dist.c
/*  dist.c: distance between 2 points          */
/*                                             */
/*  This program computes the                  */
/*  distance between two points.               */

#include <stdio.h>
#include <math.h>

int main() {
   /*  Declare variables. */
   double x1, y1, x2, y2;
   double a;

   /*  Initialize variables. */
   x1 = 1; y1 = 5; /* point p1 */
   x2 = 4; y2 = 7; /* point p2 */

   /* Print points. */
   printf("Point p1: (%6.4f, %6.4f)\n", x1, y1);
   printf("Point p2: (%6.4f, %6.4f)\n", x2, y2);

   /*  Compute distance. */
   a = sqrt((x2 - x1)*(x2 - x1) +
            (y2 - y1)*(y2 - y1));

   /*  Print distance. */
   printf("Distance between points: %8.4f\n", a);
}
Output of a run
Point p1: (1.0000, 5.0000)
Point p2: (4.0000, 7.0000)
Distance between points: 3.6056
    
Graph of triangle for part 2


What to do:
  1. Download and run the program dist.c given above. (Just use "copy" and "paste".) We will work on this in class using the Microsoft Visual Studio development system. You may also use whatever C that you have access to in this course. Check that you get the output shown above.

  2. Extend the program above to a new program triangle.c that has the additional features listed below. You should continue to indent three spaces as in the given program, and to supply comments similar to those in the given program.

    1. Add a third point with coordinates x3 and y3, initialized to 5 and 3. (You must also add declarations for the variables x3 and y3, following the model of the other similar variables.)

    2. Print the coordinates for this third point just as the first two points were printed.

    3. Calculate the other 2 distances (sides of the triangle), call them b and c, so that a, b and c are the lengths of the three sides. (You must also add declarations for the variables b and c, following the model of the variable a.)

    4. Print the values for b and c just as a's value was printed.

    5. Calculate the angle at the vertex including the sides b and c, call it A, using the formula:

        Angle of a triangle
        A = acos((b*b + c*c - a*a)/(2*b*c))
        

      (You must also add a declaration for the variable A. Note that acos is the way C designates the "arc cosine" function.)

    6. Use a similar formula to calculate the angle at the vertex including the sides a and c, call it B.
      Use a similar formula to calculate the angle at the vertex including the sides a and b, call it C.
      (You must also add declarations for the variables B and C.)

    7. Print the values for A, B, and C, which will be in radians.

    8. Print the sum of these three angles (also in radians): A+B+C.

    9. Print the sum of these three angles in degrees. Your final answer should be approximately 180.0.
      [Hint: To convert radians to degrees, you must multiply by 180.0/PI, where PI is 3.141592653589793. You should use the following statement right after the #include statements of your program:

        #define PI 3.141592653589793
        

      and this will define PI as the desired constant.]

    10. Use the following formulas to find the area of this triangle, given the lengths of the sides. Then print this area. (This result is known as Heron's formula, discovered over 2000 years ago.)

        Area of a triangle


What you should email: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Program Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate item letters: a, b, c, etc.


Revision date: 2005-12-30. (Please use ISO 8601, the International Standard.)