Could someone please help me with this lab? It\'s in C++ by the way. /*Lab 1 Wri
ID: 3822967 • Letter: C
Question
Could someone please help me with this lab? It's in C++ by the way.
/*Lab 1
Write the function() getData, outputCities(), reverse(), find(), delete(), insertion sort()
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#define MAX 1000
using namespace std;
//Function Prototypes
int main()
{
//Variable declarations
string city[MAX]; //holds the name, XX for each city
double costIndex[MAX]; //cost of living index for that city
int num; //number of cities in study
ifstream inFile;
ofstream outFile;
//Open file
inFile.open("cities.txt");
//Tests if file exists
if (inFile.fail())
{
//error to console
cout << "Error: File not found!" << endl;
system("pause");
exit(1);
}
outFile.open("output.txt");
//Function to input namd of city and cost index; returns number of cities in study
num = getData(inFile, city, costIndex);
inFile.close();
//Function to reverse the elements in the original arrays
//Cities will be ordered Z - A in descending order
//Function to output name of city, cost index formatted in two tabular columns
printAll(outFile, city, costIndex, num);
//Ask the user for a name of a city, call a function to find that city, and from main
// output the cost of living index of the phrase "No Such City" (linear search is okay)
//Ask the user for a name of a city and delete that city and its cost of index
//Using insertion sort sort the cities from high to low based on cost of living index
//Function to output name of city, cost index formatted in two tabular columns
printAll(outFile, city, costIndex, num);
outFile.close();
system("pause");
return 0;
}
Explanation / Answer
Here is the code for you:
/*Lab 1
Write the function() getData, outputCities(), reverse(), find(), delete(), insertion sort()
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#define MAX 1000
using namespace std;
//Function Prototypes
int getData(ifstream&, string[], double[]);
void reverse(string[], double[], int);
void printAll(ofstream&, string[], double[], int);
int find(string[], int, string);
void deleteCityAndData(string[], double[], int&, string);
void insertionSort(string[], double[], int);
int main()
{
//Variable declarations
string city[MAX]; //holds the name, XX for each city
double costIndex[MAX]; //cost of living index for that city
int num; //number of cities in study
ifstream inFile;
ofstream outFile;
//Open file
inFile.open("cities.txt");
//Tests if file exists
if (inFile.fail())
{
//error to console
cout << "Error: File not found!" << endl;
system("pause");
exit(1);
}
outFile.open("output.txt");
//Function to input namd of city and cost index; returns number of cities in study
num = getData(inFile, city, costIndex);
inFile.close();
//Function to reverse the elements in the original arrays
//Cities will be ordered Z - A in descending order
reverse(city, costIndex, num);
//Function to output name of city, cost index formatted in two tabular columns
printAll(outFile, city, costIndex, num);
//Ask the user for a name of a city, call a function to find that city, and from main
// output the cost of living index or the phrase "No Such City" (linear search is okay)
cout << "Enter the name of the city to search for: ";
string searchCity;
cin >> searchCity;
int cityIndex = find(city, num, searchCity);
if(cityIndex == -1)
cout << "No Such City." << endl;
else
cout << "The cost of living in this city is: " << fixed << setprecision(2) << costIndex[cityIndex] << endl;
//Ask the user for a name of a city and delete that city and its cost of index
cout << "Enter the name of the city to delete: ";
string deleteCity;
cin >> deleteCity;
deleteCityAndData(city, costIndex, num, deleteCity);
//Using insertion sort sort the cities from high to low based on cost of living index
insertionSort(city, costIndex, num);
//Function to output name of city, cost index formatted in two tabular columns
printAll(outFile, city, costIndex, num);
outFile.close();
system("pause");
return 0;
}
void insertionSort(string city[], double costIndex[], int num)
{
int i, j;
for(i = 1; i <= num-1; i++)
{
double tempD = costIndex[i];
string tempC = city[i];
for(j = i -1 ; j >= 0; j--)
{
if(costIndex[j] > tempD)
{
costIndex[j+1] = costIndex[j];
city[j+1] = city[j];
}
else
break;
}
costIndex[j+1] = tempD;
city[j+1] = tempC;
}
}
int find(string city[], int num, string searchCity)
{
for(int i = 0; i < num; i++)
if(city[i].compare(searchCity) == 0)
return i;
return -1;
}
void deleteCityAndData(string city[], double costIndex[], int &num, string deleteCity)
{
int index = find(city, num, deleteCity);
if(index != -1)
{
for(int i = index; i < num-1; i++)
{
city[i] = city[i+1];
costIndex[i] = costIndex[i+1];
}
num--;
}
}
void printAll(ofstream &fout, string city[], double costIndex[], int count)
{
for(int i = 0; i < count; i++)
{
cout << left << setw(30) << city[i];
cout << fixed << setprecision(2) << costIndex[i] << endl;
}
}
int getData(ifstream &fin, string city[], double costIndex[])
{
int count = 0;
while(!fin.eof())
{
fin >> city[count] >> costIndex[count];
count++;
}
return count;
}
void reverse(string city[], double costIndex[], int count)
{
for(int i = 0; i < count/2; i++)
{
string tempC = city[i];
city[i] = city[count-i-1];
city[count-i-1] = tempC;
double tempD = costIndex[i];
costIndex[i] = costIndex[count-i-1];
costIndex[count-i-1] = tempD;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.