For this assignment, I have to re-write the following program so that rather tha
ID: 3621573 • Letter: F
Question
For this assignment, I have to re-write the following program so that rather than using multiple arrays to hold the city temperature information, it uses a single array of structures.The programis as follows:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int buildArrays(string[],int[],int[]);
void printArrays(string[],int[],int[],int);
void sortArrays(string[],int[],int[],int);
void printHighTemp( string[], int [], int );
void printLowTemp( string[], int [], int );
void swaps(string[],int,int);
void swap(int[],int,int);
int main()
{string name[30];
int low[30],high[30],count;
count=buildArrays(name,low,high);
if(count>0)
{
cout<<" Unsorted Cities Report ";
printArrays(name,low,high,count);
sortArrays(name,low,high,count);
cout<<" Sorted Cities Report ";
printArrays(name,low,high,count);
}
system("pause");
return 0;
}
int buildArrays(string name[],int low[],int high[])
{ifstream input;
int i=0;
input.open("cities.txt"); //open file
if(input.fail()) //is it ok?
{cout<<"program aborting-file did not open please check it ";
return i;
}
input>>name[i];
while(input)
{input>>low[i]>>high[i];
i++;
input>>name[i];
}
input.close();
return i;
}
void printArrays(string name[],int low[],int high[],int count)
{int i;
cout<<"City Low Temperature High Temperature ";
cout<<"----------------------------------------------------------------- ";
for(i=0;i<count;i++)
cout<<setw(15)<<left<<name[i]<<" "<<low[i]<<" "<<high[i]<<endl;
printLowTemp(name,low,count );
printHighTemp( name,high,count );
cout<<" ";
}
void sortArrays(string name[],int low[],int high[],int count)
{int i,j;
for(i=0;i<count-1;i++)
for(j=i+1;j<count;j++)
if(name[i].compare(name[j])>0)
{swaps(name,i,j);
swap(low,i,j);
swap(high,i,j);
}
}
void swaps(string a[],int i,int j)
{string t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
void swap(int a[],int i,int j)
{int t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
void printLowTemp( string name[], int a[], int count)
{int i,index=0;
for(i=1;i<count;i++)
if(a[i]<a[index])
index=i;
cout<<name[index]<<" has the lowest temperature of "<<a[index]<<endl;
}
void printHighTemp( string name[], int a[], int count)
{int i,index=0;
for(i=1;i<count;i++)
if(a[i]>a[index])
index=i;
cout<<name[index]<<" has the highest temperature of "<<a[index]<<endl;
}
(End of program)
The structure that will be used in this programs will represent 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, you will only need to declare one array that can hold a maximum of 30 elements:
*one array of cityInfo to hold all of the city information
Suggested Logic for main():
NOTE: all of the functions mentioned in this logic are described below.
*Fill the array by calling the buildArrays() function.
*Display the title "Unsorted Cities Report"
*Display the unsorted array by calling the printArrays() function.
*Sort the array by calling the sortArrays() function.
*Display the title "Sorted Cities Report"
*Display the sorted array by calling the printArrays() 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
--------------------------------------------------------------------------------
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 buildArrays( cityInfo cities[] )
This function will read the file of data and fill the array. It takes as its argument the array of cityInfo structures. It returns the number of valid cities that were placed in the arrays.
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 "buildArrays notes" below)
*Initialize a subscript to the beginning of the array
*Get the first city record from input by using the read function (see "buildArrays 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)
buildArrays notes:
*The information in the file has been written in BINARY rather than regular text like the previous assignment. 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 data contains binary data rather than text, the information has to read in a slightly different manner. Rather than reading each field individually like in Program 7, each cities information will be read as a "chunk" of data by using the read function:
cityInfo city;
inFile.read( (char *) &city, sizeof(cityInfo) );
*As in the previous assignment, 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.
--------------------------------------------------------------------------------
void printArrays( cityInfo cities[], 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 array of cityInfo structures and the number of cities in the array.
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( cityInfo cities[], 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 array of cityInfo structures and the number of cities in the arrays.
Don't forget that since the city name is stored as a real C++ string (null-terminated character array) rather than using the string data type, the comparison of city names MUST be changed from program 7. Use the strcmp function instead of the < operator.
--------------------------------------------------------------------------------
void calcStats( cityInfo cities[], int numCities, double &averageLow, double &averageHigh, double &stdDevLow, double &stdDevHigh )
This function will calculate the average and standard deviation of the low and high temperatures.
This function takes as its arguments the array of cityInfo structures, the number of cities in the arrays, a reference to a double that will be used to pass-back the average of the low temperatures, a reference to a double that will be used to pass back the average of the high temperatures, a reference to a double to pass back the standard deviation of the low temperatures, and a reference to a double to pass back the standard deviation of the high temperatures.
See the Calculation Note at the end for the standard deviation formula.
--------------------------------------------------------------------------------
For 10 points of extra credit
Write the following functions and alter the printArrays() function to include calling statements for the functions.
void printLowTemp( cityInfo cities[], int numCities )
This function will find the lowest temperature in the cities array and display the city that had the lowest temperature along with the temperature.
This function takes as its arguments the array of cityInfo structures, and the number of cities in the arrays.
void printHighTemp( cityInfo cities[], int numCities )
This function will find the highest temperature in the cities array and display the city that had the highest temperature along with the temperature.
This function takes as its arguments the array of cityInfo structures, and the number of cities in the arrays.
Programming Notes:
Add #include <fstream> at the top of your program.
The array should be able to hold 30 elements. Use a symbolic constant to represent the maximum size of an array.
The 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 binary_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
Temperature Statistics
----------------------
Average Temperature - Low -5.500
Average Temperature - High 107.150
Standard Deviation - Low Temperatures 30.823
Standard Deviation - High Temperatures 10.241
Calculation Note
The standard deviation is calculated by implementing the following formula:
STANDARD DEVIATION = [{((S x)^2)-((S x)^2)/n)}/(n-1)]^(1/2)
where
*S( x^2 ) is the sum of the values in the array squared
*(S x)^2 is the square of the sum of the values in the array
*n is the number of values in the array
For example, if an array contains the values:
7.5 3.6 4.8 5.0
then
*S( x^2 ) is equal to
-(7.5 * 7.5) + (3.6 * 3.6) + (4.8 * 4.8) + (5.0 * 5.0)
*(S x)^2 is equal to
-(7.5 + 3.6 + 4.8 + 5.0) * (7.5 + 3.6 + 4.8 + 5.0)
*n is equal to 4
Explanation / Answer
This isn't very different from your previous assignment. The only difference is that now instead of having multiple arrays where each element in every array corresponds to a certain city and its high and low temperatures, you have an object that effectively encapsulates all the information you need (the cityInfo structure). So now, you can write a constructor for your cityInfo structure that initializes the name, low temp, and high temp, then make an array of cityInfo objects. For your "buildArray()" function, all you would need to do is go through a loop initializing each cityInfo object with its correct cityname, and low and high temperatures. Then for your print, you also go through a for loop, printing out the city name and temperatures inside of each cityInfo object. For sorting, since you sort by city name, your function would just check the city name variable of every cityInfo object and swap accordingly. The other functionality would not be changed.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.