For this assignment, write a program that will place city temperature information into an array of structures and then calculate some statistics.
The structure that is used in this programs represents one city. It has fields for the city name, low temperature for the city, and high temperature for the city.
struct cityInfo
{
char cityName[25];
int lowTemp;
int highTemp;
};
For the assignment, declare one array that can hold a maximum of 30 elements (cities).
Suggested Logic for main():
NOTE: all of the functions mentioned in this logic are described below.
Fill the array by calling the buildArray() function.
Display the title "Unsorted Cities Report"
Display the unsorted array by calling the printArray() function.
Sort the array by calling the sortArray() function.
Display the title "Sorted Cities Report"
Display the sorted array by calling the printArray() function.
Calculate the average and standard deviation of the low and high temperatures by calling the calcStats function
Display the average and standard deviation of the low and high temperatures with 3 digits after the decimal point
Input
The input for this program will be read from a disk file named binary_cities.txt. The file consists of a set of cityInfo records. Each record represents one city that has been written in BINARY.
Functions to write and use
int buildArray( cityInfo cities[] )
This function will read the file of data and fill an array with that data. It takes as its argument the array of cityInfo structures. It returns the number of valid cities that were placed in the array.
Suggested logic:
Declare an input file stream and any other variables that you may need
Open the input file in BINARY mode and make sure that it opened correctly (see "buildArray notes" below)
Initialize a subscript to the beginning of the array
Get the first city record from input by using the read function (see "buildArray notes" below)
While there are records in the file
Put the city record into the cities array
Increment the subscript to the next spot in the array
Get the next city from input by using the read function
Endwhile
Close the input file
Return the number of cities that were placed in the array (think about the subscript)
buildArray notes:
The information in the file has been written in BINARY rather than regular text like the previous assignments. This means that the statement to open the file must be altered to make sure that the data is read properly. To open a file in BINARY mode and make sure that it opened correctly:
inFile.open( "binary_cities.txt", ios::binary );
if ( inFile.fail() )
{
cout << "The binary_cities.txt input file did not open";
exit(-1);
}
Since the file contains binary data rather than text, the information has to be read in a slightly different manner. Rather than reading each field individually as in previous programs, the information for each city will be read as a "chunk" of data by using the read function:
cityInfo city;
inFile.read( (char *) &city, sizeof(cityInfo) );
As in program 6, to test if there are records in the file, simply use the name of the input file stream as a boolean variable.
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.
After all of the data has been read from the file, the file must be closed.
THIS IS WHAT I HAVE SO FAR:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <ctype.h>
#include <math.h>
using namespace std;
char cityInfo;
int buildArray(cityInfo cities[]);
int main()
{
double array[30]
buildArray(cityInfo cities[])
system("pause");
}
int buildArray(cityInfo cities[])
{
inFile.open( "binary_cities.txt." );
if( inFile.fail() )
{
cout << "Input binary_cities.txt input file did not open" << endl << endl;
exit(-1)
cityInfo city;
inFile.read( (char *) &scity, sizeof(cityInfo) );
}
THE BINARY IS A TEXT IN WINDOWS NOTEPAD AND THIS IS WHATS IN IT:
Port Charles ‘| Ð Æw €ý c Springfield ‘| Ð Æw €ýíÿÿÿf Llanview ld ‘| Ð Æw €ý l Salem ew ld ‘| Ð Æw €ý. r Pine Valley ‘| Ð Æw €ý1 x Genoa City ‘| Ð Æw €ýïÿÿÿn Los Angeles ‘| Ð Æw €ý< q Oakdale les ‘| Ð Æw €ýÜÿÿÿu Bay City es ‘| Ð Æw €ýØÿÿÿy Henderson s ‘| Ð Æw €ý¾ÿÿÿr Monticello ‘| Ð Æw €ýâÿÿÿi Harmony lo ‘| Ð Æw €ý b Denver lo ‘| Ð Æw €ýãÿÿÿi New York City ‘| Ð Æw €ýñÿÿÿj Seaview Circle | Ð Æw €ý ~ Tuscany Valley | Ð Æw €ý a