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

C programming . In C please add a function to this code so that it will sort by

ID: 3775126 • Letter: C

Question

C programming .

In C please add a function to this code so that it will sort by age.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <stdlib.h>

#define PAUSE system("pause")

typedef struct {

       int age;

       int weight;

       int height;

} STATS;

STATS *makeArrayOfPointers(int );

void loadArray(STATS **array, int *c);

void displayArray(STATS **array, int c);

void saveArray(STATS **array, int count, int size);

STATS **reloadArray(char *, int *count, int *size);

main() {

       STATS** array = 0;

       int count = 0;

       int size = 500;

       char reloaded = 'N';

       array = reloadArray(&reloaded, &count, &size);

       if(reloaded == 'N')

          array = makeArrayOfPointers(size);

       PAUSE;

       loadArray(array, &count);

       printf("** unSorted Array ** ");

       displayArray(array, count);

       //sortArray(array, count)

       printf("** Sorted by Age ** ");

       displayArray(array, count);

       saveArray(array, count, size);

       PAUSE;

} // end of main

void displayArray(STATS **array, int c) {

       int i;

       for (i = 0; i < c; i++) {

              printf("Record[%i] Age is %i. Weight is %i. Height is %i. ", i, array[i]->age, array[i]->weight, array[i]->height);

       }

       PAUSE;

} // end displayArray

void loadArray(STATS **array, int *c) {

       int value;

       int counter = *c;

       for (*c; *c < (counter + 4); *c = *c + 1) {

              printf(" Information for person: %i ", (*c) + 1);

              array[*c] = calloc(1, sizeof(STATS));

              printf("Enter age: ");

              scanf("%i", &value);

              array[*c]->age = value;

              printf("Enter weight: ");

              scanf("%i", &value);

              array[*c]->weight = value;

              printf("Enter height: ");

              scanf("%i", &value);

              array[*c]->height = value;

       }    // end for

} // end loadArray

STATS *makeArrayOfPointers(int size) {

       STATS *result;

       result = malloc(sizeof(STATS*) * size);

       return result;

} // end makeArrayOfPointers

void saveArray(STATS **array, int count, int size) {

       FILE *ptr;

       int i;

       ptr = fopen("c:\myBinFile.bin", "wb");

       if (ptr == NULL) {

              printf("Could not open the file ");

              PAUSE;

              exit(-1);

       }

       // SAVE THE SIZE OF THE ARRAY

       fwrite(&size, sizeof(int), 1, ptr);

       // SAVE THE EFFECTIVE SIZE or COUNT

       fwrite(&count, sizeof(int), 1, ptr);

       // SAVE EACH NODE/ELEMENT in the ARRAY

       for (i = 0; i < count; i++) {

              fwrite(array[i], sizeof(STATS), 1, ptr);

       } // end for

       fclose(ptr);

}// end saveArray

STATS **reloadArray(char *result, int *count, int *size) {

       STATS **temp = 0;

       FILE *ptr;

       int i;

       *result = 'Y';

       ptr = fopen("c:\myBinFile.bin", "rb");

       if (ptr == NULL) {

              printf("Could not open the file ");

              PAUSE;

              *result = 'N';

       }

       else {

              // Reload the size of the array

              fread(size, sizeof(int), 1, ptr);

              // Create the Array of Pointers

              temp = makeArrayOfPointers(*size);

              // Reload the count or effective size variable

              fread(count, sizeof(int), 1, ptr);

              // Reload the nodes or elements of the array

              for (i = 0; i < *count; i++) {

                     temp[i] = calloc(1, sizeof(STATS));

                     fread(temp[i], sizeof(STATS), 1, ptr);

              }

       } // end else

       fclose(ptr);

       return temp;

}// end reloadArray

Explanation / Answer

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <stdlib.h>

#define PAUSE system("pause")

typedef struct {

       int age;

       int weight;

       int height;

} STATS;

STATS *makeArrayOfPointers(int );

void loadArray(STATS **array, int *c);

void displayArray(STATS **array, int c);

void saveArray(STATS **array, int count, int size);

STATS **reloadArray(char *, int *count, int *size);

main() {

       STATS** array = 0;

       int count = 0;

       int size = 500;

       char reloaded = 'N';

       array = reloadArray(&reloaded, &count, &size);

       if(reloaded == 'N')

          array = makeArrayOfPointers(size);

       PAUSE;

       loadArray(array, &count);

       printf("** unSorted Array ** ");

       displayArray(array, count);

       //sortArray(array, count)

       printf("** Sorted by Age ** ");

       displayArray(array, count);

       saveArray(array, count, size);

       PAUSE;

} // end of main

void displayArray(STATS **array, int c) {

       int i,j;

       for (i = 0; i < c; i++) {

for (j = i + 1; j < c; ++j)
{
if (c[i] < c[j])
{
a = c[i];
c[i] = c[j];
c[j] = a;
}
}

              printf("Record[%i] Age is %i. Weight is %i. Height is %i. ", i, array[i]->age, array[i]->weight, array[i]->height);

       }

       PAUSE;

} // end displayArray

void loadArray(STATS **array, int *c) {

       int value;

       int counter = *c;

       for (*c; *c < (counter + 4); *c = *c + 1) {

              printf(" Information for person: %i ", (*c) + 1);

              array[*c] = calloc(1, sizeof(STATS));

              printf("Enter age: ");

              scanf("%i", &value);

              array[*c]->age = value;

              printf("Enter weight: ");

              scanf("%i", &value);

              array[*c]->weight = value;

              printf("Enter height: ");

              scanf("%i", &value);

              array[*c]->height = value;

       }    // end for

} // end loadArray

STATS *makeArrayOfPointers(int size) {

       STATS *result;

       result = malloc(sizeof(STATS*) * size);

       return result;

} // end makeArrayOfPointers

void saveArray(STATS **array, int count, int size) {

       FILE *ptr;

       int i;

       ptr = fopen("c:\myBinFile.bin", "wb");

       if (ptr == NULL) {

              printf("Could not open the file ");

              PAUSE;

              exit(-1);

       }

       // SAVE THE SIZE OF THE ARRAY

       fwrite(&size, sizeof(int), 1, ptr);

       // SAVE THE EFFECTIVE SIZE or COUNT

       fwrite(&count, sizeof(int), 1, ptr);

       // SAVE EACH NODE/ELEMENT in the ARRAY

       for (i = 0; i < count; i++) {

              fwrite(array[i], sizeof(STATS), 1, ptr);

       } // end for

       fclose(ptr);

}// end saveArray

STATS **reloadArray(char *result, int *count, int *size) {

       STATS **temp = 0;

       FILE *ptr;

       int i;

       *result = 'Y';

       ptr = fopen("c:\myBinFile.bin", "rb");

       if (ptr == NULL) {

              printf("Could not open the file ");

              PAUSE;

              *result = 'N';

       }

       else {

              // Reload the size of the array

              fread(size, sizeof(int), 1, ptr);

              // Create the Array of Pointers

              temp = makeArrayOfPointers(*size);

              // Reload the count or effective size variable

              fread(count, sizeof(int), 1, ptr);

              // Reload the nodes or elements of the array

              for (i = 0; i < *count; i++) {

                     temp[i] = calloc(1, sizeof(STATS));

                     fread(temp[i], sizeof(STATS), 1, ptr);

              }

       } // end else

       fclose(ptr);

       return temp;

}// end reloadArray