Write a C++ program that will take an input file of hurricane information named FloridaHurricanes.txt and sort the information by hurricane name.

QUESTION

Write a C++ program that will take an input file of hurricane information named FloridaHurricanes.txt and sort the information by hurricane name.

The input file should be located in the current directory of the project. A sample file has been given to you to test your project and it can be found in the Canvas module for this project.

The input file format is: hurricane name followed by category.  An example: Alma 2

The input file contains 38 hurricanes’ information. Read the hurricane names into a string array within the class. Read the storm category into an integer array. 

Sort the string array and integer array using either the bubblesort method or selection sort method found in this chapter of the textbook in ascending order.

Display the list of hurricanes sorted by hurricane names. (See example below)

should use appropriate private member variables and methods.

An example:

If the input contained the following:

Elena 3

Kate 2

Floyd 1

Andrew 5

The output would be:

Florida Major Hurricanes

Sorted by hurricane name:

Name           Category

Andrew             5

Elena                 3

Floyd                 1

Kate                  2

End of results

 

SUMMARY (sort the file)

A text file named ‘FloridaHurricanes’ is first read. The hurricane names present in the file are stored in a string array and the hurricane categories present in the file are stored in an integer array. These are then sorted based on ascending order of the hurricane names. (sort is done)

 

EXPLANATION 

In the program, the file named ‘FloridaHurricanes’  is first opened to read its contents. Each line of the file is read and the hurricane string is stored in an array called hurricane string and the category is stored in an integer array. By using the bubble sort technique we sort the content of both of the arrays in ascending order. The sorted output is then printed.

 

CODE sort

#include <iostream>

using namespace std;

#include <iomanip>

#include <fstream>

int main()

{

    /* To open and read file contents */

    ifstream infile;

    infile.open("FloridaHurricanes.txt");

    int cont=0;

    string hurricanes[40];

    int categories[40];

    while(infile>>hurricanes[cont])

    {

        infile>>categories[cont];

        cont++;

    }

    /* sorting using bubble sort*/

    for(int i=0;i<cont;i++)

    {

        for(int j=0;j<cont-1-i;j++)

        {

            if(hurricanes[j]>hurricanes[j+1])

            {

                string temp=hurricanes[j];

                hurricanes[j]=hurricanes[j+1];

                hurricanes[j+1]=temp;

                int temp2=categories[j];

                categories[j]=categories[j+1];

                categories[j+1]=temp2;

            }

        }

    }

    cout<<left;

    cout<<setw(12)<<"Name"<<setw(12)<<"Category"<<endl;

    for(int j=0;j<cont;j++)

    cout<<setw(12)<<hurricanes[j]<<setw(12)<<categories[j]<<endl;

}

 

OUTPUT

Name            Category

Andrew              5

Elena               3

Floyd               1

Kate                2

 

Also Read: Suppose that we have a sorted int array x with n elements in it and some room to add more elements.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *