/*
Name:
Email:
Lab:
*/
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
const int MAX = 100; //maximum of 100 cities
int ReadInput(ifstream& fin, string name[], double total_poulation[], double average_age[], double number_households[], double average_households[]);
void output(string name[], double total_poulation[], double average_age[], double average_households[], int count_records, ofstream& out);
double ComputeAverage(double number_households[], int count_records);
int GetCityWithHighestAverageAge(double average_age[], int count_records);
void SelectionSort(string name[], double total_poulation[], double average_age[], double number_households[], double average_households[], int count_records);
void Swap(double& x, double& y);
void FindCity(string name[], double total_poulation[], double average_age[], double number_households[], double average_households[], int count_records, ofstream& out);
int main()
{
//five arrays
string name[MAX]; //one for name of city
double total_poulation[MAX]; //one for total population
double average_age[MAX]; //one for average age of residents
double number_households[MAX]; //one for number of households
double average_households[MAX]; //one for the average number of people per household.
int count_records = 0;
//open file
ifstream fin;
fin.open("cityCensus.txt");
//if file not opened
if (!fin)
{
cout << "ERROR: File not found\n\n";
return 1;
}
//open output file
ofstream fout;
fout.open("output.txt");
//call function to read values from file
count_records = ReadInput(fin, name, total_poulation, average_age, number_households, average_households);
//Call the output function to output to a file all data in the original order.
output(name, total_poulation, average_age, average_households, count_records, fout);
double average_population = ComputeAverage(number_households, count_records);
cout << "Average Population = " << average_population << endl;
int index = GetCityWithHighestAverageAge(average_age, count_records);
cout << "City with highest average age " << name[index] << endl;
//call selection sort function to sort the array
SelectionSort(name, total_poulation, average_age, number_households, average_households, count_records);
//Write the sorted array to the file.
fout << endl << endl;
output(name, total_poulation, average_age, average_households, count_records, fout);
//call findcity function
FindCity(name, total_poulation, average_age, number_households, average_households, count_records, fout);
fout.close();
cout << "All output written to file output.txt\n\n";
return 0;
}
/*
Description: function to input the name of each city, the total population, the average age of residents, and the number of households.
Pre:
fin - Input File pointer
name - city name
total_poulation - total poulation of city
average_age - average age
number_households - number of households
average_households - average of households
Post - number of records read from file
*/
int ReadInput(ifstream& fin, string name[], double total_poulation[], double average_age[], double number_households[], double average_households[])
{
int i = 0;
while (getline(fin, name[i]))
{
fin >> total_poulation[i];
fin >> average_age[i];
fin >> number_households[i];
//average number of people per household is not read in but it is calculated by dividing the total population by the number of households.
average_households[i] = total_poulation[i] / number_households[i];
i++;
fin.ignore();
}
return i;
}
/*Description:function to print output to file in original order
Pre:
name - city name
total_poulation - total poulation of city
average_age - average age
average_households - average of households
count_records - number of records
out - Output File Pointer
Post - None
*/
void output(string name[], double total_poulation[], double average_age[], double average_households[], int count_records, ofstream &out)
{
out << setw(20) << left << "City" << setw(15) << "Population" << setw(15) << "Average Age" << setw(15) << "Average number" << endl;
out << setw(65) << right << " People / household" << endl;
for (int i = 0; i < count_records; i++)
{
out << setw(20) << left << name[i] << setw(15) << right << setprecision(0) << fixed << total_poulation[i] << setw(10) << setprecision(1) << fixed << average_age[i] << setw(10) << average_households[i] << endl;
}
}
/*Description: function to compute and return the average population for all cities.
Pre:
number_households - number of households
count_records - number of records
Post- average of number_households
*/
double ComputeAverage(double number_households[], int count_records)
{
double sum = 0;
for (int i = 0; i < count_records; i++)
{
sum = sum + number_households[i];
}
return sum / count_records;
}
/*
Description: function to return the subscript of the city with the highest average age.
Pre:
average_age - average age
count_records - number of records
Post:
index of city with highest average age
*/
int GetCityWithHighestAverageAge(double average_age[], int count_records)
{
double highest = average_age[0];
int index = 0;
for (int i = 1; i < count_records; i++)
{
if (average_age[i] > highest)
{
highest = average_age[i];
index = i;
}
}
return index;
}
/*Description: function to swap teo double values, used in selection sort
Pre:
x - reference value to be swapped
y - reference value to be swapped
Post: None
*/
void Swap(double& x, double& y)
{
double temp = x;
x = y;
y = temp;
}
/*Descritpion: selection sort to sort cities by population in descending order.
Pre:
name - city name
total_poulation - total poulation of city
average_age - average age
number_households - number of households
average_households - average of households
count_records - number of records
Post: None
*/
void SelectionSort(string name[], double total_poulation[], double average_age[], double number_households[], double average_households[], int count_records)
{
int i, j, max_index;
for (i = 0; i < count_records - 1; i++)
{
max_index = i;
for (j = i + 1; j < count_records; j++)
if (total_poulation[j] > total_poulation[max_index])
max_index = j;
Swap(total_poulation[max_index], total_poulation[i]);
Swap(average_age[max_index], average_age[i]);
Swap(number_households[max_index], number_households[i]);
Swap(average_households[max_index], average_households[i]);
string temp = name[max_index];
name[max_index] = name[i];
name[i] = temp;
}
}
/*Description: function to prompt the user for the name of a city. Output to the file from this function the population,
average age, and average number of people per household or output message “No such cityin the list”.
Pre:
name - city name
total_poulation - total poulation of city
average_age - average age
number_households - number of households
average_households - average of households
count_records - number of records
out - Output File Pointer
POST: None
*/
void FindCity(string name[], double total_poulation[], double average_age[], double number_households[], double average_households[], int count_records, ofstream &out)
{
string city;
cout << "Enter City name: ";
getline(cin, city);
bool found = false;
for (int i = 0; i < count_records; i++)
{
if (city == name[i])
{
out << endl << "City Name: " << name[i] << endl;
out << "Population: " << total_poulation[i] << endl;
out << "Average Age: " << average_age[i] << endl;
out << "Average Number of People/Household: " << average_households[i] << endl;
out << endl;
found = true;
}
}
if (found == false)
{
cout << "No such city in the list\n";
}
}
ACTON
7993 45.2 2729
AERIAL ACRES
3074 27 1056
BREA
3613 38.6 1154
CITY RANCH
50798 37 15963
COMMERCE
45903 28.4 10727
CRYSTALAIRE
1259 52.4 569
DEL SUR
70918 34.4 20964
ELIZABETH LAKE
2932 41.7 1079
FIRESTONE PARK
57110 26.6 12971
FRAZIER PARK
5077 44 2080
FULLERTON
51767 36 15849
GORMAN
1699 40.9 623
HI VISTA
72046 28.3 20672
HOLLYWOOD
45151 38.3 21929
JUNIPER HILLS
2138 43.3 816
LAKE LOS ANGELES
7285 30.9 1982
LOS ANGELES
51223 25.5 11731
MARICOPA
4176 38.4 647
PALMDALE
38158 28.4 9690
PEAR BLOSSOM
388 44.5 103
PINON HILLS
6220 41.8 2198
PIRU
2031 29.3 522
ROSAMOND
18910 32.4 6469
SIMI VALLEY
54366 39 18650
WRIGHTWOOD
4894 44.1 1998