Question

In: Computer Science

4.4 Lab: Arrays of Pointers to Structures (Sort) C++ programming This assignment is based on Homework...

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

*~*/


Solutions

Expert Solution

#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


Related Solutions

C programming 4. Exploring Pointers and Use of Arrays [15 pts] In a shell environment, programs...
C programming 4. Exploring Pointers and Use of Arrays [15 pts] In a shell environment, programs are often executed using command lines arguments. For example: g++ -o cm03_la01 cm03_la01.c. In C such program can be develop using a standard for which the main program has two parameters as follows: int main (int argc, char * argv[ ]) { /*program here */ } 3 Because of the equivalence between pointers and arrays, the above C code can also be written as...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using malloc or calloc. Examples: Input: n = 5, A= {33,21,2,55,4} Output: {2,4,21,33,55} b) Write a function that declares an array of 256 doubles and initializes each element in the array to have a value equal to half of the index of that element. That is, the value at index 0 should be 0.0, the value at index 1 should be 0.5, the value at...
C++ programming test 2, chapters 6,7,& 9 on Functions, Arrays, & Pointers 1. Create a one...
C++ programming test 2, chapters 6,7,& 9 on Functions, Arrays, & Pointers 1. Create a one dimensional array, Ages, which will hold 4 ages. Each age is an int.        b. Write a for loop and print, in reverse order the 4 values stored in memory assuming that the ages in the previous question have already been entered with a space between each value. Use subscript notation.                                     short cnt; c. Do the same as above, but use pointer...
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects •...
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects • Methods • Arrays of Primitive Values • Arrays of Objects • Recursion • for and if Statements • Insertion Sort 2. Use the following Guidelines • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for...
C++ Pig Latin Lab This assignment uses pointers to perform something done often in computer applications:...
C++ Pig Latin Lab This assignment uses pointers to perform something done often in computer applications: the parsing of text to find “words” (i.e., strings delineated by some delimiter). Write a program that encodes English language phrases into Pig Latin. Pig Latin is a form of coded language often used for amusement. Many variations exist in the methods used to form Pig Latin phrases. Use the following algorithm: to form a Pig Latin phrase from an English language phrase, tokenize...
PLEASE NO USE OF LINKED LIST OR ARRAYS(USE CHARACTER POINTERS INSTEAD) TO HOLD DATA STRUCTURES This...
PLEASE NO USE OF LINKED LIST OR ARRAYS(USE CHARACTER POINTERS INSTEAD) TO HOLD DATA STRUCTURES This lab, for which you may work in groups of two, will require you to use a C structure to hold a record of any kind of data, i.e., address book, library of books as with chapter 14, etc. These records will be held in dynamic memory using memory allocation (malloc) to create new memory and the function (free) to release memory created via (malloc)....
Homework Arrays and Tables In this assignment you are to create an algorithm, flowchart, and pseudocode...
Homework Arrays and Tables In this assignment you are to create an algorithm, flowchart, and pseudocode for a solution of the following problem. This solution will include the use of arrays needed to complete all parts of the logic. You have requested to develop a program that will record and process the rainfall totals of a 12 month period. You would use an array to store each months total. Once all 12 months amounts are entered then your solution need...
In this assignment you will use pointers and structures to create Single and double link list....
In this assignment you will use pointers and structures to create Single and double link list. Write a menu driven program with the menu options: 1. Insert in Linked List       1. Insert in Single linked list              1. Insert at front(head)              2. Insert at Index              3. Insert at end(tail)       2. Insert in double linked list              1. Insert at front(head)              2. Insert at Index              3. Insert at end(tail) 2. Print       1. Print linked...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on...
The assignment: C++ program or Java You need to use the following programming constructs/data structures on this assignment. 1. A structure named student which contains:   a. int ID; student id; b. last name; // as either array of char or a string c. double GPA;    2. An input file containing the information of at least 10 students. 3. An array of struct: read the student information from the input file into the array. 4. A stack: you can use the...
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and...
Write a C++ function that accepts array size and pointers to three arrays a1, a2 and a3 of type float as parameters. It then multiplies a1 and a2 and stored the result in a3. Assume that array multiplication is done by multiplying corresponding array elements, e.g. a3[i] = a1[i] * a2[i] where 0 <= i <= (array size – 1) Write a C++ main program to dynamically create three equal sized float arrays a1, a2 and a3. The size of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT