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

QuickSort C++ Program Help Input file link: https://www.dropbox.com/s/49azukky67

ID: 668375 • Letter: Q

Question

QuickSort C++ Program Help

Input file link: https://www.dropbox.com/s/49azukky67vgb4c/pop_density.txt?dl=0

Final output of the program should look like this:

QuickSort program and clock clicks Program Information: You are consulting for a small restaurant chain which wants to expand its locations nationwide. The company executives would like to start theirexpansion by placing locations in the most densely populated areas first. To assist them, you have been asked to produce a list of Census Tracts ordered by population density. You have been given a list of the approximately 74,000 Census Tracts in the United States. The name, area (in square kilometers), total population, and population density (in people per square kilometer) are given to you foreach tract. Input File Format Each record of the input file is on a separate line. Each line contains (in this order) a name (string), an area in square kilometers (double), a population count (int), and a population density (double). A pipe (I) is used to separate the different attributes of each record. Below are three example records from the input file Census Tract 201, Autauga County, Alabama 19.84473419420788118081183.651479494869 Census Tract 202, Autauga County, Alabama | 3.34583234555866123551703.860730836106 Census Tract 203, Autauga County, Alabama 15.35750339330735130571570.60159846447

Explanation / Answer

comments added:

--------------------------------

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
//compare method
static int compare( const void *pa, const void *pb ) {
string (*a)[3] = pa;
string (*b)[3] = pb;
if ( a[0][3] < b[0][3] ) return -1;
if ( a[0][3] > b[0][3] ) return +1;
return 0;
}
int main()
{
std::ifstream readfile("data.txt");//reding numbers from text file
std::string eachline;
int count=0,temp=0;
string data[10][10];
while (std::getline(readfile, eachline))
{
std::istringstream iss(eachline);
int n;
string name,area,populationCount,density;
temp=0;
while (iss >> name >> area >> populationCount >> density) //reading line by line
{
//data storing into arrays
data[count][temp] = name;
temp++;
data[count][temp] = area;
temp++;
data[count][temp] = populationCount;
temp++;
data[count][temp] = density;
}
count++;
}
qsort( data, count, sizeof data[0], compare);
//outputting to file
ofstream out_data("pop_density_sorted.txt");
for(int i=0;i<u;i++){
for(int j=0;j<4;j++){
out_data<<a[i][j];
}
}
//printing sorted data to console
for(int i=0;i<count;i++){
for(int j=0;j<4;j++){
cout<<data[i][j]<<" ";
}
cout<<" ";
}
return 0;
}