Write Program in C: pain_allergy.txt file: Acetaminophen 310 83 183 141 98 193 9
ID: 3600526 • Letter: W
Question
Write Program in C:
pain_allergy.txt file:
Acetaminophen 310 83 183 141 98 193 92
Mortrin 275 46 82 103 108 95 121
Ibuprofen 873 31 134 98 103 210 45
Aspirin 633 87 100 121 99 176 97
Aleve 177 23 53 99 75 35 86
Zyrtec 675 111 43 88 109 45 32
Benadryl 477 201 123 98 65 43 50
Claritin 349 112 123 98 93 43 65
Allegra 783 182 239 122 83 92 76
Original selection_sort() function that needs to be modified (instructions on modifications provided in the picture above):
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);
}
Consider some data gathered in a file for general pain medicine and allergy medicine for a local drugstore in a small town for a six month period Name Acetaminophen Mortrin InStock Jan Feb Mar AprMay Jun 310 83 275 46 183 141 98 193 92 82 103 108 95 121 The numbers represents the number of units in stock at the drugstore, and units sold for the six months starting January. The file has the following format Acetaminophen 310 83 183 141 98 193 92 Write a program that can read in the data from a file and sort the medicine by total units sold for the six months. The sorted data should be written the same file name as the input file name with added extension of.srt. For example, if the original file name is pain_allergy.txt, the output file name is then pain allergy.txt.srt. Define a structure medicine to store the name (string), unitslnStock (integer), and six month's unitsSold as an array of int, and total units sold (int). Assume the name is no more than 100 characters. Assume the name is one word name Build an array of medicine structures. Assume that there are no more than 100 medicines Modify the selection_sort function to sort an array of medicines. The medicines should be sorted by total units sold. The function should have the following prototype 1. 2. 3. void selection sort (struct medicine meds[], int n) 4. The output file should include the total units sold as the following InStock Jan Feb 53 43 123 82 123 134 100 183 239 Total 371 428 534 Name Mar Apr Jun 23 112 201 35 45 43 95 Aleve177 2 109 93 108 65 103 32 65 121 50 45 yrtec 675 Claritin 349 Mortrin 275 Benadryl 477 Ibuprofen 873 Aspirin 633 Acetaminophen 310 Allegra 783 98 103 580 621 680 790 794 6 7 98 121 141 122 210 176 193 92 31 83 98 182Explanation / Answer
Given below is the code and output. Output will be written to output file.
#include <stdio.h>
#include <string.h>
struct medicine
{
char name[100];
int unitsInStock;
int unitsSold[6]; //data for 6 months
int totalSold;
};
int readFile(char *filename, struct medicine meds[]); //returns the number of records read
void saveToFile(char *filename, struct medicine meds[], int n);
void selection_sort(struct medicine meds[], int n);
int main()
{
char inFileName[50];
char outFilename[50];
int n;
struct medicine meds[100];
printf("Enter input filename: ");
scanf("%s" ,inFileName);
//make the output filename
strcpy(outFilename, inFileName);
strcat(outFilename, ".srt");
n = readFile(inFileName, meds);
selection_sort(meds, n);
saveToFile(outFilename, meds, n);
printf("Output written to file %s ", outFilename);
}
int readFile(char *filename, struct medicine meds[]) //returns the number of records read
{
FILE *fp = fopen(filename, "r");
int count = 0;
int i;
if(fp == NULL)
{
printf("Error opening input file %s ", filename);
return 0;
}
while(fscanf(fp, "%s", meds[count].name) == 1)
{
fscanf(fp, "%d", &meds[count].unitsInStock);
meds[count].totalSold = 0;
for(i = 0; i < 6; i++) //get data for 6 months
{
fscanf(fp, "%d", &meds[count].unitsSold[i]);
meds[count].totalSold += meds[count].unitsSold[i]; //calculate total
}
count++;
}
fclose(fp);
return count;
}
void saveToFile(char *filename, struct medicine meds[], int n)
{
FILE *fp = fopen(filename, "w");
int i, j;
if(fp == NULL)
{
printf("Error opening output file %s ", filename);
return;
}
fprintf(fp, "%5s %30s %10s %6s %6s %6s %6s %6s %6s %10s ",
"#", "Name", "InStock", "Jan", "Feb", "Mar","Apr", "May", "Jun", "Total");
for(i = 0; i < n; i++)
{
fprintf(fp, "%5d %30s %10d", (i+1), meds[i].name, meds[i].unitsInStock);
for(j = 0; j < 6; j++)
fprintf(fp, " %6d", meds[i].unitsSold[j]);
fprintf(fp, " %10d ", meds[i].totalSold);
}
fclose(fp);
}
void selection_sort(struct medicine meds[], int n)
{
{
int i, largest = 0;
struct medicine temp;
if (n <= 1)
return;
for (i = 1; i < n; i++)
if (meds[i].totalSold > meds[largest].totalSold)
largest = i;
if (largest < n - 1) {
temp = meds[n-1];
meds[n-1] = meds[largest];
meds[largest] = temp;
}
selection_sort(meds, n - 1);
}
}
input file: pain_allergy.txt
Acetaminophen 310 83 183 141 98 193 92
Mortrin 275 46 82 103 108 95 121
Ibuprofen 873 31 134 98 103 210 45
Aspirin 633 87 100 121 99 176 97
Aleve 177 23 53 99 75 35 86
Zyrtec 675 111 43 88 109 45 32
Benadryl 477 201 123 98 65 43 50
Claritin 349 112 123 98 93 43 65
Allegra 783 182 239 122 83 92 76
output
Enter input filename: pain_allergy.txt
Output written to file pain_allergy.txt.srt
output file: pain_allergy.txt.srt
# Name InStock Jan Feb Mar Apr May Jun Total
1 Aleve 177 23 53 99 75 35 86 371
2 Zyrtec 675 111 43 88 109 45 32 428
3 Claritin 349 112 123 98 93 43 65 534
4 Mortrin 275 46 82 103 108 95 121 555
5 Benadryl 477 201 123 98 65 43 50 580
6 Ibuprofen 873 31 134 98 103 210 45 621
7 Aspirin 633 87 100 121 99 176 97 680
8 Acetaminophen 310 83 183 141 98 193 92 790
9 Allegra 783 182 239 122 83 92 76 794
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.