Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PROBLEM. Write a program that uses structures and pointers. You will receive a p

ID: 3573335 • Letter: P

Question

PROBLEM. Write a program that uses structures and pointers. You will receive a partially written program and a data file of triangle sides.

• The names of items in triangle require the use of the dot notation

• The names of the items in kind_counts require the use of the points-into notation ( -> ).

• When you print the triangle kind and when you print the Stats at the bottom, you need to print the name of the triangle kind. They already exist in the character array called triangle_kind_name. You can use that array to print the words.

        To print the word for each triangle, refer to: triangle_kind_name[triangles[t].triangle_kind]

        To print the word in the statistics section, you can use: triangle_kind_name[your_loop_counter].

• When printing, I used: %3i for number, %4i for triangle sides, %8.3f for the area, %s for the triangle_kind_name, and %2i for the stats numbers. Your choices may vary. Make sure your columns are aligned

The program must be written in C and follow the psuedo code exactly.

ALGORITHM DEVELOPMENT - PSEUDO CODE:

/*-------------------------------------------------------------*/

main

// Main code provided for you

return triangle_count

/*-------------------------------------------------------------*/

/* THIS IS A SUB-FUNCTION THAT YOU HAVE TO WRITE */

void figure_triangle_area (triangle_t triangles[],

int triangle_count)

loop from zero < triangle_count, increment by one

{

Set a, b, c equal to the full names of the sides

Compute s

Compute the area

(PS: sides and area use the “dot” notation)

[HINT: area = sqrt(s(s-a)(s-b)(s-c)) where s = (a+b+c)/2 ]

}

return

/*-------------------------------------------------------------*/

void figure_triangle_kind (triangle_t triangles[],

int triangle_count)

// Code provided for you

return

/*-------------------------------------------------------------*/

void count_kind_stats (triangle_t triangles[],

int triangle_count, stats_t *kind_counts)

// Code provided for you

return

/*-------------------------------------------------------------*/

/* THIS IS A SUB-FUNCTION THAT YOU HAVE TO WRITE */

void print_all (triangle_t triangles[],

        int triangle_count,

                    stats_t *kind_counts)

do an fopen with error checking.

print your name and assignment number.

print the column header lines

loop from zero < triangle_count increment by one

{

print the triangle number (Hint: based on the loop counter)

loop from zero, < NSIDES, increment by one

print a side

print the area

print the kind and a New Line

}

print the Stats header

loop from zero, <3, increment by one

print number of triangles-kinds

return

/*-------------------------------------------------------------*/

Here are the provided codes if interested. I just need the subfunctions written. The main is already provided.

https://www.dropbox.com/sh/otol39e484ue2sg/AAD4Dx4-uYXUNgVCgDQ5u9pca?dl=0

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#define OUT_FILENAME "prog5.out"

#define IN_FILENAME "prog5.dat"

// #define IN_FILENAME "prog5test.dat"

#define MAXN_TRIANGLES 20

#define NSIDES 3

#define EQUILATERAL 0

#define ISOSCELES 1

#define SCALENE 2

char * triangle_kind_name[] ={"Equilateral", "Isosceles", "Scalene"};

typedef struct triangle_kind_count

{  

    int t_kind_counts[3];

     

} stats_t;

        

/* Function Prototype Section */

                               

    /* get_data is a function to get and read data */

int get_data (char *filename, triangle_t triangles[]);

    /* figure_triangle_area does just that */

void figure_triangle_area (triangle_t triangles[], int triangle_count);

    /* figure_triangle_kind, either Equilateral, Isosceles, or Scalene */

void figure_triangle_kind(triangle_t triangles[],int triangle_count);

    /* add up how many of each type of triangle */

void count_kind_stats(triangle_t triangles[], int triangle_count,stats_t *kind_counts);

    /* Print the whole thing */

void print_all(triangle_t triangles[], int triangle_count,stats_t *kind_counts);               

/*-----------------------------------------------------------*/

int main(void)

{

    int triangle_count;

    triangle_t triangles[MAXN_TRIANGLES];

    stats_t kind_counts = {0,0,0};

        

    /* Start the action. */

    triangle_count = get_data(IN_FILENAME, triangles);

    figure_triangle_area(triangles, triangle_count);

    figure_triangle_kind(triangles, triangle_count);

    count_kind_stats(triangles, triangle_count, &kind_counts);

    print_all(triangles, triangle_count, &kind_counts);

    return EXIT_SUCCESS;

}

