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...

  • A Gumball machine has a number of different colors available: There are a random number of...

    A Gumball machine has a number of different colors available: There are a random number of yellow, blue, white, green, black, purple, silver, cyan and magenta gumballs and one red gumball. With a pocket full of quarters, you have set your heart on getting that single red gumball. So, write a program that will simulate buying gumballs until you get the red one. (Using Python) To start with, the number of gumballs for each color are randomly chosen as follows....

  • Find the number a Show your work. ssistentes for "cach situation using a list, free diagram...

    Find the number a Show your work. ssistentes for "cach situation using a list, free diagram or table. " You olla her cute s mes.llow many Many wies are possible? 2. Carmen has a deck of cards wwwbe wutcomes are possible! Wheted10She picks one card and flips a coin. How many 3. Kendall has three jars of poll' balls. O orange and purple golf ball. The third Ball from cach jar Iwany outcomes arep & Oriejar has a white and...

  • IN C++!! Program 2: BACK AWAY FROM THE TV! If you sat close enough to an...

    IN C++!! Program 2: BACK AWAY FROM THE TV! If you sat close enough to an old TV, you could see individual (R)ed, (G)reen and (B)lue (or RGB) “phosphors” that could represent just about any color you could imagine (using “additive color model”). When these three color components are at their maximum values, the resulting color is white from a distance (which is amazing – look at your screen with a magnifying glass!). When all are off, the color results...

  • 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...

  • Spanish Vocabulary Helper For this assignment, we are going to work with adding and removing data...

    Spanish Vocabulary Helper For this assignment, we are going to work with adding and removing data from arrays, linear search, and File I/O. In addition, you will learn to work with parallel arrays This program will assist an English-speaking user to build their vocabulary in Spanish This program will read a file containing a list of words in English and another containing the translation of those words in Spanish. The words and corresponding translations should to be stored in two...

  • 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...

  • is a nucleus the sole source of DNA in a eukaryotic cell? WORKSHEET: EXERCISE 10 NAME:...

    is a nucleus the sole source of DNA in a eukaryotic cell? WORKSHEET: EXERCISE 10 NAME: DATE: SECTION Twenty microliters (20 ㎕) of each sample was loaded into your gel. Convert 2OuL to milliliters (mL). I. 2. Describe the charge on the DNA molecule and specify which component of the molecule is responsible for contributing to that charge. Why do some DNA samples travel further in the gel than others? 3. 4. Once electrophoresed, how are the DNA bands visualized?...

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