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

C program using Linux/Unix environment: A gym would like to maintain an ordered

ID: 3915748 • Letter: C

Question

C program using Linux/Unix environment:
A gym would like to maintain an ordered list of the dumbbells for its group fitness classes. Each dumbbell was stored with the weight (# of lbs), color, and quantity. Each dumbbell was stored with the weight (# of lbs), color, and quantity. Write a program that reads in the data (a list of dumbbells in random order) from a file, and order the dumbbells by weight, and write the ordered list to the output file.

Assume dumbbells.txt has the format of weight (int), color (one word string), followed by quantity (int) with each type of dumbbell on a separate line:

8 blue 16

3 purple 20

1. Name your program dumbbells.c.

2. The program should be built around an array of structures, with each structure containing information of a dumbbell’s weight, color, and quantity. Assume that there are no more than 100 dumbbells in the file. Assume that color is one word, and no more than 100 characters.

3. Use fscanf and fprintf to read and write data.

4. Copy the selection_sort function provided in selection_sort.c to your program and modify it so that it sorts an array of dumbbell struct. The dumbbells should be sorted by weight in ascending order. The function should have the following prototype:

void selection_sort(struct dumbbell array_dumbbells[], int n);

5. Output the ordered dumbbells in a text file called ordered_dumbbells.txt, in the same format as the input file.

Suggestions:

1. Set up dumbbell struct.

2. Use fscanf function to read the input file (note that fscanf returns number of entries filled).

3. Initially output unsorted array to screen to make sure the file input and array setup are working.

4. Modify the selection_sort function for processing dumbbells.

5. Initially output sorted array to the screen.

6. When output is correct, write to the output file.

dumbells.txt data:

2 blue 16
10 grey 16
5 sliver 28
1 black 12
15 pink 20
8 orange 22
20 black 12
4 green 18
12 red 16
3 purple 24

selection_sort.c code:

#include <stdio.h>

#define N 10

void selection_sort(int a[], int n);

int main(void)

{

int i;

int a[N];

printf("Enter %d numbers to be sorted: ", N);

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

scanf("%d", &a[i]);

selection_sort(a, N);

printf("In sorted order:");

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

printf(" %d", a[i]);

printf(" ");

return 0;

}

void selection_sort(int a[], int n)

{

int i, largest = 0, temp;

if (n == 1)

return;

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

if (a[i] > a[largest])

largest = i;

if (largest < n - 1) {

temp = a[n-1];

a[n-1] = a[largest];

a[largest] = temp;

}

selection_sort(a, n - 1);

}

Explanation / Answer

#include<stdio.h>

#define N 100 // Size for array of structure dumbbell

/* structure for dumbbell */

struct dumbbell

{

int weight, quantity;

char color[100];

};

/* Function to read data from dumbbells.txt */

int readData(struct dumbbell d[])

{

int i = 0;

FILE *f;

f = fopen("dumbbells.txt","r");

if(f == NULL)

{

printf("Error. Cannot open file");

return 0;

}

printf("dumbbells.txt data: ");

while(fscanf(f, "%d %s %d", &d[i].weight, d[i].color, &d[i].quantity) == 3) // fscanf will return 3, since it is reading 3 values

{

printf("%d %s %d ",d[i].weight,d[i].color,d[i].quantity);

i++; // i will be used as number of records

}

return i;

fclose(f);

}

/* Function to write data to ordered_dumbbells.txt */

void writeData(struct dumbbell d[], int n)

{

FILE *f;

f = fopen("ordered_dumbbells.txt","w");

if(f == NULL)

{

printf("Error. Cannot open file");

return;

}

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

{

fprintf(f,"%d %s %d ",d[i].weight,d[i].color,d[i].quantity);

}

fclose(f);

}

/* Modified selection sort function */

void selection_sort(struct dumbbell array_dumbbells[], int n)

{

int i, largest = 0;

struct dumbbell temp;

if (n == 1)

return;

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

if (array_dumbbells[i].weight > array_dumbbells[largest].weight)

largest = i;

if (largest < n - 1)

{

temp = array_dumbbells[n-1];

array_dumbbells[n-1] = array_dumbbells[largest];

array_dumbbells[largest] = temp;

}

selection_sort(array_dumbbells, n - 1);

}

int main()

{

struct dumbbell d[N];

int n;

n = readData(d);

selection_sort(d,n);

printf("Sorted array of dumbbells: ");

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

{

printf("%d %s %d ", d[i].weight, d[i].color, d[i].quantity);

}

writeData(d,n);

return 0;

}