Question

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 supply on a separate line:

blue 16 crayon
purple 20 #2 pencil red 6 pencil box orange 23 glue sticks ...

  1. The user will be asked to enter the input file name. The list of sorted supply list should be written to the same file name as the input file name with “sorted_” added at the beginning. For example, if the original file name is Mrs_Olson_supply.txt, the output file name is then sorted_Mrs_Olson_supply.txt.
  2. Read the data using fscanf function. Assume color is one word. To read the name (last item in a line), use "%[^\n]\n" as the conversion specifier for fscanf function.
  3. Define a structure supply to store the color (string), quantity(integer), and name (string). Assume the color and name are no more than 100 characters.
  4. Build an array of supply structures. Assume that there are no more than 200 supply in the list.
  5. Modify the selection_sort function provided to sort an array of supplies. The supply list should be sorted by name using strcmp in ascending order. For example, crayon should be before glue sticks in the list.The function should have the following prototype:

void selection_sort(struct supply list[], int n);

6. The output file should include all supply in the same format as the input file but in sorted order by name.

***In most cases, a function should have a brief comment above its definition describing what it does. Other than that, comments should be written only needed in order for a reader to understand what is happening***

------------------------------------------------------

Mrs_Olson_supply.txt file information below:

red 6 pencil box
blue 1 pencil box
orange 16 glue sticks
mutli 9 facial tissues
black 8 notebook
blue 20 crayon
purple 8 crayon
pink 5 post-it
white 4 pencil box
purple 2 protractor
pink 15 eraser
red 2 dry-erase marker
red 13 #2 pencil
yellow 8 2-pocket folder
blue 5 post-it
green 3 pairs of kids scissors
multi 7 pack of colored pencils
green 4 2-pocket folder
red 5 notebook
blue 6 dry-erase marker
yellow 4 highlighter
blue 6 2-pocket folder
mutli 10 #2 pencil
red 13 crayon
red 3 rollerball pen
yellow 3 post-it
red 4 pairs of kids scissors
black 5 composition book
white 4 pack of index cards
yellow 8 #2 pencil
white 2 binder
black 5 rollerball pen
white 3 school glue
black 5 dry-erase marker

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// supply structure
struct supply
{
   char name[100];
   char color[100];
   int quantity;
};

// sort function prototype
void selection_sort(struct supply list[], int n);

int main()
{
   // asking user to enter file name
   char filename[100];
   printf("Enter the file name to read data: ");
   scanf("%s", filename);

   // opening file to read
   FILE *infile = fopen(filename, "r");

   // check file open status
   if(infile == NULL) {
       printf("Cannot open file!\n");
       return 1;
   }

   // array of supply named list
   struct supply list[200];

   // reding data from file
   int i = 0;
   while(1)
   {
       if(feof(infile)) { // if end of file reached
           break;
       }
       fscanf(infile, "%s %d %[^\n]\n", list[i].color, &list[i].quantity, list[i].name);
       i++;
   }

   selection_sort(list, i);

   // changing output file name
   char outName[107] = "sorted_";
   int j,k;
   for(j = 7, k = 0; j < strlen(filename)+7; j++, k++)
   {
       outName[j] = filename[k];
   }

   // opening file for output
   FILE *outfile = fopen(outName, "w");
   // output file status
   if(outfile == NULL)
   {
       printf("Cannot open output file!\n");
       return 1;
   }

   // writing data to sorted file
   for(j = 0; j < i; j++)
   {
       fprintf(outfile, "%s %d %s\n", list[j].name, list[j].quantity, list[j].color);
   }

   // closing both files
   fclose(infile);
   fclose(outfile);

   return 0;
}

void selection_sort(struct supply list[], int n)
{
   struct supply temp;
   int i, j, min;
   for(i = 0; i < n-1; i++)
   {
       min = i;
       for(j = i+1; j < n; j++)
       {
           int r = strcmp(list[j].name , list[min].name );
           if(r < 0)
           {
               temp = list[min];
               list[min] = list[j];
               list[j] = temp;
           }
       }
   }
}

supplyc × 1 #include <stdio.h> 2 #include<stdlib.h> 3 #include <string.h> supply structure struct supply 8 char name [ 100] c

int j,k; for(j-7.k 0:jstrlen(filename)+7: j++, k++) 51 52 53 54 7,k-0: j strlen(filename)+7:j++,k+) outNamelj]- filename[k] 5

OUTPUT:

NOTICE: We've to print the sorted file to see the output. I've used the cat command to print it.

