Question

A Les Mills BODYPUMP instructor has a list of the weight plates for her classes and...

A Les Mills BODYPUMP instructor has a list of the weight plates for her classes and saved in a file. Each weight plate was stored with the weight (kg), color, and quantity. Write a program that reads in the data (a list of weight plates) from a file, and sort the plates by weight, and write the ordered list to the output file.


Assume the input file plates.txt has the format of weight (double), color (one word string), followed by quantity (int) with in the following format:
10 blue 16
1.25 purple 20

1. Name your program weight_plates.c.


2. Declare a plate structure containing information of a weight plate’s weight, color, and quantity. Assume that there are no more than 100 plates 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. The list of plates should be stored in an array of struct plate.

4. Modify the selection_sort function provided so that it sorts an array of plate struct. The plates should be sorted by weight in ascending order. The function should have the following

prototype:

void selection_sort(struct plate array_plates[], int n);

5. Output the ordered plates in a text file called ordered_plates.txt, in the same format as the input file (with two decimal digits for weight).
Suggestions:

1. Set up plate 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 plates.
5. Initially output sorted array to the screen.
6. When output is correct, write to the output file.

The txt file includes the following:

40 yellow 10

2.5 blue 18

10 grey 26

5 sliver 28

15 pink 20

45 black 12

2 gold 14

25 green 16

1. red 16

30 purple 24

1.25 black 8

20 magenta 20

35 maroon 12

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* Structure definition */
struct plate{
   double weight;
   char color[100];
   int quantity;
};

/* Function prototypes */
void selection_sort(struct plate array_plates[],int n);
void print(struct plate array_plates[],int n);
void writeToFile(struct plate array_plates[],int n);

/* Definition of main function which will open a file to read and store the datat in an array of structure.*/
int main(){
   FILE *fp;       //file pointer
   char buff[100];   //character buffer
   int i=0;
   struct plate pt[100];   //Array of plates
   fp = fopen("input.txt", "r");     //opening the file to read
   /*Iterating through the file and storing data into array */
   while(fscanf(fp, "%s", buff)!=EOF){
   pt[i].weight = atof(buff);
   fscanf(fp, "%s", buff);
   strcpy(pt[i].color,buff);
   fscanf(fp,"%s",buff);
   pt[i].quantity = atoi(buff);
   i++;
   }
   printf("Data after file reading: \n");
   print(pt,i);       //Calling print function to display data of file before sorting
   selection_sort(pt,i);   // Calling sort function to sort array according to weight in ascending order.
   printf("Data after sorting on weight: \n");
   print(pt,i);       //Calling print function to display data of file after sorting
   writeToFile(pt,i);   //Calling function to write sorted data into output file.
   fclose(fp);       //Closing file pointer
}

/*Definition of selection sort*/
void selection_sort(struct plate array_plates[],int n){
   int i,pos,j;
   struct plate temp;
   for(i=0;i<n-1;i++){
       pos = i;
       for(j=i+1;j<n;j++){
           if(array_plates[pos].weight > array_plates[j].weight)
               pos = j;
       }
       if(pos != i){
           temp = array_plates[i];
           array_plates[i] = array_plates[pos];
           array_plates[pos] = temp;
       }
   }
}

/*Definition of print function to display data on screen.*/
void print(struct plate array_plates[],int n){
   int i;
   for(i=0;i<n;i++){
       printf("%0.2f",array_plates[i].weight);
       printf(" %s",array_plates[i].color);
       printf(" %d\n",array_plates[i].quantity);
   }
}

/*Definition of writeToFile function which will write sorted datat into output.txt file.*/
void writeToFile(struct plate array_plates[],int n){
   FILE *fp;
   int i;
   fp = fopen("output.txt","w");
   for(i=0;i<n;i++){
       fprintf(fp,"%0.2f",array_plates[i].weight);
       fprintf(fp," %s",array_plates[i].color);
       fprintf(fp," %d\n",array_plates[i].quantity);
   }
   fclose(fp);
}

SCREEN SHOTS:

Add a comment
Know the answer?
Add Answer to:
A Les Mills BODYPUMP instructor has a list of the weight plates for her classes and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A school teacher has a list of leftover school supplies from previous semesters. The teacher woul...

    A school teacher has a list of leftover school supplies from previous semesters. The teacher would like to maintain an ordered list of the supplies. Write a program in C that reads in the data (a list of supplies in random order) from a file, orders the items by name, and writes the ordered list to the output file. Assume the input file has the format of color (one word string), name, followed by quantity (int) with each type of...

  • Please write the complete code in C. Write a C function that prints the minimum spanning...

    Please write the complete code in C. Write a C function that prints the minimum spanning tree of a graph. At the end, print the weight of the spanning tree. A suggested report format is shown in the following example. Source Vertex To Vertex Weight A B 2 A C 4 B D 3 D E 1 Total weight of spanning tree: 10 Your main program will read a graph from DataIn file to an adjacency table before calling the...

  • This program will store a roster of most popular videos with kittens. The roster can include...

    This program will store a roster of most popular videos with kittens. The roster can include at most 10 kittens.You will implement structures to handle kitten information. You will also use functions to manipulate the structure. (1) Create a structure kitten. The structure should contain the following attributes: name; string color; string score; integer Important! The name of the structure and each of its field must match exactly for the program to work and be graded correctly. (2) Create a...

  • Hi, it's C++ question. Only can use <iostream> and <fstream>libraries Please help me, thanks Question Description:...

    Hi, it's C++ question. Only can use <iostream> and <fstream>libraries Please help me, thanks Question Description: For this project you will write a program to: a) read-in the 10 first names from a file (the file is a priori given to have exactly 10 entries, of a maximum length of 8 letters each) into a 2-dimensional character array, b) output the names to the terminal with each one preceded by a number indicating its original order in the list, c)...

  • // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given...

    // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given a partially completed program that creates a list of patients, like patients' record. // Each record has this information: patient's name, doctor's name, critical level of patient, room number. // The struct 'patientRecord' holds information of one patient. Critical level is enum type. // An array of structs called 'list' is made to hold the list of patients. // To begin, you should trace...

  • Hello! I'm posting this program that is partially completed if someone can help me out, I...

    Hello! I'm posting this program that is partially completed if someone can help me out, I will give you a good rating! Thanks, // You are given a partially completed program that creates a list of employees, like employees' record. // Each record has this information: employee's name, supervisors's name, department of the employee, room number. // The struct 'employeeRecord' holds information of one employee. Department is enum type. // An array of structs called 'list' is made to hold...

  • In C++ Programming Write a program in Restaurant.cpp to help a local restaurant automate its breakfast...

    In C++ Programming Write a program in Restaurant.cpp to help a local restaurant automate its breakfast billing system. The program should do the following: Show the customer the different breakfast items offered by the restaurant. Allow the customer to select more than one item from the menu. Calculate and print the bill. Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right of the item): Name Price Egg (cooked to order)...

  • 1. Define Structure UNIT as described in the list: ınit char codease complete the question int...

    1. Define Structure UNIT as described in the list: ınit char codease complete the question int numberOfCreditHours int semester int numberOfPreReq; pointer to number of pre-requisites eg:like char preRequisite[numberOfPreReq][10]; char* preReq int numberOfPostReq; pointer to number of post-requisites eg:like char postRequisite[numberOfPostReq] [1 0]; char* postReq NOTE: this is a BIG question and has to be asked seperately, answering only one question here. pluginfile.php/94428/mod resource/content/1/lab 6202 %20 %28file %20and%20parsing % 29.pdf Expected C++Skills to finish this activity Simple data types C++...

  • write a C++program to analyze a small subset of the data that has been collected. See...

    write a C++program to analyze a small subset of the data that has been collected. See file universities.txt .Use precisely seven parallel arrays: one for name of university, one for state, one for city, one for yearly tuition, one for enrollment, one for average freshman retention, and one for the percent of students who graduate with in six years. Note that the percentage of student accepted is not stored.An output file is opened in main() and remains open until the...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT