I need help with C++ Write a program that reads data in from a file, sorts the d
ID: 3688039 • Letter: I
Question
I need help with C++
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 nthelement 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
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
#define MAX 50
int main () {
char city_name[MAX][20], state_name[MAX][3];
int population[MAX];
string line, mini_line, m;
char a[20];
int count=0, i=0;
int lenght_file=0;
ifstream myfile ("lab10.txt");
if (myfile.is_open()){
while ( getline (myfile,line) ){
cout << line << ' ';
if (count == 0)
lenght_file = atoi(line.c_str());
else{
for (i=0; i<50; i++)
if (line[i] != 32)
strcpy(city_name[count][i],line[i]);
}
count++;
}
myfile.close();
}
else cout << "Unable to open file";
cout << city_name[0];
cout << lenght_file;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.