adnanli@euler1 /Desktop$ make supply supply.c o adnanli@euler1:~/Desktop$ ./supply Enter the file name to read data: Mrs olso

Add a comment
Know the answer?
Add Answer to:
A school teacher has a list of leftover school supplies from previous semesters. The teacher woul...
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 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...

  • programming language: C++ *Include Line Documenatations* Overview For this assignment, write a program that will simulate...

    programming language: C++ *Include Line Documenatations* Overview For this assignment, write a program that will simulate a game of Roulette. Roulette is a casino game of chance where a player may choose to place bets on either a single number, the colors red or black, or whether a number is even or odd. (Note: bets may also be placed on a range of numbers, but we will not cover that situation in this program.) A winning number and color is...

  • PYTHON Text file Seriesworld attached below and it is a list of teams that won from...

    PYTHON Text file Seriesworld attached below and it is a list of teams that won from 1903-2018. First time mentioned won in 1903 and last team mentioned won in 2018. World series wasn’t played in 1904 and 1994 and the file has entries that shows it. Write a python program which will read this file and create 2 dictionaries. The first key of the dictionary should show name of the teams and each keys value that is associated is the...

  • The ACME Manufacturing Company has hired you to help automate their production assembly line. Cameras have...

    The ACME Manufacturing Company has hired you to help automate their production assembly line. Cameras have been placed above a conveyer belt to enables parts on the belt to be photographed and analyzed. You are to augment the system that has been put in place by writing C code to detect the number of parts on the belt, and the positions of each object. The process by which you will do this is called Connected Component Labeling (CCL). These positions...

  • Please help me with this question! It is due by midnight! I'm doing this in Visual...

    Please help me with this question! It is due by midnight! I'm doing this in Visual Studio 2019. But any others are fine as long as it can compile. I also wanted to see the results if it is compiled and QT comments as well. Please respond immediately! Description: For this homework we will use classes to model something in the real world. In this case a resistor. The colored bands on the top-most resistor shown in the photo below...

  • Java 2 Final Project A young entrepreneur has decided to start an online exotic car sales...

    Java 2 Final Project A young entrepreneur has decided to start an online exotic car sales company named Exotic Moves and he’s contacted you to build the company’s website.  You have been given the following requirements and will need to create a prototype with JavaFX: The company sells 5 brands of exotic cars (Aston Martin, Ferrari, Lamborghini, McLaren, and Maserati).   The website should allow a user to see the total inventory of cars or filter their view based on certain...

  • 69. THE ENDOSPORE itself is might to stain_?_ color in an ACID FAST stain. (a) HOT...

    69. THE ENDOSPORE itself is might to stain_?_ color in an ACID FAST stain. (a) HOT pink (c) purple (d) green (e) baby-blue 70. All STAINS begin with a properly prepared _?_ . (a) dye (b) slide (c) smear (d) dog (e) cat 71. Which of the following is an ENDOTOXIN found in some microbes? This is results in fever, blood vessel dilation and possibly SHOCK when it is released into the human blood stream? (a) the plasma membrane (b)...

  • Using Doubly Linked List, and Sorting methods: (In Java) (please attach your output with the answer)...

    Using Doubly Linked List, and Sorting methods: (In Java) (please attach your output with the answer) (Please answer if it is correct and working) (Have been getting many wrong and spam answers lately) Introduction: In this project, we use the same file structure of Artist and Art to extend the concepts of array and linked list and have them applied to sorting. The input files of p1arts.txt and p1artists.txt have been slightly modified. They are named p7arts.txt and p7artists.txt. Assignments:...

  • microbiology help TOT Zoo Add Page Insert Table Chart Text Shape Media Comment These questions will...

    microbiology help TOT Zoo Add Page Insert Table Chart Text Shape Media Comment These questions will serve in lieu of a lab report for Exercise 15, 16, and 17 You will find the answer to these questions in the background, procedure, results and interpretation sections of manual Exercise 15, 16, and 17, videos, Actions of Selective and Differential Media Chart, and the Principle/Theory article in homework section.) General Questions 1. What is the purpose (function) of selective media? (How does...

  • Write a French/English dictionary lookup program. Read a list of pairs of English and French words...

    Write a French/English dictionary lookup program. Read a list of pairs of English and French words from a file specified by the user. English/French words should be exact matches (don't try to find partial matches). Use the supplied EnglishFrenchDictionary.java class as your main class. Fill in the missing code in the DictionaryTable.java class to read the input file and perform the searches. Add code to the DictionaryTable read() method to: read pairs of lines (English word is on the first...

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
Active Questions
ADVERTISEMENT