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

EG 106 sam Ra Sudn KO nhttps:/mail.google.com/mail/u/2/nbox/162d125da9af3ceß?pro

ID: 3710029 • Letter: E

Question

EG 106 sam Ra Sudn KO nhttps:/mail.google.com/mail/u/2/nbox/162d125da9af3ceß?projector-18messagePartid-0.1 File I/O and array example problems 1. Reading in a file with list of numbers Get the min, max, and size values of the array using pointers and output to one file Arrange the array in min to max and output to another file using the same array File would be formatted in a similar fashion . 67 32 67 23 2. Reading in a file of data points using multidimensional array Read the coordinate values of the location of the mass and the value of the mass from Calculate the center of mass Formula Center of mass in direction =-1(xim)

Explanation / Answer

#include <stdarg.h>

#include <stdio.h>

#include <stdlib.h>

int sortAscending(int * num1, int * num2);

void sort(int * arr, int size, int (* compare)(int *, int *));

int main(void)

{

static const char n1[] = "data.in";

static const char n2[] = "data_sort_arr.out";

static const char n3[] = "data_size_max_min.out";

FILE *f1 = fopen(n1, "r");

FILE *f2 = fopen(n2, "w");

FILE *f3 = fopen(n3, "w");

int *V = 0;

char line[1024];

int n = 0;

int max_n = 0;

if (f1 == 0){

printf("Failed to open file %s for reading ", n1);

return 0;

}

if (f2 == 0){

printf("Failed to open file %s for reading ", n2);

return 0;

}

while (fgets(line, sizeof(line), f1) != NULL)

{

int v;

if (sscanf(line, "%d", &v) != 1)

break;

if (n == max_n)

{

int new_n = (max_n + 2) * 2;

int *new_V = realloc(V, new_n * sizeof(*V));

if (new_V == 0){

printf("Failed to realloc array of size %d ", new_n);

return 0;

}

V = new_V;

max_n = new_n;

}

V[n++] = v;

}

int min=V[0];

int max=V[0];

int i;

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

if(V[i]>max)

max=V[i];

if(V[i]<min)

min=V[i];

}

fprintf(f3, "SIZE : %d ", n);

fprintf(f3, "MIN : %d ", min);

fprintf(f3, "MAX : %d ", max);

sort(V, n, sortAscending);

for (i = 0; i < n; i++)

fprintf(f2, "%d ", V[i]);

free(V);

fclose(f1);

fclose(f2);

fclose(f3);

return(0);

}

int sortAscending(int * num1, int * num2)

{

return (*num1) - (*num2);

}

void sort(int * arr, int size, int (* compare)(int *, int *))

{

// Pointer to last array element

int * arrEnd = (arr + size - 1);

// Pointer to current array element

int * curElem = arr;

int * elemToSort;

// Iterate over each array element

while(curElem <= arrEnd)

{

elemToSort = curElem;

// Compare each successive elements with current element

// for proper order.

while(elemToSort <= arrEnd)

{

/*

* Compare if elements are arranged in order

* or not. If elements are not arranged in order

* then swap them.

*/

if(compare(curElem, elemToSort) > 0)

{

*curElem ^= *elemToSort;

*elemToSort ^= *curElem;

*curElem ^= *elemToSort;

}

elemToSort++;

}

// Move current element to next element in array.

curElem++;

}

}