In: Computer Science
Given a data file murders.dat containing the number of murders in Hampton roads cities write program code to accomplish the following: 1. Read the data from the murders.dat file and store into parallel arrays or an array of struct 2. Print the total number of murders in the Hampton roads cities 3. Print all of the data read 4. Print the name of the city with the most murders 5. Print the name of the city with the least murders 6. Print the names of all cities with 10 or more murders 7. Print cities sorted by number of murders (lowest to highest)
C++
i just need help with printing the cities sorted by number of murders
/* So as asked above you need helps with only the part 7, I have used buuble sort to sort the structure array*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct detail
{
string city;
int n;
};
int
main ()
{
string city;
int n, i = 0, j = 0;
ifstream myfile1 ("murders.dat");
while (!myfile1.eof ())
{
myfile1 >> city >> n;
i++;
}
myfile1.close ();
struct detail s[i];
ifstream myfile ("murders.dat");
if (myfile.is_open ())
{
while (!myfile.eof ())
{
myfile >> city >> n;
s[j].city = city;
s[j].n = n;
j++;
}
for (int k = 0; k < i - 1; k++)
{
for (int i1 = k + 1; i1 < i; i1++)
{
if (s[k].n > s[i1].n)
{
struct detail temp = s[k];
s[k] = s[i1];
s[i1] = temp;
}
}
}
for (int k = 0; k < i; k++)
cout << s[k].city << endl;
myfile.close ();
}
else
cout << "Unable to open file";
return 0;
}