Question

Write a C++ program that reads unknown number of schools infymation (school name, number of teachers, number of students) from a file called schools.txt and then displays a summary statistic of the information. Your program shall implement the following functions: double GetRatio (int Tn, int Sn): The function receives the number of teachers (Tn) and the number of students (Sn) on a school and it returns the ratio of the number of the students to the number of teachers (Sn/Tn). string MaxRatio (string Sname, double r): The function receives the school name (Sname) and the ration of the number of students to the number of teachers computed by GetRatio function. The function returns the school name (Sname) with the maximum ratio value. Hint: Use static variable to keep record of the maximum ratio value and another variable to keep record of the name of the school of the maximum ratio value. void Totals (int Tn, int Sn, int&Ttot, int &Stot): void function that receives number of teachers (Tn) and the number of students (Sn) on a school and it computes the total number of students and the total number of teachers for all schools through pass by reference variables Ttot (total teachers) and Stot (total students) STRatio School_e1 40 500 School 82 33 465 School 03 20 276 School 84 53 687 School_e5 30 365 School 86 33 453 School e7 55 653 School e8 10 222 School 09 43 546 School_1e 15 234 School 11 22 453 School 12 40 334 hool_e1 hool e2 hool e3 hool 64 chool es chool 66 hool 07 chool e8 hool_e9 hool 1e hool 11 hool 12 12.5 14.1 13.8 13.0 12.2 13.7 11.9 22.2 12.7 15.6 20.6 8.3 1 with maximun Student/Teacher Ration: Schoo1 es Total Number of Teachers: 394 Total Number of Students: 5188 Press any key to continue . . . 100 % Sample Output Sample Input
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include "stdafx.h"

#include <fstream>

#include <iostream>

#include <sstream>

#include <string>

#include <vector>

#include <algorithm>

using namespace std;

//function to get ratio

double GetRatio(int Tn,int Sn){

return (double)Sn/Tn;

}

//function to calculate maximum ratio

string MaxRatio(string Sname,double r){

//use of static variable as required

static double maxRatio=-1;

static string schoolName;

if(maxRatio<r){

schoolName=Sname;

maxRatio=r;

}

return schoolName;

}

//function to calculate total for student and teacher

void Totals(int Tn,int Sn,int &Ttot,int &Stot){

Ttot=Ttot+Tn;

Stot=Stot+Sn;

}

int main()

{

//reading file

//give path here

ifstream in("school.txt");

ofstream myfile;

//creating vector to store data in the form of matrix

vector<vector<string>> matrix;

int Ttot=0,Stot=0;

int col=0,count=0,row=0,i,j;

string schoolName;

double ratio;

if (in) {

string line;

while (getline(in, line)) {

stringstream sep(line);

string field;

row=0;

matrix.push_back(vector<string>());

while (getline(sep, field, ' ')) {

//adding data to vector as string

matrix.back().push_back(field);

row++;

}

col=col+1;

}

}

cout<<"School Name\t\tSTRatio\n";

cout<<"***********\t\t*******\n";

for (int i = 0; i < matrix.size(); ++i)

{

//stoi is used to convert readed string to integer

//function is called to get ratio

ratio=GetRatio(stoi(matrix[i][1]),stoi(matrix[i][2]));

//printing school and ratio

cout<<matrix[i][0]<<"\t\t"<<ratio<<endl;

//getting school name with maximum ratio

schoolName=MaxRatio(matrix[i][0],ratio);

//adding to totals

Totals(stoi(matrix[i][1]),stoi(matrix[i][2]),Ttot,Stot);

}

//printing remainin Info

cout<<"School with maximum student/teacher Ratio: "<<schoolName<<endl;

cout<<"Total Number of Teachers: "<<Ttot<<endl;

cout<<"Total Number of Students: "<<Stot<<endl;

system("Pause");

return 0;

}

STRatio 3xxa 12.5 14.0909 13.8 12.9623 12.1667 13.7273 11.8727 22.2 12.6977 15.6 20.5909 8.35 School Name School_e1 School 02 School 03 School 04 School 05 School 06 School 07 School 08 School 09 School_10 School 11 School_12 School with maximum student/teacher Ratio: School 08 Total Number of Teachers: 394 Total Number of Students 5188 Press any key to cont inue . .

STRatio 3xxa 12.5 14.0909 13.8 12.9623 12.1667 13.7273 11.8727 22.2 12.6977 15.6 20.5909 8.35 School Name School_e1 School 02 School 03 School 04 School 05 School 06 School 07 School 08 School 09 School_10 School 11 School_12 School with maximum student/teacher Ratio: School 08 Total Number of Teachers: 394 Total Number of Students 5188 Press any key to cont inue . .

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that reads unknown number of schools infymation (school name, number of teachers,...
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
  • Programming in C: Write a program that will help an elementary school student learn multiplication. Use...

    Programming in C: Write a program that will help an elementary school student learn multiplication. Use the random number generator to produce two positive integers between 1 and 12 inclusive. It should then type a question such as:             How much is 6 times 7? The student then types the answer. Your program checks the students’ answer. If it is correct, print a message such as Very good!   If the answer is wrong, print a message such as No. Please...

  • C++ Question Question 3 [43] The Question: You are required to write a program for an...

    C++ Question Question 3 [43] The Question: You are required to write a program for an Online Tutoring Centre to determine the weekly rate depending on the number of sessions the students attend and then to calculate total revenue for the center. The Online Tutoring Centre charges varying weekly rates depending on the on grade of the student and number of sessions per week the student attends as shown in Table 1 below. Grade 8 9 Session 1 75 80...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

  • write a C++program to analyze a small subset of the data that has been collected. See...

    write a C++program to analyze a small subset of the data that has been collected. See file universities.txt .Use precisely seven parallel arrays: one for name of university, one for state, one for city, one for yearly tuition, one for enrollment, one for average freshman retention, and one for the percent of students who graduate with in six years. Note that the percentage of student accepted is not stored.An output file is opened in main() and remains open until the...

  • Please!!! need help asap!!!! write a C++program to analyze a small subset of the data that...

    Please!!! need help asap!!!! write a C++program to analyze a small subset of the data that has been collected. See file universities.txt .Use precisely seven parallel arrays: one for name of university, one for state, one for city, one for yearly tuition, one for enrollment, one for average freshman retention, and one for the percent of students who graduate with in six years. Note that the percentage of student accepted is not stored.An output file is opened in main() and...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • I need help programming this program in C. 1.) Write a program that reads a message,...

    I need help programming this program in C. 1.) Write a program that reads a message, then checks whether it's a palindrome (the letters in the message are the same from left to right as from right to left), example is shown below: Enter a message: He lived as a devil, eh? Palindrome Enter a message: Madam, I am Adam. Not a Palindrome 2.) Ignore all characters that aren't letters. Use integer variables to keep track of positions in the...

  • Write in JAVA Get Familiar with the Problem Carefully read the program description and look at...

    Write in JAVA Get Familiar with the Problem Carefully read the program description and look at the data file to gain an understanding of what is to be done. Make sure you are clear on what is to be calculated and how. That is, study the file and program description and ponder! Think! The Storm Class Type Now concentrate on the Storm class that we define to hold the summary information for one storm. First, look at the definition of...

  • == Programming Assignment == For this assignment you will write a program that reads in the...

    == Programming Assignment == For this assignment you will write a program that reads in the family data for the Martian colonies and stores that data in an accessible database. This database allows for a user to look up a family and its immediate friends. The program should store the data in a hashtable to ensure quick access time. The objective of this assignment is to learn how to implement and use a hashtable. Since this is the objective, you...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

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