Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program Using Arrays, Sorting, Filestreams, and Functional Decomposition. IN

ID: 3571185 • Letter: C

Question

C++ Program Using Arrays, Sorting, Filestreams, and Functional Decomposition.
INPUT FILE DESCRIPTION

-Assume you are given an input file that contains the daily high and low temperature readings for several months.
-For each month the file will contain,
a. the name of the month (string) - the name will be in a random mixture of upper and lower case letters
b. the number of temperature readings for the month (int) - the maximum value of this number will be the number of days in the month
c. high followed by the low temperature reading (int) for each day

sample input for a month:

    jUlY 5   101 89   105 93   100 85   102 81   105 85

PROGRAM DESCRIPTION
Create a C++ program that will
1. prompt the user for the name of the input file and read it
2. use an input filestream variable to open the file for reading
3. all output should be displayed to the screen
4. read and process the data for each month
5. display the name of the month - with the first letter capitalized and the remaining letters in lower case along with a descriptive label
6. display the number of temperature readings given with label
7. determine and display the highest high and lowest low temperature readings (as ints) for the month
8. determine and display the average high and average low temperatures (with 3 digits to the right of the decimal) for the month
9. determine and display the median high and low temperatures (will require sorting the temperatures - use bubblesort)
10. median should be displayed as an integer if there are an odd number of data sets
11. median should be displayed with 1 digit to the right of the decimal if there are an even number of data sets.
12. determine and display the standard deviation of the high and low temperatures (with 3 digits to the right of the decimal) for the month

OUTPUT REQUIREMENTS (See sample output below.)

-All numeric values should be appropriately labelled.
-All averages and standard deviations should be displayed with 3 digits to the right of the decimal.
-The median should be displayed as an integer if there are an odd number of data sets and with 1 digit to the right of the decimal if there are an even number of data sets.
-The names of months must be displayed with the first letter capitalized and the remaining letters in lower case.
-Left justify labels, right justify numbers.
-There should be at least one blank line between the output for each month.

FUNCTIONS MUST BE USED IN THE PROGRAM. AT LEAST 6 FUNCTIONS BESIDES MAIN MUST BE USED.The functions must be meaningful.

Assumptions about input: (you do not have to test for these conditions)

the data file will exist, will not be empty

the data set for each month will consist of

a string (name of month)

an integer (number of pairs of temperatures) (integer will be > 0)

the indicated number of pairs of high and low temperatures (all int) (high followed by low)

each input value will be separated by whitespace (blanks and/or linefeeds)

the last line in the data file will be terminated with a linefeed (' ')

Program must be designed to interactively prompt the user for the name of an input file and use a filestream variable to represent the file.

All output will be written to the screen.

Include header files for all library functions used.

Input file can only be read one time.

Sample terminal session:
[keys]$ more data4five
jUlY 5   101 78   105 77   104 82   108 83   98 68
sePteMber 6 103 80 103 78 99 73 93 74 92 68 95 68
jAnuaRy 10
30 15 31 14 29 20 26 10 32 20
33 23 34 31 34 22 22 -6 -1 -10
[keys]$ g++ assign05.cpp
[keys]$ ./a.out
What is the name of the input file?
data4five

July Temperature Data: 5 Readings Taken
Maximum high:            108    Minimum low:              68
Average high:        103.200    Average low:          77.600
Median high:             104    Median low:               78
Std dev of highs:      3.834    Std dev of lows:       5.941

September Temperature Data: 6 Readings Taken
Maximum high:            103    Minimum low:              68
Average high:         97.500    Average low:          73.500
Median high:            97.0    Median low:             73.5
Std dev of highs:      4.889    Std dev of lows:       4.970

January Temperature Data: 10 Readings Taken
Maximum high:             34    Minimum low:             -10
Average high:         27.000    Average low:          13.900
Median high:            30.5    Median low:             17.5
Std dev of highs:     10.530    Std dev of lows:      12.905

**DO NOT USE ALGORITHM OR VECTORS TO SOLVE THE PROBLEM.

Here is what bubblesort is if you don't know it, I have to use it in the program:

void bubblesort(int list[ ],int count)

// Sort an array of integers into descending order.
// Parameters:

//     list: array of integers to be sorted

//     count: (integer) number of values in the array

// Value passed back: sorted list
{
   int temp;   //place holder when values are interchanged
   for (int i=0; i < count-1; i++)
       for (int j=0; j < count-(i+1); j++)
         if (list[j] < list[j+1])
         {
            temp = list[j];
            list[j] = list[j+1];
            list[j+1] = temp;
         }
}

Here is some of the code that has part of the requirements, I need help sorting it, as well as finding the standard deviations.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
using namespace std;
int readTemperatures(ifstream &fin, string month[], int nReadings[], int maxTemp[][10], int minTemp[][10])
{
int count = 0;
while(!fin.eof())
{
fin>>month[count]>>nReadings[count];
for(int i = 0; i < nReadings[count]; i++)
fin>>maxTemp[count][i]>>minTemp[count][i];
count++;
}
return count;
}
int maxTemperature(int array[], int n)
{
int high = array[0];
for(int i = 0; i < n; i++)
if(array[i] > high)
high = array[i];
return high;
}
int minTemperature(int array[], int n)
{
int low = array[0];
for(int i = 0; i < n; i++)
if(array[i] > low)
low = array[i];
return low;
}
double avgTemperature(int array[], int n)
{
double sum = 0;
for(int i = 0; i < n; i++)
sum += array[i];
return sum / n;
}
int main()
{
ifstream fin;
//1. interactively prompt the user for the name of the input file and read it
string fileName;
cout<<"Enter the name of the file: ";
cin>>fileName;
//2. use an input filestream variable to open the file for reading   
fin.open(fileName.c_str());
//3. all output should be displayed to the screen
//4. read and process the data for each month
string month[12];
int numOfReadings[12], maxTemperatures[12][10], minTemperatures[12][10];
int numOfMonths = readTemperatures(fin, month, numOfReadings, maxTemperatures, minTemperatures);
for(int i = 0; i < numOfMonths; i++)
{
//5. display the name of the month - with the first letter capitalized and the remaining letters in lower case along with a descriptive label
month[i][0] = toupper(month[i].at(0));
//transform(month[i].begin()+1, month[i].end()+1, month[i].begin()+1, ::tolower);
//6. display the number of temperature readings given with label   
cout<<month[i]<<" Temperature Data: "<<numOfReadings[i]<<" Readings Taken"<<endl;
//7. determine and display the highest high and lowest low temperature readings (as ints) for the month
cout<<"Maximum high: "<<maxTemperature(maxTemperatures[i], numOfReadings[i])<<" Minimum low: "<<minTemperature(minTemperatures[i], numOfReadings[i])<<endl;
//8. determine and display the average high and average low temperatures (with 3 digits to the right of the decimal) for the month   
cout<<"Average high: "<<fixed<<setprecision(3)<<avgTemperature(maxTemperatures[i], numOfReadings[i])<<" Average low: "<<fixed<<setprecision(3)<<avgTemperature(minTemperatures[i], numOf
Readings[i])<<endl;
//9. determine and display the median high and low temperatures (will require sorting the temperatures - use bubblesort)   
//10. median should be displayed as an integer if there are an odd number of data sets   
//11. median should be displayed with 1 digit to the right of the decimal if there are an even number of data sets.
//12. determine and display the standard deviation of the high and low temperatures (with 3 digits to the right of the decimal) for the month
}

}

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <cmath> //added for power and square root functions
using namespace std;

int readTemperatures(ifstream &fin, string month[], int nReadings[], int maxTemp[][10], int minTemp[][10])
{
int count = 0;
while(!fin.eof())
{
fin>>month[count]>>nReadings[count];
for(int i = 0; i < nReadings[count]; i++)
fin>>maxTemp[count][i]>>minTemp[count][i];
count++;
}
return count;
}

//bubblesort merged code.
void bubblesort(int list[ ],int count)
// Sort an array of integers into descending order.
// Parameters:
// list: array of integers to be sorted
// count: (integer) number of values in the array
// Value passed back: sorted list
{
int temp; //place holder when values are interchanged
for (int i=0; i < count-1; i++)
for (int j=0; j < count-(i+1); j++)
if (list[j] < list[j+1])
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}