/*-----------------------------------------------------------*/

/* This function will open the input file, and read the      */

/* sides of each triangle into the array triangles           */

int get_data (char *filename,       /* input */ triangle_t triangles [])   /* output */

{

    int t, c;

    int scan_count=0;

    int triangle_count=0;

    FILE *in_file;

    /* Open the data file and read in the array */

    in_file = fopen(filename, "r");

    if (in_file == NULL)

    {

        printf("Error on fopen of %s ", filename);

        exit(EXIT_FAILURE);

    }

    /* Use "t" for Triangles/rows.

       Use "c" for columns */

    for(t = 0; t < MAXN_TRIANGLES; t++)

    {

        for(c = 0; c < NSIDES; c++)

        {

            scan_count = fscanf(in_file, "%i", &triangles[t].side[c]);

                                                    

            if (scan_count == EOF)

                break;

        }

        if (scan_count == EOF)

            break;

        triangle_count++;

    }

    fclose(in_file);

    return triangle_count;

}

void figure_triangle_area (triangle_t triangles[],int triangle_count)

{

          int a,b,c;

          float s;

         FILE *in_file;

         in_file = fopen(filename, "a");

        if (in_file == NULL)

    {

        printf("Error on fopen of %s ", filename);

        exit(EXIT_FAILURE);

    }

for(int i=0;i<triangle_count;i++)

{

                    a= triangles[i].side[0];

                     b= triangles[i].side[1];

                     c= triangles[i].side[2];

                   s=(a+b+c)/2;       

triangles[i].area = sqrt(s(s-a)(s-b)(s-c));

fprintf(in_file, "%F", triangles[i].area);  

}

fclose(in_file);

return

}

/* figure_triangle_kind, either Equilateral, Isosceles, or Scalene */

void figure_triangle_kind(triangle_t triangles[], int triangle_count)

{

    int t;

     for (t = 0; t < triangle_count; t++)

    {  

        if(triangles[t].side[0] == triangles[t].side[1] &&

           triangles[t].side[1] == triangles[t].side[2] )

        {

           triangles[t].triangle_kind = EQUILATERAL;

        }

        else if (triangles[t].side[0] == triangles[t].side[1] ||

                 triangles[t].side[1] == triangles[t].side[2] ||

             triangles[t].side[0] == triangles[t].side[2] )

        {

           triangles[t].triangle_kind = ISOSCELES;

        }

        else

        {

           triangles[t].triangle_kind = SCALENE;

        }

    }

    return;

}

/*----------------------------------------------------------------*/

/* add up how many of each kind of triangle                       */

void count_kind_stats(triangle_t triangles[],

              int triangle_count,

              stats_t *kind_counts)

{

    int t;

    for (t = 0; t < triangle_count; t++)

    {

        (kind_counts->t_kind_counts [triangles[t].triangle_kind]) ++;

    }

    return;

}

/*----------------------------------------------------------------*/

/* Print the whole thing                                          */

void print_all (triangle_t triangles[],int triangle_count,stats_t *kind_counts)

{

FILE * outfile;

outfile = fopen(filename, "a");

          if (outfile == NULL)

          {

                    printf("Error on fopen of %s ", filename);

                    exit(EXIT_FAILURE);

          }

          fprintf(outfile, " Your Name. Program 5 output. ");   fprintf(outfile, "TRIANGLES ");

          fprintf(outfile, "Count   Sides         Area       Kind ");

            fprintf(outfile, "----- -----------   --------   ------------- ");

          for(int i=0;i< triangle_count;i++)

        {

                   fprintf(outfile,” %3i%4i %4i %4i%8.3f%s ”,i+1, triangles[i].side[0], triangles[i].side[1]; triangles[i].side[2], triangles[i].area, triangles[t].triangle_kind);

}

fprintf(outfile, "Star header ");

for(int i=0;i<3;i++)

{

fprintf(outfile, “%2i ”,kind_counts[i]);

}

}

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#define OUT_FILENAME "prog5.out"

#define IN_FILENAME "prog5.dat"

// #define IN_FILENAME "prog5test.dat"

#define MAXN_TRIANGLES 20

#define NSIDES 3

#define EQUILATERAL 0

#define ISOSCELES 1

#define SCALENE 2

