Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

* Programming Assignment Details You have been asked to write a program to help

ID: 3858389 • Letter: #

Question

* Programming Assignment Details

You have been asked to write a program to help a friend choose among schools in Ohio. Your friend has input the data in a file with specific format to help you.

The input file contains the names, locations, current enrollments, and current tuitions of schools in Ohio, respectively. In particular, all information for one school is listed together. For example, in the input file, “OH-in.dat”, the first four lines indicate the name of the school (i.e., AntiochCollege), location of the school (i.e., YellowSprings), current enrollment of the school (i.e., 330), and current tuition of the school (i.e., 27800).   Then, the same information for other schools is followed in the file. (Note that you must use “OH-in.dat” file as the input file for the program.) Also note that there are no spaces in the names of the schools, so you can safely use the >> operator to process input from the file using string type variables. The input file is in the lib directory under the class directory. Use cp command to copy the input file from the lib directory to your work directory.  

           

-   How to copy OH-in.dat to your work directory.

o Go to your work directory (i.e., Prog02).

o Type pwd command to get the full path to the work directory. This path will be used in the cp command later.

(Full path should be similar to “/home/xx/yourID/cs202jl/Prog02”.) o Copy the full path to the work directory.

o Then, go to the lib directory under the class directory.

o Type ls –lt command to list the files in the lib directory.

(The input file should be listed if you are in the correct directory.) o Use cp command to copy the input file.

§ cp OH-in.dat fullPathToWorkDirecory

-   The number of schools should be passed to the main( ) function via command line arguments.

o Use int main (int argc, char *argv[ ]) in your program.

o argc (argument count) is the number of strings pointed to by argv. It is 1 plus the number of arguments.

o argv (argument vector) holds the arguments passed to the main function.

o For example, when executing your program using ./runprog02 48 argc will be 2 and argv[0] will be ./runprog02 argv[1] will be 48

o The number of schools should be no more than 48.

(I may test your program with 48 and a smaller number.)

-   The school information should be read and stored in an array of structure.

o struct name: schoolInfo

o struct members: schoolName, city, enrollment, tuition

Complete the following tasks in the order listed.

1. Using a function called, find_school( ), list the schools and the tuition of the schools in Cincinnati with tuition below 20000.

Before calling find_school( ) function, call another function called count_school( ) function to count how many school in Cincinnati with tuition below 20000. This function should accept the array of struct (for the school info), a string variable which was initialized with “Cincinnati”, and an int variable which holds the total number of schools. count_school( ) function should return an int variable which holds the count of the schools that meet the criteria. Use the int variable value to dynamically-declare an array of struct for the array of struct returned by find_school( ) function.

The find_school( ) function should accept the array of struct (for the school info), a string variable which was initialized with “Cincinnati”, an int variable which holds the total number of schools, and an int variable which holds the count of the schools that meet the criteria determined from the count_school( ) function. The find_school( ) function should return an array of struct for the school info that meets the criteria (name the struct as returnedSchools)

Using the returned array of struct, in the main function, display the number of schools that meet these criteria and the school names with their tuition.

2. Using a function called, avg_tuition( ), calculate and display the average tuition of schools with more than 10000 enrolled students.

The function should accept the array of struct (for the school info) and an int variable which holds the total number of schools.

All calculation and displaying the result should be done in this function for this task.

3. Using a function called, find_expensive_schools( ), give the name and tuition of the three most expensive schools. You can assume that the top three schools have different tuitions than all others (there are no "ties").

The function should accept the array of struct (for the school info) and an int variable which holds the total number of schools.

The function should return an array of struct for the school info that meets the criteria (name the struct as expensiveSchools). (Note that you know the size of this array of struct.)

Using the returned array of struct, in the main function, display the school names with their tuition.

Explanation / Answer

Given below is the code for the question. Tested it with a dummy test file. Please test with the input file specified by the question. Please don't forget to rate the answer if it helped. Thank you.

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <cctype>

#include <iomanip>

using namespace std;

typedef struct

{

string schoolName;

string city;

int enrollment;

int tuition;

}schoolinfo;

schoolinfo* find_school(schoolinfo schools[], int total, string location, int matched);

int count_school(schoolinfo schools[], int total, string location);

void avg_tuition(schoolinfo schools[], int total);

schoolinfo* find_expensive_schools(schoolinfo schools[], int total);

int main(int argc, char *argv[])

{

  

if(argc != 2)

{

cout << "Expected number of schools as argument " << endl;

exit(1);

}

  

int size = atoi(argv[1]);

  

//open input file

char filename[30] = "OH-in.dat";

ifstream infile(filename);

schoolinfo schools[50];

  

if(!infile.is_open())

{

cout << "Input file " << filename << " could not be opened " << endl;

exit(1);

}

  

//load the file contents

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

infile >> schools[i].schoolName >> schools[i].city >> schools[i].enrollment >> schools[i].tuition;

  

  

infile.close();

  

//find schools in Cincinnati with tuition below 20000

  

string location = "Cincinnati";

int matched = count_school(schools, size, location);

schoolinfo *returnedSchools = find_school(schools, size, location, matched);

  

cout << "Schools matching location = " << location << " and tuition below 20000 are " << endl;

cout << "School name Location Enrollment Tuition" << endl;

for(int i = 0 ; i < matched; i++)

cout << returnedSchools[i].schoolName << " "

<< returnedSchools[i].city << " "

<< returnedSchools[i].enrollment << " "

<< returnedSchools[i].tuition << endl;

  

  

//display average tuition of schools with enrollment more than 10000

cout << endl;

avg_tuition(schools, size);

  

  

//get the top 3 expensive schools

cout << endl;

schoolinfo *expensiveSchools = find_expensive_schools(schools, size);

cout << "The top 3 expensive schools are - " << endl;

cout << "School name Location Enrollment Tuition" << endl;

for(int i = 0 ; i < 3; i++)

cout << expensiveSchools[i].schoolName << " "

<< expensiveSchools[i].city << " "

<< expensiveSchools[i].enrollment << " "

<< expensiveSchools[i].tuition << endl;

  

cout << endl;

  

//free the memory at end

delete [] returnedSchools;

delete [] expensiveSchools;

return 0;

  

}

int count_school(schoolinfo schools[], int total, string location)

{

int count = 0;

for(int i = 0; i < total; i ++)

{

//schools in the specified location with tuitiion below 20000

if(schools[i].city == location && schools[i].tuition < 20000)

count++;

}

  

return count;

}

schoolinfo* find_school(schoolinfo schools[], int total, string location, int matched)

{

schoolinfo *retschools = new schoolinfo[matched];

for(int i = 0, j = 0; i < total; i++)

{

if(schools[i].city == location && schools[i].tuition < 20000)

retschools[j++] = schools[i];

}

  

return retschools;

  

  

}

void avg_tuition(schoolinfo schools[], int total)

{

double avg = 0, n = 0;

for(int i = 0; i < total; i++)

{

if(schools[i].enrollment > 10000)

{

avg += schools[i].tuition;

n++;

}

}

  

avg /= n;

  

cout << "There were " << n << " schools with more than 10000 enrollments." << endl;

cout << "The average tuition fees of these schools is " << avg << endl;

}

schoolinfo* find_expensive_schools(schoolinfo schools[], int total)

{

int first = 0, second = 0, third = 0; //indices of first, 2nd and 3rd expensive schools

//assume 0th school is the most expensive

  

//compare the rest of them

for(int i = 1; i < total; i++)

{

if(schools[i].tuition > schools[first].tuition)

{

third = second;

second = first;

first = i;

}

else if(schools[i].tuition > schools[second].tuition)

{

third = second;

second = i;

}

else if(schools[i].tuition > schools[third].tuition)

third = i;

}

  

//now allocate memory and retrive the records indicated by first, second and third

schoolinfo *expSchools = new schoolinfo[3];

expSchools[0] = schools[first];

expSchools[1] = schools[second];

expSchools[2] = schools[third];

  

return expSchools;

  

}

input file : OH-in.dat

TurpinHighSchool
Cincinnati
18000
12000
AlexanderHighSchool
Albany
15000
14000
GenevaHighSchool
Geneva
11000
24000
BridgetownMiddleSchool
Cincinnati
8000
10000

output

Schools matching location = Cincinnati and tuition below 20000 are
School name        Location        Enrollment    Tuition
TurpinHighSchool   Cincinnati           18000       12000
BridgetownMiddleSchool   Cincinnati           8000       10000

There were 3 schools with more than 10000 enrollments.
The average tuition fees of these schools is 16666.7

The top 3 expensive schools are -
School name        Location        Enrollment    Tuition
GenevaHighSchool   Geneva           11000       24000
AlexanderHighSchool   Albany           15000       14000
TurpinHighSchool   Cincinnati           18000       12000