C programming For this code I only need to add a function so

C programming.   

For this code I only need to add a function so that the array of pointers is sorted by the age of the individual entered.

it must be a function and called in main . That is the only thing needed.

#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.\\t Weight is %i.\\t 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

Solution

//Declare function prototype above main function

void sortArray(STATS **array, int count);

//call sortArray function in main fuction

sortArray(array, count);

//Write the function defination as fallows

/*I have used Bubble sort technique to sort the array. The following code will sort the array in ascending order of age. In Bubble sort it checks the cosecutive elements of array and swaps the elements if first element is greater than second element. Keep checking elements till end so that the greatest element will move to last position of array. */

void sortArray(STATS **array, int count)
{
STATS **temp;
int i,j;

//No. of Iterations required
for(i=0;i<count;i++)
{

//to check consecutive elements
for(j=0;j<count-1-i;j++)
{

//check condition
if(array[j]->age > array[j+1]->age)
{

//swapping of array elements
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}//end of if
}//end of j loop
}//end of i loop
}//end of fuction

C programming. For this code I only need to add a function so that the array of pointers is sorted by the age of the individual entered. it must be a function a
C programming. For this code I only need to add a function so that the array of pointers is sorted by the age of the individual entered. it must be a function a
C programming. For this code I only need to add a function so that the array of pointers is sorted by the age of the individual entered. it must be a function a
C programming. For this code I only need to add a function so that the array of pointers is sorted by the age of the individual entered. it must be a function a
C programming. For this code I only need to add a function so that the array of pointers is sorted by the age of the individual entered. it must be a function a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site