Arrays and functions...

I was assigned this program, and I can't even get started on it I am so confused. I need to take info from a text file entitles cities.txt then display it and sort it using arrays..Here were the instructions

Overview

For this assignment, you are to write a program that will process a data set of cities and temperatures. The information in the file will be needed for later processing, so it will be stored in a set of arrays that will be displayed, sorted, and displayed (again).

For the assignment, you will need to declare four arrays, each of which will hold a maximum of 30 elements:

one array of strings to hold the city names
one array of integers to hold the low temperatures for each of the cities
one array of integers to hold the high temperatures for each of the cities
The three arrays will be parallel arrays. This means that the information for one city can be found in the same "spot" in each array. This also means that any change that is applied to one array must also be applied to the other two in order to maintain data integrity.

Suggested Logic for main():

NOTE: all of the functions mentioned in this logic are described below.

Fill the four arrays by calling the buildArrays() function.

Display the title "Unsorted Cities Report"

Display the four arrays by calling the printArrays() function.

Sort the four arrays by calling the sortArrays() function.

Display the title "Sorted Cities Report"

Display the sorted arrays by calling the printArrays() function.

Input

The input for this program will be read from a disk file named cities.txt. The file consists of a set of city records. Each record represents one city and is made up of three values that are contained on three lines of the file: the first is the city name, the second is the low temperature for the city, and the third is the high temperature for the city. The file resembles the following:

Freeport ----> The first city name
-12 ----> The first city's low temperature
103 ----> The first city's high temperature
DeKalb ----> The second city name
2 ----> The second city's low temperature
98 ----> The second city's high temperature
NOTE: You may assume that if there is a city name, then there will be a low temperature and a high temperature.

Functions to write and use

int buildArrays( string cityNames[], int lowTemps[], int highTemps[] )

This function will read the file of data and fill the three arrays. It takes as its arguments the array of strings and two arrays of integers. It returns the number of valid cities that were placed in the arrays.

Suggested logic:

Declare an input file stream (see "buildArrays notes" below) and any other variables that you may need

Open the input file and make sure that it opened correctly (see "buildArrays notes" below)

Initialize a subscript to the beginning of the array

Get the first city from input (see "buildArrays notes" below)

While there are records in the file (see "buildArrays notes" below)

Put the city into the cityNames array

Get a low temperature from input
Put the low temperature into the lowTemps array

Get a high temperature from input
Put the high temperature into the highTemps array

Increment the subscript to the next spot in the array

Get the next city from input
Endwhile

Close the input file (see "buildArrays notes" below)

Return the number of cities that were placed in the arrays (think about the subscript)
buildArrays notes:

The data is being read from an input file. Before the file can be processed, you must:

declare an input file stream (see "Programming Note 1" below)

ifstream inFile;
open the file and make sure that it opened correctly

inFile.open( "cities.txt" );

if ( inFile.fail() )
{
cout << "The cities.txt input file did not open";
exit(-1);
}
The above code will open the file and then make sure that the file did indeed open correctly.

To read from a file, use the name of the ifstream in place of cin. For example:

int num;

inFile >> num;

To test if there are records in the file, simply use the name of the input file stream as a boolean variable. For example:

while ( inFile )
As long as there are records in the file, the name of the input file stream name will resolve to true. Once all of the records have been read from the file, the input file stream name will resolve to false.

Once a value has been read from a file, it can be put into the appropriate array element:

lowTemps[i] = num;
After all of the data has been read from the file, the file must be closed. To do this, execute the following:

inFile.close();
void printArrays( string cityNames[], int lowTemps[], int highTemps[], int numCities )

This function will display the information for the cities. For each city, display the city name, low temperature, and the high temperature.

This function takes as its arguments the three arrays and the number of cities in the arrays.

Use the following as a basic format for the output:

City Low Temperature High Temperature
------------------------------------------------------
Freeport 3 99
Chicago -19 102
Troy 12 108
Centerville 46 114
Ozark 49 120
void sortArrays( string cityNames[], int lowTemps[], int highTemps[], int numCities )

This function will sort the arrays in ASCENDING order based on the city name. Use the selection sort algorithm presented in lecture.

This function takes as its arguments the three arrays and the number of cities in the arrays.

It's important to note that the three arrays are parallel arrays, meaning that elements in each array that have the same subscript correspond. Therefore, every time the algorithm swaps two elements in the cityNames array, it must also swap the corresponding elements in the lowTemps and highTemps arrays.

For 10 points of extra credit

Write the following functions and alter the printArrays() function to include calling statements for the functions.

void printLowTemp( string cityNames[], int lowTemps[], int numCities )

This function will find the lowest temperature in the lowTemps array and display the city that had the lowest temperature along with the temperature.

This function takes as its arguments the array of strings, the array of low temperatures, and the number of cities in the arrays.

void printHighTemp( string cityNames[], int highTemps[], int numCities )

This function will find the highest temperature in the highTemps array and display the city that had the highest temperature along with the temperature.

This function takes as its arguments the array of strings, the array of high temperatures, and the number of cities in the arrays.

Programming Notes:

Add #include <fstream> at the top of your program.

Each array should be able to hold 30 elements. Use a symbolic constant to represent the maximum size of an array.

Each array has the capability to hold 30 elements, however, that does not mean that they will all be used. This is the reason that the number of elements in the array is being passed to the sort and print functions. This value is the return value from buildArrays.

Copy the input file to your hard disk and write your program so that it reads the data from the current directory (ie. don't specify a file path).

Hand in a copy of your source code using Blackboard.

Output

Here is the output for this assignment using the cities.txt file from above. It includes the extra credit output.

Unsorted Cities Report

City Low Temperature High Temperature
------------------------------------------------------
Freeport 3 99
Chicago -19 102
Troy 12 108
Centerville 46 114
Ozark 49 120
Millsboro -17 110
Boca -45 94
Congerville -36 117
Alton -40 121
Basin -66 114
Bloomfield -30 105
Tipton 2 98
Birmingham -6 104
Pelham 10 111
Burbank 27 126
Port_Charles 4 97
South_Pasadena 23 117
Hawley_Lake -40 88
Pahala 12 100
Rockford 1 98

Basin has the lowest temperature of -66

Burbank has the highest temperature of 126




Sorted Cities Report

City Low Temperature High Temperature
------------------------------------------------------
Alton -40 121
Basin -66 114
Birmingham -6 104
Bloomfield -30 105
Boca -45 94
Burbank 27 126
Centerville 46 114
Chicago -19 102
Congerville -36 117
Freeport 3 99
Hawley_Lake -40 88
Millsboro -17 110
Ozark 49 120
Pahala 12 100
Pelham 10 111
Port_Charles 4 97
Rockford 1 98
South_Pasadena 23 117
Tipton 2 98
Troy 12 108

Basin has the lowest temperature of -66

Burbank has the highest temperature of 126

Here is the cities.txt file -

Freeport
3
99
Chicago
-19
102
Troy
12
108
Centerville
46
114
Ozark
49
120
Millsboro
-17
110
Boca
-45
94
Congerville
-36
117
Alton
-40
121
Basin
-66
114
Bloomfield
-30
105
Tipton
2
98
Birmingham
-6
104
Pelham
10
111
Burbank
27
126
Port_Charles
4
97
South_Pasadena
23
117
Hawley_Lake
-40
88
Pahala
12
100
Rockford
1
98

Any help would help!!!

Also...willing to pay $5 PayPal for anyone who does it all lol

Thanks!!
Topic archived. No new replies allowed.