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

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

  • The One Stop Fabric Shop sells 80 yards bolt of fabric for $100 each. Your job...

    The One Stop Fabric Shop sells 80 yards bolt of fabric for $100 each. Your job is to write a program that provides an invoice to a customer that will also include information on anything that might be back ordered. The program should have a function that asks for the following data: Customer information (include name, mailing address and phone number) The number of bolts ordered. The number of bolts in stock. Whether there are special shipping and handling charges....

  • This is in C. For this assignment we will write a simple database server. We will...

    This is in C. For this assignment we will write a simple database server. We will be creating a simple database of student records, so let’s describe these first. The format of a student record is as follows: typedef struct student {     char lname[ 10 ], initial, fname[ 10 ];     unsigned long SID;     float GPA; } SREC; Part One – the Server We will create a database server. The job of the server is to accept a...

  • NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions...

    NOTE: LANGUAGE IS PYTHON CS160 Computer Science Lab 17 Objectives Work with dictionaries Work with functions Work with files Overview This lab will have you store the birthdays for a group of people. This program will store the information with the focus on the date of the month and not on the individual. The point of this program is to be able to see who has a birthday on a given day or to create a table of birthdays, in...

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