//Median temperature after sorting
double medTemperature(int array[], int n){
   double med=0;
   bubblesort(array,n);
   if ((n%2)==0)
       med=(array[n/2]+array[(n/2)-1])/2;
   else
       med=array[n/2];
   return med;
}

int maxTemperature(int array[], int n)
{
int high = array[0];
for(int i = 0; i < n; i++)
if(array[i] > high)
high = array[i];
return high;
}
int minTemperature(int array[], int n)
{
int low = array[0];
for(int i = 0; i < n; i++)
if(array[i] > low)
low = array[i];
return low;
}
double avgTemperature(int array[], int n)
{
double sum = 0;
for(int i = 0; i < n; i++)
sum += array[i];
return sum / n;
}
double stdTemperature(int array[], int n){
   double stdDev=0;
   double avg=avgTemperature(array,n);
   for(int i = 0; i < n; i++)
       stdDev+=pow((array[i]-avg),2); // sum of the square of temp[i]-mean(avg)
   stdDev=sqrt(stdDev/n);           // stddev = sqrt of square/ number of observation;
   return stdDev;
}

int main()
{
ifstream fin;
//1. interactively prompt the user for the name of the input file and read it
string fileName;
cout<<"Enter the name of the file: ";
cin>>fileName;
//2. use an input filestream variable to open the file for reading   
fin.open(fileName.c_str());
//3. all output should be displayed to the screen
//4. read and process the data for each month
string month[12];
int numOfReadings[12], maxTemperatures[12][10], minTemperatures[12][10];
int numOfMonths = readTemperatures(fin, month, numOfReadings, maxTemperatures, minTemperatures);
for(int i = 0; i < numOfMonths; i++)
{
//5. display the name of the month - with the first letter capitalized and the remaining letters in lower case along with a descriptive label
month[i][0] = toupper(month[i].at(0));
//transform(month[i].begin()+1, month[i].end()+1, month[i].begin()+1, ::tolower);
//6. display the number of temperature readings given with label   
cout<<month[i]<<" Temperature Data: "<<numOfReadings[i]<<" Readings Taken"<<endl;
//7. determine and display the highest high and lowest low temperature readings (as ints) for the month
cout<<"Maximum high: "<<maxTemperature(maxTemperatures[i], numOfReadings[i])<<" Minimum low: "<<minTemperature(minTemperatures[i], numOfReadings[i])<<endl;
//8. determine and display the average high and average low temperatures (with 3 digits to the right of the decimal) for the month   
cout<<"Average high: "<<fixed<<setprecision(3)<<avgTemperature(maxTemperatures[i], numOfReadings[i])<<" Average low: "<<fixed<<setprecision(3)<<avgTemperature(minTemperatures[i], numOfReadings[i])<<endl;

//9. determine and display the median high and low temperatures (will require sorting the temperatures - use bubblesort)   
if (numOfReadings[i]%2==0)
   cout<<"Median high: "<<medTemperature(maxTemperatures[i], numOfReadings[i])<<" Median low: "<<medTemperature(minTemperatures[i], numOfReadings[i])<<endl;
else
   cout<<"Median high: "<<fixed<<setprecision(1)<<medTemperature(maxTemperatures[i], numOfReadings[i])<<" Median low: "<<fixed<<setprecision(1)<<medTemperature(minTemperatures[i], numOfReadings[i])<<endl;  
//10. median should be displayed as an integer if there are an odd number of data sets   
//11. median should be displayed with 1 digit to the right of the decimal if there are an even number of data sets.

//12. determine and display the standard deviation of the high and low temperatures (with 3 digits to the right of the decimal) for the month
   cout<<"Std Dev high: "<<fixed<<setprecision(1)<<stdTemperature(maxTemperatures[i], numOfReadings[i])<<" Std Dev low: "<<fixed<<setprecision(1)<<stdTemperature(minTemperatures[i], numOfReadings[i])<<endl;

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote