Eleventh Week Examples ---
  Structures

Following D. Milicic Eleventh Week Examples and Twelvth Week Examples

Structures


In next examples we discuss the notion of a structure, which genralizes data types and arrays. A structure may consist of several data types gathered as a unit. Stuctures allow nesting, so that a structure may contain substructures. It is to data as function subprograms are to code. Structures allow one to think of components logically. We use examples from geometry. We set up structures that represent geometric entities: point, segment, triangle and tetrahedron. The fact that we can view, say, a triangle as a unit enables us to organize our thinking in terms of the bigger picture. We contemplate triangles and tetrahedrons instead of the x and y-coordinates of a bunch of points. Breaking a programming problem (or any problem) consisting of many details into smaller, more manageable pieces, and then viewing the problem "top down" with the pieces as units is usually referred to as structured programming. It is a valuable heuristic for complex problems.

In the next seven examples, the word point has a different meanings. The first example is a structure representing points in the plane. Two variables A,B are allocated memory according to the variable type given by the structure. The first program calculates the distance between two given points.


/* Milicic

Structures 1.

 point1.c               */

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

int main() {

float d;

struct {
  float x;
  float y;
} A, B;

printf("The coordinates of the point A are: ");
scanf("%f %f",&A.x,&A.y);

printf("\nThe coordinates of the point B are: ");
scanf("%f %f",&B.x,&B.y);

d = sqrt((A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y));
printf("\nThe distance between A and B is %f\n",d);

}


In the second example we give the structure the tag point before the program main. Then struct point is like any variable declaration, and assigns memory to two struct point structures A and B. Look at the fourth example to see how to use struct tag to declare functions, function arguments and nested structures.
/* Milicic

Structures 2.

 point2.c               */

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

struct point {
  float x;
  float y;
};

int main(){

float d;
struct point  A, B;

printf("The coordinates of the point A are: ");
scanf("%f %f",&A.x,&A.y);

printf("\nThe coordinates of the point B are: ");
scanf("%f %f",&B.x,&B.y);

d = sqrt((A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y));
printf("\nThe distance between A and B is %f\n",d);

}

The third example we define a new type point. Thus the single word point now identifies the data type struct tag. This is the most flexible use of structures.
/* Milicic

Structures 3.

 poin31.c               */


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

typedef struct {
  float x;
  float y;
} point ;

float dist( point A, point B) {
  return(sqrt((A.x - B.x)*(A.x - B.x) + (A.y - B.y)*(A.y - B.y)));
}

int main(){

float d;
point A, B;

printf("The coordinates of the point A are: ");
scanf("%f %f",&A.x,&A.y);

printf("\nThe coordinates of the point B are: ");
scanf("%f %f",&B.x,&B.y);

printf("\nThe distance between A and B is %f\n", dist(A,B));
}

Fourth, we show several uses of structures. We show how we define a structure tagged coords which is a pair of doubles. We show how to declare a structure tagged threepoints which is a structure of structures, representing a triangle. We give a program that inputs the vertices of a triangle and an angle, computes the area and perimeter of the triangle, rotates the triangle by the given angle, and then recomputes the area and perimeter of the new triangle.
/******************************************************************************
Treibergs                                                          Mar. 3, 2006 

Structures

The first structure is tagged "coords" which represents a point.
The second structure is tagged "threepoints" which represents a triangle.

The main program asks for the vertices of a triangle. A function call prints 
the vertices of a triangle. Then it prints the area of the triangle and the 
perimeter of the triangle. These are computed by double-valued functions that 
take structure arguments. The perimeter function calls the edge-length function
which is a double valued function that takes two structure coords arguments.

It then asks for a rotation angle. It computes a new triangle which is the 
rotation about the origin of the old tringle. This time we use a structure valued 
function. Finally we compute the area and perimeter of the new triangle. We are 
relieved that the area and perimeter work out to be the same as before, as they
should.

struct.c
********************************************************************************/
      
# include <stdio.h>
# include <stdlib.h>
# include <math.h>

struct coords   /* Structure tagged "coords" consists of two doubles                */
{           /* Every data type  struct coords  has two members called  .x  and  .y  */
     double x;
     double y;
 };
    
struct threepoints /* Nested structures: a structure tagged "threepoints"  */
 {                 /* consists of three "struct coords" structures         */
     struct coords aa;
     struct coords bb;
     struct coords cc;
 }
 aaa;  /* A global variable named "aaa" is declared of the type
            "struct threepoints". ie., we reserve memory for a triangle  aaa  */
     
double
lengthe( struct coords v, struct coords w );   
     
double
areat( struct threepoints t );

double
perimetert( struct threepoints t );

void
printt( struct threepoints t );

struct threepoints
rotatet( struct threepoints t, double x );
    
int
main( void )
{
  double angledeg, anglerad;
  struct threepoints temp;
   
   printf ( "Triangle Program - - - Playing with Structures\n\n" );
   printf ( "Enter the first vertex x y : " );
   scanf ( "%lf%lf", &aaa.aa.x, &aaa.aa.y );
   
   printf ( "Enter the second vertex x y : " );
   scanf ( "%lf%lf", &aaa.bb.x, &aaa.bb.y );
   
   printf ( "Enter the third vertex x y : " );
   scanf ( "%lf%lf", &aaa.cc.x, &aaa.cc.y );
   
   printt ( aaa );

   printf (" The area of the triangle is %f\n", areat( aaa ) );
   printf (" and the perimeter of the triangle is %f\n", perimetert( aaa ) );

   printf ( "\n Enter the rotation angle in degrees : " );
   scanf ( "%lf", &angledeg );
   
   anglerad = angledeg * M_PI / 180.;
 
   printf ( "\n After rotation, \n" );
   temp = rotatet( aaa, anglerad );
   printt ( temp );
   printf (" The area of the triangle is %f\n", areat( temp ) );
   printf (" and the perimeter of the triangle is %f\n", perimetert( temp ) );
   
   return EXIT_SUCCESS;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Example of a double valued function that takes two "struct coords" arguments.   */

double
lengthe ( struct coords v, struct coords w )
{
return sqrt ( (v.x  -  w.x) * (v.x  -  w.x)  +  (v.y  -  w.y) * (v.y  -  w.y) );
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 

double
areat( struct threepoints t )
{
return fabs ( ( t.bb.x * t.cc.y  +  t.aa.x * t.bb.y  +  t.aa.y * t.cc.x
             -  t.bb.y * t.cc.x  -  t.aa.x * t.cc.y  -  t.aa.y * t.bb.x ) / 2.0 );
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Example of a function that doesn't return anything.                             */
/*            It only prints the vertices of a triangle.                           */

void
printt ( struct threepoints t )
{
printf ( "The vertices of the triangle are\n (%f,%f), (%f,%f), (%f,%f).\n",
           t.aa.x, t.aa.y, t.bb.x, t.bb.y, t.cc.x, t.cc.y );
return;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Example of a structure-valued function.                                         */
/* Input triangle and angle to rotate in radians. Output rotated triangle.         */

struct threepoints
rotatet( struct threepoints t, double x )
{
      struct threepoints u;
      double s,c;
      s = sin(x);
      c = cos(x);
      u.aa.x  =  c * t.aa.x  -  s * t.aa.y;
      u.aa.y  =  s * t.aa.x  +  c * t.aa.y;
      u.bb.x  =  c * t.bb.x  -  s * t.bb.y;
      u.bb.y  =  s * t.bb.x  +  c * t.bb.y;
      u.cc.x  =  c * t.cc.x  -  s * t.cc.y;
      u.cc.y  =  s * t.cc.x  +  c * t.cc.y;
      return  u;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Note that the member of a structure of type  struct threepoints,  t,  is given  */
/* by  t.aa  which itself is a structure of type  struct coords.                   */

double
perimetert( struct threepoints t )
{
return  lengthe ( t.aa, t.bb )  +  lengthe ( t.bb, t.cc )  +  lengthe ( t.cc, t.aa );
}


Fifth, define a structure of structures, segment, consisting of two points. Write a float function with arguments of type segment that returns the distance between the endpoints.
/* Milicic

Distance

 distance.c                                                                         */

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

typedef struct {
  float x;
  float y;
} point ;

typedef struct {
  point first;
  point last;
} segment ;

float dist( segment A) {
  return(sqrt((A.first.x - A.last.x)*(A.first.x - A.last.x) + 
(A.first.y - A.last.y)*(A.first.y - A.last.y)));
}

int main(){

float d;
segment A;

printf("The coordinates of the first point are: ");
scanf("%f %f",&A.first.x,&A.first.y);

printf("\nThe coordinates of the last point are: ");
scanf("%f %f",&A.last.x,&A.last.y);

printf("\nThe length of the segment is %f\n", dist(A));
}

Area of a triangle

Almost the same program as struct.c above. Different author.
/* Milicic

Area of triangle

triangle.c                                                              */

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

typedef struct {
     float x;
     float y;
     } point;

typedef struct {
     point ver1;
     point ver2;
     point ver3;
     } triangle;

float area(triangle T) {
     return(fabs((T.ver2.x - T.ver1.x)*(T.ver3.y-T.ver1.y) -
     (T.ver2.y - T.ver1.y)*(T.ver3.x-T.ver1.x))/2);
}

int main() {

triangle T;
float a;

printf("The first point is: ");
scanf("%f %f",&T.ver1.x,&T.ver1.y);

printf("The second point is: ");
scanf("%f %f",&T.ver2.x,&T.ver2.y);

printf("The third point is: ");
scanf("%f %f",&T.ver3.x,&T.ver3.y);

a = area(T);
printf("The area of the triangle is %f.\n",a);
}


Volume of a tetrahedron

Program defines structure point and structure of structures tetrahedron. It defines point valued functions of two points, Cross and Diff, the float valued function of two points, Dot, and the float valued function of tetrahedron, Volume.
/* Milicic

Volume of a tetrahedron

tetr.c                                                         */

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

typedef struct {
        float x;
        float y;
        float z;
} point;

typedef struct {
        point ver1;
        point ver2;
        point ver3;
        point ver4;
} tetrahedron;


point Diff(point a, point b) {
        point c;

        c.x = a.x - b.x;
        c.y = a.y - b.y;
        c.z = a.z - b.z;

        return(c);
}


point Cross(point a, point b) {
        point c;

        c.x = a.y*b.z - a.z*b.y;
        c.y = a.z*b.x - a.x*b.z;
        c.z = a.x*b.y - a.y*b.x;

        return(c);
}

float Dot(point a, point b) {
        return(a.x*b.x + a.y*b.y + a.z*b.z);
}

float Volume(tetrahedron t) {
        point a, b, c;

        a = Diff(t.ver3,t.ver1);
        b = Diff(t.ver2,t.ver1);
        c = Diff(t.ver4,t.ver1);

        return(fabs(Dot(c,Cross(a,b)))/6);
}

int main(){
        tetrahedron T;
        float vol;

printf("The coordinates of the first point are: ");
scanf("%f %f %f", &T.ver1.x, &T.ver1.y, &T.ver1.z);

printf("The coordinates of the second point are: ");
scanf("%f %f %f", &T.ver2.x, &T.ver2.y, &T.ver2.z);

printf("The coordinates of the third point are: ");
scanf("%f %f %f", &T.ver3.x, &T.ver3.y, &T.ver3.z);

printf("The coordinates of the fourth point are: ");
scanf("%f %f %f", &T.ver4.x, &T.ver4.y, &T.ver4.z);

vol = Volume(T);

printf("The volume of the tetrahedron T is equal to %f\n", vol);

}


/* Treibergs ----used for today's demonstration program        4-3-6
almost verbatim copy of program by
Milicic

Volume of a tetrahedron

today.c                                                         */

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

typedef struct 
{
        float x;
        float y;
        float z;
} 
point;

typedef struct 
{
        point ver1;
        point ver2;
        point ver3;
        point ver4;
} 
tetrahedron;

/* * * * * * * * * * * * * * * * * * * * * */

point 
Diff(point a, point b)
 {
        point c;

        c.x = a.x - b.x;
        c.y = a.y - b.y;
        c.z = a.z - b.z;

        return(c);
}

/* * * * * * * * * * * * * * * * * * * * * */


point 
Cross(point a, point b)
 {
        point c;

        c.x = a.y*b.z - a.z*b.y;
        c.y = a.z*b.x - a.x*b.z;
        c.z = a.x*b.y - a.y*b.x;

        return(c);
}

/* * * * * * * * * * * * * * * * * * * * * */

float 
Dot(point a, point b) 
{
        return(a.x*b.x + a.y*b.y + a.z*b.z);
}

/* * * * * * * * * * * * * * * * * * * * * */


float 
Volume(tetrahedron t) 
{
        point a, b, c;

        a = Diff(t.ver3,t.ver1);
        b = Diff(t.ver2,t.ver1);
        c = Diff(t.ver4,t.ver1);

        return ( fabs ( Dot ( c, Cross ( a, b ) ) ) / 6.0 );
}

/* * * * * * * * * * * * * * * * * * * * * */


int 
main(void)
{
        tetrahedron T;
        float vol;
        printf( "Volume of a Tetrahedron\n\n" );

        printf("enter tthe coordinates of the first point : ");
        scanf("%f %f %f", &T.ver1.x, &T.ver1.y, &T.ver1.z);

        printf("Enter the coordinates of the second point : ");
        scanf("%f %f %f", &T.ver2.x, &T.ver2.y, &T.ver2.z);

        printf("Enter the coordinates of the third point : ");
        scanf("%f %f %f", &T.ver3.x, &T.ver3.y, &T.ver3.z);

        printf("Enter the coordinates of the fourth point : ");
        scanf("%f %f %f", &T.ver4.x, &T.ver4.y, &T.ver4.z);

        vol = Volume ( T );

        printf ( "The volume of the tetrahedron T is equal to %f\n", vol);

        return EXIT_SUCCESS;

}