Program c++ Write an application that uses a two-dimensional dynamic array to st
ID: 3557737 • Letter: P
Question
Program c++
Write an application that uses a two-dimensional dynamic array to store daily temperatures for as many weeks as requested by the user. The application should display all daily. Use a user-defined function as handler of memory allocation errors. The user-defined function should be registered as a default handler of new failures temperatures as well as average weekly temperatures in a table-like format. The application should provide the following menu options to the user
Create a file with the temperatures
Add a temperature to the existing file
Display the temperatures
Delete a temperatures from the file
Update (edit) a temperature.
Sort temperatures by a range (define the range for them as hot, nice, cold) .
Design the main( ) function to prompt the user to enter two vectors in either polar or rectangular coordinates (users choice). It should then display a menu with the options to add, subtract, multiply, or divide vectors and prompt the user to choose an option. After the user has chosen an operation to be performed, main( ) should call appropriate operator functions to compute and output the result of the operation.
Rubrics
The application must execute correctly (no compilation and execution errors).
The application must have all requirement defined above.
The application must be developed in C++ programming language.
must submit all sources files created (files with extension .cpp).
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
const int months = 12;
void getData(double [][ 2 ], int);
double averageHigh(double [] [ 2 ], int);
double averageLow(double [] [ 2 ], int);
int indexHighTemp(double [] [ 2 ], int);
int indexLowTemp(double [] [ 2 ], int);
int main()
{
double temperatures[months][2];
getData(temperatures, months);
cout << " The average high temp. for the year: "
<< averageHigh(temperatures, months) << endl;
cout << " The average low temp. for the year: "
<< averageLow(temperatures, months) << endl;
cout << " Index of highest temp. for the year: "
<< indexHighTemp(temperatures, months) << endl;
cout << " Index of lowest temp. for the year: "
<< indexLowTemp(temperatures, months) << endl;
system("PAUSE");
return 0;
}
void getData(double t[][2], int m)
{
int i;
ifstream inFile;
ofstream outFile;
inFile.open("tempsFile.txt");
if (!inFile)
{
cout << "Cannot open input file."
<< endl;
}
for (int i=0; i<m; i++)
inFile >> t[i][0];
cout << endl;
inFile >> t[i][1];
cout << endl;
}
double averageHigh(double t[] [2], int m)
{
double sum = 0;
for (int i=0; i<m; i++)
sum += t[i][0];
return (sum/m);
}
double averageLow(double t[][2], int m)
{
double sum = 0;
for (int i=0; i<m; i++)
sum += t[i][1];
return (sum/m);
}
int indexHighTemp(double t[][2], int m)
{
int ind = 0;
double highest = t[0][0];
for (int i=1; i<m; i++)
if (t[i][0] > highest)
{
highest = t[i][0];
ind = i;
}
return ind;
}
int indexLowTemp(double t[][2], int m)
{
int ind = 0;
double lowest = t[0][1];
for (int i=1; i<m; i++)
if (t[i][1] < lowest)
{
lowest = t[i][1];
ind = i;
}
return ind;
}
Another Example
#include<iostream>
#include<string>
using namespace std;
struct WeatherStation {
string StationDesignation;
double Temperature;
};
int i(0);
double Calc(double fahren);
double Total(0),Celsius[5],Fahrenheit[5];
double CelsiusLowTemperature(0), FahrenheitLowTemperature(1000),
CelsiusHighTemperature(0),
FahrenheitHighTemperature(-1000);
double Mean_Celsius, Mean_Fahrenheit;
void ShowList()
{
cout<<"Command Choices................"<<" "<<endl;
cout<<"_____________________________"<<" "<<endl;
cout<<" "<<"Post Temperatures" <<" "<<endl;
cout<<" "<<"Daily Report" <<" "<<endl;
cout<<" "<<"High-Low Report" <<" " <<endl;
cout<<" "<<"Quit" <<" "<<endl;
cout<<"_____________________________"<<" "<<endl;
}
void PostTemperatures()
{
cout<< "Please enter reported temperatures..."<<" "<< " ";
cout<< "Weather Station Big Basin: ";
cin>>Fahrenheit[i];
cout<< "Weather Station Foothill: ";
cin>>Fahrenheit[i];
cout<< "Weather Station DeAnza: ";
cin>>Fahrenheit[i];
cout<< "Weather Station MiddleField: ";
cin>>Fahrenheit[i];
cout<< "Weather Station Redwood City: ";
cin>>Fahrenheit[i];
cout<<" "<<" "
<<"----------------------------------------------"<<endl;
}
void DailyReport()
{
cout<<" "<< "NGS Daily Temperature Report"<<endl;
cout<<"================================================="<<endl;
cout<<" Fahrenheit Celsius "<<endl;
cout<<" --------------------------------------------------------------"<<endl;
}
void HighLowReport()
{
cout<< "===========NGS Temperature Data Report========="<<endl;
//cout<<"Lowest Temperature: "<<FahrenheitLowTemperature<<" "<<CelsiusLowTemperature<<endl;
cout<<" --------------------------------------------------------------"<<endl;
//cout<<"Highest Temperature: "<<FahrenheitHighTemperature<<" "<<CelsiusHighTemperature<<endl;
cout<<" --------------------------------------------------------------"<<endl;
}
void Quit()
{
cout<<"===============End Temperature Data Report============="<<endl;
}
int main()
{
ShowList();
PostTemperatures();
for(i=0;i<5;i++) //Loops
{
cout<<"Weather Station "<<i+1<<" = ";// Takes input for Reported Temperatures For Weather Stations...
cin>>Fahrenheit[i];
if(Fahrenheit[i]<FahrenheitLowTemperature) // Conditionals
FahrenheitLowTemperature=Fahrenheit[i];
if(Fahrenheit[i]>FahrenheitHighTemperature)
FahrenheitHighTemperature=Fahrenheit[i];
Celsius[i] = Calc(Fahrenheit[i]);
Total+=Fahrenheit[i];
}
CelsiusLowTemperature = Calc(FahrenheitLowTemperature);
CelsiusHighTemperature = Calc(FahrenheitHighTemperature);
Mean_Fahrenheit = Total/5.0; // Calculates the mean for Fahrenheit
Mean_Celsius = Calc(Mean_Fahrenheit); // Calculates the mean for Celsius
cout.precision(3);
cout<<" ========NGS Temperature Data Report======== "<<endl;
cout<<" Fahrenheit Celsius "<<endl;
cout<<" --------------------------------------------------------------"<<endl;
cout<<"Lowest Temperature: "<<FahrenheitLowTemperature<<" "<<CelsiusLowTemperature<<endl;
cout<<" --------------------------------------------------------------"<<endl;
cout<<"Highest Temperature: "<<FahrenheitHighTemperature<<" "<<CelsiusHighTemperature<<endl;
cout<<" --------------------------------------------------------------"<<endl;
cout<<"Mean Temperature: "<<Mean_Fahrenheit<<" "<<Mean_Celsius<<endl;
cout<<" --------------------------------------------------------------"<<endl;
cout<<"Raw Data..."<<endl;
cout<<" ";
// Outputs Data that include the following: Fahrenheit Low and High, Celsius Low and High and Mean...
for(i=0;i<5;i++) // Loops
{
cout.precision(3);
cout<<"WeatherStation "<< i <<" = "<<Fahrenheit[i]<<" "<<Celsius[i]<<endl;
}
cout<<" --------------------------------------------------------------"<<endl;
cout<<" =============End Temperature Data Report============="<<endl;
}
double Calc(double fah)
{
double cel; // Converts Fahrenheit to Celsius
cel = (5 *(fah - 32))/9.0;
return cel;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.