In: Computer Science
4.4 Lab: Arrays of Pointers to Structures (Sort)
C++ programming
This assignment is based on Homework 3. The program will create an array of Airport structures. On the first line in the input file airports.txt there is an integer n, representing the number of lines in the input file. On each of the following n lines there is an airport code (a unique identifier) followed by the airport’s number of enplanements (commercial passenger boardings), and city served. You may assume that the data in the input file are sorted by the airport’s code as shown below:
4 BFL 100433 Bakersfield BUR 2077892 Burbank LAX 39636042 Los Angeles MRY 192136 Monterey
Read n from this input file and use it to dynamically allocate an array of structures. Then continue reading from file to put data into the dynamically allocated array.
Create an array of n pointers to Airport structures and initialize each pointer to point to the corresponding element in the array of Airport structures.
Change the insertion sort algorithm to rearrange the pointes in the array of pointers to show the array of structures in descending order by the airport’s number of enplanements.
When done sorting the pointers, display the following report:
Original Order Descending (enp) BFL Bakersfield 100433 LAX Los Angeles 39636042 BUR Burbank 2077892 BUR Burbank 2077892 LAX Los Angeles 39636042 MRY Monterey 192136 MRY Monterey 192136 BFL Bakersfield 100433
/*~*~*~*~*~*~
Pointers, Arrays, Structures, Sorting, and Dynamic Allocation of
Memory
Name:
IDE:
*/
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct Airport
{
string code;
string city;
int enp;
};
// function prototypes
Airport *readArpData(string filename, int &size);
Airport **createPtrArray(Airport *list, int size);
void insertSort(Airport **pArp, int size);
void displayReport(Airport **pArp, Airport *list, int size);
int main()
{
Airport *list; // pointer to work with a dynamically allocated
array of structs
Airport **pArp; // pointer to work with an array of pointers to
structs
int size;
string filename = "airports.txt";
// function calls
cout << "Enter input file name: ";
cin >> filename;
// call readArpData
// call createPtrArray
// call insertSort
// call displayReport
return 0;
}
/*~*~*~*~*~*~
This function does the following:
- opens the input file(with validation: exit if file not
found)
- reads size from the input file (the first number)
- dynamically allocates an array of size Airport structures
- reads data for size airports into the dynamically allocated
array
- closes the input file
- returns the pointer to the dynamically allocated array
*~*/
Airport *readArpData(string filename, int &size)
{
ifstream inputFile;
Airport *list;
/* Write your code here */
return list;
}
/*~*~*~*~*~*~
This function ...
*~*/
Airport **createPtrArray(Airport *list, int size)
{
Airport **pArp;
/* Write your code here */
return pArp;
}
/*~*~*~*~*~*~
This function ...
*~*/
void insertSort(Airport **list, int size)
{
/* Write your code here */
}
/*~*~*~*~*~*~
This function ...
*~*/
void displayReport(Airport **pArp, Airport *list, int size)
{
cout << "\nOriginal Data Descending (enp)" <<
endl;
for (int i = 0; i < size; i++){
cout << /* code */ << " "
<< setw(15) << left << /* city */
<< setw(10) << right << /* enp */ << "
";
cout << /* code */ << " "
<< setw(15) << left << /* city */
<< setw(10) << right << /* enp */ <<
endl;
}
}
/*~*~*~*~*~*~ Save the output below
*~*/
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct Airport
{
string code;
string city;
int enp;
};
// function prototypes
Airport *readArpData(string filename, int &size);
Airport **createPtrArray(Airport *list, int size);
void insertSort(Airport **pArp, int size);
void displayReport(Airport **pArp, Airport *list, int size);
int main()
{
Airport *list; // pointer to work with a dynamically
allocated array of structs
Airport **pArp; // pointer to work with an array of
pointers to structs
int size;
string filename = "airports.txt";
// function calls
cout << "Enter input file name: ";
cin >> filename;
list = readArpData(filename,size);
pArp = createPtrArray(list,size);
insertSort(pArp,size);
displayReport(pArp,list,size);
return 0;
}
/*~*~*~*~*~*~
This function does the following:
- opens the input file(with validation: exit if file not
found)
- reads size from the input file (the first number)
- dynamically allocates an array of size Airport structures
- reads data for size airports into the dynamically allocated
array
- closes the input file
- returns the pointer to the dynamically allocated array
*~*/
Airport *readArpData(string filename, int &size)
{
ifstream inputFile;
Airport *list;
inputFile.open(filename.c_str(),ios::in) ;
if(inputFile==NULL)
{
cout<<"Error Opening
File!!";
return NULL;
}
inputFile>>size;
/* Write your code here */
list = new Airport[size];
for(int i=0;i<size;i++)
{
inputFile>>list[i].code;
inputFile>>list[i].enp;
std::getline
(inputFile,list[i].city);
}
inputFile.close();
return list;
}
/*~*~*~*~*~*~
This function ...
*~*/
Airport **createPtrArray(Airport *list, int size)
{
Airport **pArp;
pArp = new Airport*[size];
for(int i=0;i<size;i++)
{
pArp[i] = &list[i];
}
return pArp;
}
/*~*~*~*~*~*~
This function ...
*~*/
void insertSort(Airport **list, int size)
{
int i, j;
Airport *key ;
for (i = 1; i < size; i++)
{
key = (list[i]);
j = i - 1;
/* Move elements of
arr[0..i-1], that are
greater than key, to one
position ahead
of their current
position */
while (j >= 0
&& list[j]->enp < key->enp)
{
list[j + 1]
= list[j];
j = j - 1;
}
list[j + 1] = key;
}
}
/*~*~*~*~*~*~
This function ...
*~*/
void displayReport(Airport **pArp, Airport *list, int size)
{
cout << "\nOriginal Data Descending (enp)" <<
endl;
for (int i = 0; i < size; i++){
cout << list[i].code<<
" "
<< setw(15) << left
<< list[i].city
<< setw(10) << right
<< list[i].enp << " ";
cout << pArp[i]->code
<< " "
<< setw(15) << left
<< pArp[i]->city
<< setw(10) << right
<< pArp[i]->enp << endl;
}
}
/*~*~*~*~*~*~ Save the output below
*~*/
//Output