Hey guys, I'm supposed to write a program that takes census information from a file provided by the user and sort it by population or area.
I think I did most of the functions correctly, but I'm having trouble starting the function that actually sorts the data.
I was given the prototype for the function, but I have no idea where to start. This is my first time working with C-strings and it's causing me a lot of trouble!
Here was the prototype I was given:
Code:
Sort the arrays by population or area. Notice that you have to sort all three arrays together, using either integer array to control the process. (Important: You only need one function for sorting by either population or area. A simple trick with argument passing will control which one controls the sorting.) This means that you have to modify the sorting function that you've used in the past so that it has parameters for three arrays and treats one array as a special "key array" during the sorting process. The function prototype could look like this:
void sortPopulationUnits(char names[][MAX_NAME_LENGTH+1],
int keyArray[],
int otherIntArray[],
unsigned int nUnits);
I really don't understand what they mean by treating one array as a "key array", and whats with the "unsigned int nUnits" in the prototype?
If anyone could give me any hints at how to go about implenting this function, I would greatly appreciate it!
Code:
// This program helps people look at population information by sorting
// data in given files.
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
const int MAX_NAME_LENGTH = 24;
//Function Prototypes.
int readIntArray(int arr[], int maxSize, char fname[]);
int readStringArray(char arr[][MAX_NAME_LENGTH+1], int maxSize, char
fname[]);
void sortPopulationUnits(char names[][MAX_NAME_LENGTH+1], int keyArray[],
int otherIntArray[], unsigned int nUnits);
int mainMenu();
// Main function.
int main()
{
int current_array_size = 0;
const int maxSize = 200;
int population[maxSize];
int area[maxSize];
const int MAX_NAME_LENGTH = 24;
char fname[MAX_NAME_LENGTH + 1];
char names[maxSize][MAX_NAME_LENGTH + 1];
cout << "Enter name of population unit name file: ";
cin >> fname;
current_array_size = readStringArray(names, maxSize, fname);
cout << "Enter name of population file: ";
cin >> fname;
readIntArray(population, maxSize, fname);
cout << "Enter name of area file: ";
cin >> fname;
readIntArray(area, maxSize, fname);
//read in contents of population.
//Main Menu.
int selection = mainMenu();
while ((selection > 0) && (selection < 5))
{
if (selection == 1){
cout << "1";
selection = mainMenu();
//sort function...
}
else if (selection == 2){
cout << "2";
selection = mainMenu();
//sort function...
}
else if (selection == 3){
cout << "3";
selection = mainMenu();
//write file function...
}
else // if selection == 4
return 0;
//exit program.
}
}
//Function definition.
int mainMenu()
{
int selection = 0;
do{
cout << "Main menu: " << endl;
cout << "(1) Sort by population" << endl;
cout << "(2) Sort by area" << endl;
cout << "(3) Write to output file" << endl;
cout << "(4) Quit" << endl;
cout << "Your choice: ";
cin >> selection;
}
while((selection < 1) || (selection > 4));
return selection;
}
int readIntArray(int arr[], int maxSize, char fname[])
{
int i=0;
fstream fin.open(fname);
if(fin.fail()){
cout << Error this file does not exist." << endl;
end(1);
}
for(i; i < maxSize; i++){
fin >> arr[i];
}
fin.close();
return i;
}
int readStringArray(char arr[][MAX_NAME_LENGTH+1], int maxSize, char fname[])
{
int c=0;
int r=0;
fstream fin.open(fname);
if(fin.fail()){
cout << Error this file does not exist." << endl;
end(1);
}
for(c=0; c < maxSize; c++)
{
for(r=0; r < MAX_NAME_LENGTH+1; r++)
fin >> arr[c][r];
arr[c][r+1] = '\0';
return c+1;
}
}
void sortPopulationUnits(char names[][MAX_NAME_LENGTH+1], int keyArray[], int otherIntArray[], unsigned int nUnits)
{
}
Thanks in advance!