Please I would like to see the solution to this question: Write a program that r
ID: 3689401 • Letter: P
Question
Please I would like to see the solution to this question:
Write a program that reads data in from a file, sorts the data, and outputs the result to both the screen and an output file. The first line of input is a whole number that indicates how big the data set is. Each line following that contains a city name, state, and population figure for that city separated from each other with at least one blank (see sample input below).
The program reads the data from the input file into three arrays, one that contains city names, one that contains state information, and one that contains population data. Note: these arrays should be kept in parallel with each other. That is, the nth element of one array should always correspond with the nth element of the other two arrays.
The arrays will need to be at least as big as the number of records in the data set. Set your maximum number of records at 50 (i.e. your array size). This means that the array size may be larger than the number of data items you store in the array (i.e., partially filled array). The program will need to keep track of the number of elements actually stored in the array, and use that number (not the array size) when sorting and displaying information.
The program sorts the data in descending order based on the population size, and then writes the result neatly on the screen.
Write a function for each of the following:
- display the elements of the three arrays as shown below.
void print (char city [][20], char state[][3], int population[], int size)
- sort the arrays in descending order based on the population size.
void selectionSort(char city [][20], char state[][3], int population[], int size)
Sample Input and Output:
Programmer: Name of the programmer (your name)
Course: COSC 246, Winter 2016
Lab#: 10
Due Date: 4-7-16
Enter an input file name: lab10.txt
The data sorted by population is:
Chicago IL 2886251
Houston TX 2009834
Philadelphia PA 1492231
Dallas TX 1211467
Detroit MI 925051
Indianapolis IN 783612
Columbus OH 725228
Austin TX 671873
Baltimore MD 638614
Milwaukee WI 590895
Boston MA 589281
Charlotte NC 580597
Washington DC 570898
Seattle WA 570426
Denver CO 560415
Portland OR 539438
Syracuse NY 460230
Toledo OH 371822
Lansing MI 275326
Burlington VT 180000
Input file: Open a text editor (e.g., TextWrangler), type in the following data as shown, and then save it as “lab10.txt” into your project folder.
20
Indianapolis IN 783612
Detroit MI 925051
Washington DC 570898
Burlington VT 180000
Houston TX 2009834
Denver CO 560415
Philadelphia PA 1492231
Boston MA 589281
Charlotte NC 580597
Columbus OH 725228
Chicago IL 2886251
Lansing MI 275326
Austin TX 671873
Baltimore MD 638614
Milwaukee WI 590895
Seattle WA 570426
Dallas TX 1211467
Portland OR 539438
Toledo OH 371822
Syracuse NY 460230
Explanation / Answer
Hello there,
Please find below code and it's output.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* This program reads data from input file and store them in arrays and sort
* them on population and write it to output file.
*
* @author dipal.prajapati
*
*/
public class Lab10 {
static BufferedReader br = null;
static BufferedWriter bw = null;
static String inputFileName = "lab10.txt";
static int size = 0;
static String outputFileName = "lab10_output.txt";
/**
* Selection sort that sorts the given arrays with respect to population of the cities.
* @param city
* @param state
* @param population
*/
public static void selectionSort(char[][] city, char[][] state, int[] population) {
for (int i = 0; i < size; i++) {
int index = i;
for (int j = i + 1; j < size; j++)
if (population[j] < population[index])
index = j;
int pop1 = population[index];
char[] city1 = city[index];
char[] state1 = state[index];
population[index] = population[i];
city[index] = city[i];
state[index] = state[i];
population[i] = pop1;
city[i] = city1;
state[i] = state1;
}
}
/**
* It prints as well as write the output in output file
*
* @param city
* @param state
* @param population
*/
public static void print(char[][] city, char[][] state, int[] population) {
try {
File file = new File(outputFileName);
// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
System.out.println("The data sorted by population is:");
for (int i = size - 1; i >= 0; i--) {
bw.write(String.valueOf(city[i]) + " " + String.valueOf(state[i]) + " " + population[i] + " ");
System.out.println(String.valueOf(city[i]) + " " + String.valueOf(state[i]) + " " + population[i]);
}
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Done");
}
/**
* Main program.
*
* @param args
*/
public static void main(String args[]) {
char[][] arrayOfCities = new char[50][20];
char[][] arrayOfStates = new char[50][3];
int[] arrayOfPopulations = new int[50];
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(inputFileName));
br.readLine(); // Ignore first line
while ((sCurrentLine = br.readLine()) != null) {
String[] strArray = sCurrentLine.split(" ");
arrayOfCities[size] = strArray[0].toCharArray();
arrayOfStates[size] = strArray[1].toCharArray();
arrayOfPopulations[size] = Integer.parseInt(strArray[2]);
size++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
selectionSort(arrayOfCities, arrayOfStates, arrayOfPopulations);
print(arrayOfCities, arrayOfStates, arrayOfPopulations);
}
}
===I/P==
20
Indianapolis IN 783612
Detroit MI 925051
Washington DC 570898
Burlington VT 180000
Houston TX 2009834
Denver CO 560415
Philadelphia PA 1492231
Boston MA 589281
Charlotte NC 580597
Columbus OH 725228
Chicago IL 2886251
Lansing MI 275326
Austin TX 671873
Baltimore MD 638614
Milwaukee WI 590895
Seattle WA 570426
Dallas TX 1211467
Portland OR 539438
Toledo OH 371822
Syracuse NY 460230
==O/P==
The data sorted by population is:
Chicago IL 2886251
Houston TX 2009834
Philadelphia PA 1492231
Dallas TX 1211467
Detroit MI 925051
Indianapolis IN 783612
Columbus OH 725228
Austin TX 671873
Baltimore MD 638614
Milwaukee WI 590895
Boston MA 589281
Charlotte NC 580597
Washington DC 570898
Seattle WA 570426
Denver CO 560415
Portland OR 539438
Syracuse NY 460230
Toledo OH 371822
Lansing MI 275326
Burlington VT 180000
Done
===lab10_output.txt===
Chicago IL 2886251
Houston TX 2009834
Philadelphia PA 1492231
Dallas TX 1211467
Detroit MI 925051
Indianapolis IN 783612
Columbus OH 725228
Austin TX 671873
Baltimore MD 638614
Milwaukee WI 590895
Boston MA 589281
Charlotte NC 580597
Washington DC 570898
Seattle WA 570426
Denver CO 560415
Portland OR 539438
Syracuse NY 460230
Toledo OH 371822
Lansing MI 275326
Burlington VT 180000
Let me know if you have any queries.
Thanks.