This is a copy of the assignemnt that I\'m having the most difficult problem wit
ID: 3621135 • Letter: T
Question
This is a copy of the assignemnt that I'm having the most difficult problem with. Pleases help if you can
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:
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:
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
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:
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)
open the file and make sure that it opened correctly
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:
To test if there are records in the file, simply use the name of the input file stream as a boolean variable. For example:
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:
After all of the data has been read from the file, the file must be closed. To do this, execute the following:
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:
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.
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.
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 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.
Explanation / Answer
please rate - thanks
#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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.