Code examples: Tuesday, 10/15

Array of Structs

// basic geometry - struct demo
// 2019-10-15
#include <stdio.h>

struct Point {
    double x, y, z;
    double weight;
} ;

struct Point weighted_average(struct Point ary[], int size);

int main(int argc, char **argv)
{
    struct Point mypoints[6];
    struct Point *origin;

    printf("Enter points:  x  y  z  weight \n");
    for (int i = 0; i < 6; i++) {
        scanf(" %lf %lf %lf %lf",
            &(mypoints[i].x), &(mypoints[i].y), &(mypoints[i].z), &(mypoints[i].weight));
    }

    struct Point average;
    average = weighted_average(mypoints, 6);

    for (int i = 0; i < 6; i++)
        printf("%6.2lf  %6.2lf  %6.2lf  %.3lf\n",
            mypoints[i].x, mypoints[i].y, mypoints[i].z, mypoints[i].weight);

    origin = mypoints + 3;   // OR origin = &(mypoints[3]);

    printf("\norigin: %6.2lf  %6.2lf  %6.2lf  %.3lf\n",
        (*origin).x, (*origin).y, (*origin).z, (*origin).weight);

    printf("average: %6.2lf  %6.2lf  %6.2lf  %.3lf\n",
        average.x, average.y, average.z, average.weight);

    return 0;
}

struct Point weighted_average(struct Point ary[], int size)
{
    struct Point wa;
    wa.x = wa.y = wa.z = wa.weight = 0.0;

    for (int i = 0; i < size; i++) {
        wa.x += ary[i].x * ary[i].weight;
        wa.y += ary[i].y * ary[i].weight;
        wa.z += ary[i].z * ary[i].weight;
        wa.weight += ary[i].weight;
    }
    wa.x /= wa.weight;
    wa.y /= wa.weight;
    wa.z /= wa.weight;

    return wa;
}

Pointers to Structs

// basic geometry - struct demo
// 2019-10-15
#include <stdio.h>

struct Point {
    double x, y, z;
    double weight;
} ;

struct Point weighted_average(struct Point ary[], int size);

int main(int argc, char **argv)
{
    struct Point mypoints[6];
    struct Point *origin;

    printf("Enter points:  x  y  z  weight \n");
    for (int i = 0; i < 6; i++) {
        scanf(" %lf %lf %lf %lf",
            &(mypoints[i].x), &(mypoints[i].y), &(mypoints[i].z), &(mypoints[i].weight));
    }

    struct Point average;
    average = weighted_average(mypoints, 6);

    for (int i = 0; i < 6; i++)
        printf("%6.2lf  %6.2lf  %6.2lf  %.3lf\n",
            mypoints[i].x, mypoints[i].y, mypoints[i].z, mypoints[i].weight);

    origin = mypoints + 3;   // OR origin = &(mypoints[3]);

    printf("\norigin: %6.2lf  %6.2lf  %6.2lf  %.3lf\n",
        origin->x, origin->y, origin->z, origin->weight);

    printf("average: %6.2lf  %6.2lf  %6.2lf  %.3lf\n",
        average.x, average.y, average.z, average.weight);

    return 0;
}

struct Point weighted_average(struct Point ary[], int size)
{
    struct Point wa, *p;
    wa.x = wa.y = wa.z = wa.weight = 0.0;

    p = ary;
    for (int i = 0;  i < size ;  i++, p++) {
        wa.x += p->x * p->weight;
        wa.y += p->y * p->weight;
        wa.z += p->z * p->weight;
        wa.weight += p->weight;
    }
    wa.x /= wa.weight;
    wa.y /= wa.weight;
    wa.z /= wa.weight;

    return wa;
}

Linked List of Structs

// basic geometry - struct demo
// 2019-10-15
#include <stdio.h>
#include <stdlib.h>     // malloc( ),  free( )

struct Point {
    double x, y, z;
    double weight;
    struct Point *next;
} ;

struct Point weighted_average(struct Point *head);

int main(int argc, char **argv)
{
    struct Point *head, *cursor;

    head =NULL;
    printf("Enter points:  x  y  z  weight \n");
    while (1) {
        cursor = (struct Point *)malloc( sizeof(struct Point) );
        int retcode = scanf(" %lf %lf %lf %lf",
            &(cursor->x), &(cursor->y), &(cursor->z), &(cursor->weight));
        if (retcode == EOF)
            break;
        else {
            cursor->next = head;
            head = cursor;
        }
    }
    free(cursor);

    struct Point average;
    average = weighted_average(head);

    for (cursor = head; cursor != NULL; cursor = cursor->next)
        printf("%6.2lf  %6.2lf  %6.2lf  %.3lf\n",
            cursor->x, cursor->y, cursor->z, cursor->weight);

    printf("average: %6.2lf  %6.2lf  %6.2lf  %.3lf\n",
        average.x, average.y, average.z, average.weight);

    return 0;
}

struct Point weighted_average(struct Point *head)
{
    struct Point wa, *p;
    wa.x = wa.y = wa.z = wa.weight = 0.0;

    for (p = head; p != NULL; p = p->next) {
        wa.x += p->x * p->weight;
        wa.y += p->y * p->weight;
        wa.z += p->z * p->weight;
        wa.weight += p->weight;
    }
    wa.x /= wa.weight;
    wa.y /= wa.weight;
    wa.z /= wa.weight;

    return wa;
}