char * triangle_kind_name[] ={"Equilateral", "Isosceles", "Scalene"};

typedef struct triangle_kind_count

{  

    int t_kind_counts[3];

     

} stats_t;

        

/* Function Prototype Section */

                               

    /* get_data is a function to get and read data */

int get_data (char *filename, triangle_t triangles[]);

    /* figure_triangle_area does just that */

void figure_triangle_area (triangle_t triangles[], int triangle_count);

    /* figure_triangle_kind, either Equilateral, Isosceles, or Scalene */

void figure_triangle_kind(triangle_t triangles[],int triangle_count);

    /* add up how many of each type of triangle */

void count_kind_stats(triangle_t triangles[], int triangle_count,stats_t *kind_counts);

    /* Print the whole thing */

void print_all(triangle_t triangles[], int triangle_count,stats_t *kind_counts);               

/*-----------------------------------------------------------*/

int main(void)

{

    int triangle_count;

    triangle_t triangles[MAXN_TRIANGLES];

    stats_t kind_counts = {0,0,0};

        

    /* Start the action. */

    triangle_count = get_data(IN_FILENAME, triangles);

    figure_triangle_area(triangles, triangle_count);

    figure_triangle_kind(triangles, triangle_count);

    count_kind_stats(triangles, triangle_count, &kind_counts);

    print_all(triangles, triangle_count, &kind_counts);

    return EXIT_SUCCESS;

}

/*-----------------------------------------------------------*/

/* This function will open the input file, and read the      */

/* sides of each triangle into the array triangles           */

int get_data (char *filename,       /* input */ triangle_t triangles [])   /* output */

{

    int t, c;

    int scan_count=0;

    int triangle_count=0;

    FILE *in_file;

    /* Open the data file and read in the array */

    in_file = fopen(filename, "r");

    if (in_file == NULL)

    {

        printf("Error on fopen of %s ", filename);

        exit(EXIT_FAILURE);

    }

    /* Use "t" for Triangles/rows.

       Use "c" for columns */

    for(t = 0; t < MAXN_TRIANGLES; t++)

    {

        for(c = 0; c < NSIDES; c++)

        {

            scan_count = fscanf(in_file, "%i", &triangles[t].side[c]);

                                                    

            if (scan_count == EOF)

                break;

        }

        if (scan_count == EOF)

            break;

        triangle_count++;

    }

    fclose(in_file);

    return triangle_count;

}

void figure_triangle_area (triangle_t triangles[],int triangle_count)

{

          int a,b,c;

          float s;

         FILE *in_file;

         in_file = fopen(filename, "a");

        if (in_file == NULL)

    {

        printf("Error on fopen of %s ", filename);

        exit(EXIT_FAILURE);

    }

for(int i=0;i<triangle_count;i++)

{

                    a= triangles[i].side[0];

                     b= triangles[i].side[1];

                     c= triangles[i].side[2];

                   s=(a+b+c)/2;       

triangles[i].area = sqrt(s(s-a)(s-b)(s-c));

fprintf(in_file, "%F", triangles[i].area);  

}

fclose(in_file);

return

}

/* figure_triangle_kind, either Equilateral, Isosceles, or Scalene */

void figure_triangle_kind(triangle_t triangles[], int triangle_count)

{

    int t;

     for (t = 0; t < triangle_count; t++)

    {  

        if(triangles[t].side[0] == triangles[t].side[1] &&

           triangles[t].side[1] == triangles[t].side[2] )

        {

           triangles[t].triangle_kind = EQUILATERAL;

        }

        else if (triangles[t].side[0] == triangles[t].side[1] ||

                 triangles[t].side[1] == triangles[t].side[2] ||

             triangles[t].side[0] == triangles[t].side[2] )

        {

           triangles[t].triangle_kind = ISOSCELES;

        }

        else

        {

           triangles[t].triangle_kind = SCALENE;

        }

    }

    return;

}

/*----------------------------------------------------------------*/

/* add up how many of each kind of triangle                       */

void count_kind_stats(triangle_t triangles[],

              int triangle_count,

              stats_t *kind_counts)

{

    int t;

    for (t = 0; t < triangle_count; t++)

    {

        (kind_counts->t_kind_counts [triangles[t].triangle_kind]) ++;

    }

    return;

}

/*----------------------------------------------------------------*/

/* Print the whole thing                                          */

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote