Question

The Canadian government wants to keep track of how many masks and protective gowns are available...

The Canadian government wants to keep track of how many masks and protective gowns are available in each hospital. There is a text file called F: equipment.txt that contains this data. Each line in the file uses this format:

hospital name;number of masks;number of gowns

For example, the first four lines in the file look like this:

burnaby;495;483

Vancouver;872;791

Victoria;361;361

Calgary;476;502

Note that the hospital names are not in any particular order in the file.

Each hospital should have the same number of masks and gowns. Write a C program that reads all the data from the file and displays each hospital with the number of items that are needed to make the two numbers the same. When your program is running the screen should look something like this:

Items to be supplied to each hospital:

Vancouver 81 gowns

Calgary 26 masks

Burnaby 12 gowns

Victoria no items needed

The hospital names must be displayed in alphabetical order.

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

If you will feel any problem with the solution, then feel free to ask it in comments.

Happy Learning

1. You ask for The hospital names must be displayed in alphabetical order.

2. But your output not follows it.

Finally, I choose to print in alphabetical order.

#include<stdio.h>

#include<string.h>

#include<ctype.h>

// Hospital type structure

typedef struct Hospital

{

char name[20];

int masks;

int gowns;

int required;

char required_item_name[20];

}Hospital;

// sort hosp array in increasing order with respect to name

void sort(Hospital *hosp, int no_of_hosp)

{

int i, j;

Hospital temp;

for(i=0; i<no_of_hosp-1; ++i)

{

for(j=0; j<no_of_hosp-i-1; ++j)

{

if(strcmp(hosp[j].name, hosp[j+1].name)>0)

{

temp = hosp[j];

hosp[j] = hosp[j+1];

hosp[j+1] = temp;

}

}

}

}

// calculate mask required or gown required

void calculate(Hospital *hosp, int no_of_hosp)

{

int i;

char masks[] = "masks";

char gowns[] = "gowns";

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

{

// if mask > gown, then gowns required

if(hosp[i].masks>hosp[i].gowns)

{

hosp[i].required = hosp[i].masks - hosp[i].gowns;

strcpy(hosp[i].required_item_name, gowns);

}

// if mask < gown, then masks required

else if(hosp[i].masks<hosp[i].gowns)

{

hosp[i].required = hosp[i].gowns - hosp[i].masks;

strcpy(hosp[i].required_item_name, masks);

}

// if mask == gown, then nothing required

else

{

hosp[i].required = 0;

}

}

}

// display hosp array in given format

void display(Hospital *hosp, int no_of_hosp)

{

int i;

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

{

if(hosp[i].required == 0)

{

printf("%s no items needed\n", hosp[i].name);

}

else

{

printf("%s %d %s\n", hosp[i].name, hosp[i].required, hosp[i].required_item_name);

}

}

}

void main()

{

// array to stor hospitals data read from file

Hospital hospitals[1000];

// number of hospitals present

int num_of_hospitals = 0;

char temp;

FILE *fid;

fid = fopen("equipment.txt", "r");

while(!feof(fid))

{

// read hospital name

fscanf(fid, "%[^;]s", hospitals[num_of_hospitals].name);

// make first letter of name uppercase

hospitals[num_of_hospitals].name[0] = toupper( hospitals[num_of_hospitals].name[0]);

// read ';'

fscanf(fid, "%c", &temp);

// read number of masks

fscanf(fid, "%d", &hospitals[num_of_hospitals].masks);

// read ';'

fscanf(fid, "%c", &temp);

// read number of gowns

fscanf(fid, "%d", &hospitals[num_of_hospitals].gowns);

// read '\n'

fscanf(fid, "%c", &temp);

num_of_hospitals++;

}

// sort first

sort(hospitals, num_of_hospitals);

// calculate

calculate(hospitals, num_of_hospitals);

// display

display(hospitals, num_of_hospitals);

}

Activities Visual Studio Code Jun 24 11:00 enı HomeworkLib40.c - Chegg - Visual Studio Code File Edit Selection View Go Run TerminaActivities Visual Studio Code Jun 24 11:00 eni HomeworkLib40.c - Chegg - Visual Studio Code File Edit Selection View Go Run TerminaActivities Visual Studio Code Jun 24 11:01 eni HomeworkLib40.c - Chegg - Visual Studio Code File Edit Selection View Go Run TerminaActivities Visual Studio Code Jun 24 11:01 eni HomeworkLib40.c - Chegg - Visual Studio Code File Edit Selection View Go Run TerminaActivities Visual Studio Code Jun 24 11:01 en equipment.txt - Chegg - Visual Studio Code File Edit Selection View Go Run Term

Add a comment
Know the answer?
Add Answer to:
The Canadian government wants to keep track of how many masks and protective gowns are available...
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
  • in C PROGRAM but the techniques required are common for many real-world data situations. Input Data...

    in C PROGRAM but the techniques required are common for many real-world data situations. Input Data The following data is available in the file WHLdata-txt attached to the assignment on connex. You can open the file in a text editor and then copy and paste the data into the console window when testing your program. This data was captured on Feb. 4. We will test your program using up to date data. There are 22 teams and that will be...

  • in python in the simplest way I have a file “a.txt” under the directory /temp/, the...

    in python in the simplest way I have a file “a.txt” under the directory /temp/, the file contains some company names, the format is like that: IBM APPLE cisco Microsoft …… Each line contains only one company name. Among these company names, some are capitalized such as IBM, APPLE, some are not, such as cisco. Write a program to read this file a.txt and convert all names to uppercase and save them to the file name “b.txt” in the same...

  • Write a program in C++: Using classes, design an online address book to keep track of the names f...

    Write a program in C++: Using classes, design an online address book to keep track of the names first and last, addresses, phone numbers, and dates of birth. The menu driven program should perform the following operations: Load the data into the address book from a file Write the data in the address book to a file Search for a person by last name or phone number (one function to do both) Add a new entry to the address book...

  • Lab #2 Address Book Unsorted List (C++) Using classes, design an online address book to keep...

    Lab #2 Address Book Unsorted List (C++) Using classes, design an online address book to keep track of the names (first and last), addresses, phone numbers, and dates of birth. The menu driven program should perform the following operations: Load the data into the address book from a file Write the data in the address book to a file Search for a person by last name or phone number (one function to do both) Add a new entry to the...

  • Hi I how do i use a vector to search a array for a string that...

    Hi I how do i use a vector to search a array for a string that user has enter into the console. here is some of the instructions for the programing I am trying to work on The purpose of this program is to write a class whose job is to read up to 10,000 names into a string array. The class sorts the names and then the user can ask the class to find full names containing a name...

  • Order up:: Write a program that will be used to keep track of orders placed at...

    Order up:: Write a program that will be used to keep track of orders placed at a donut shop. There are two types of items that can be ordered: coffee or donuts. Your program will have a class for each order type, and an abstract superclass. Coffee orders are constructed with the following information: quantity (int) - how many coffees are being ordered size (String) - the size of the coffees (all coffees in the order have the same size)...

  • (JAVA) Using classes and inheritance, design an online address book to keep track of the names

    (JAVA)Using classes and inheritance, design an online address book to keep track of the names, addresses, phone numbers and birthdays of family members, friends and business associates. Your program should be able to handle a maximum of 500 entries.Define the following classes: Class Address to store a street name, city, state and zip code. Class Date to store the day, month and year. Class Person to store a person's last name and first name. Class ExtPerson that extends the class...

  • Objective: Text File I/0 and Regular Expressions Note that both classes below should handle all exceptions...

    Objective: Text File I/0 and Regular Expressions Note that both classes below should handle all exceptions that might be thrown within them. 1. Create a class that does the following: a. Reads the name of a file to create as the first command line argument. (Overwrite any file with the same name). b. Reads an integer value as the second command line argument. C. The program should generate as many random numbers as are specified in the second command line...

  • Make a Java, program with two processes, a producer and a consumer. The producer process consists...

    Make a Java, program with two processes, a producer and a consumer. The producer process consists of a loop that writes the loop count (a value from 0 to 4) into a variable that it shares with the consumer process (this variable is to be initialized to 100). On each pass through the loop, before the producer writes into the shared variable, it does a random wait of from one to three seconds (compute a new random wait value on...

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