112713 I am getting error. Could you please correct the codes for the following
ID: 3547374 • Letter: 1
Question
112713
I am getting error. Could you please correct the codes for the following problem?:
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions:
a. Function getData: This function reads and stores data in the twodimensional array.
b. Function averageHigh: This function calculates and returns the average high temperature for the year.
c. Function averageLow: This function calculates and returns the average low temperature for the year.
d. Function indexHighTemp: This function returns the index of the highest high temperature in the array.
e. Function indexLowTemp: This function returns the index of the lowest low temperature in the array.
These functions must all have the appropriate parameters.)
#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;
}
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];
inFile >> t[i][1];
}
}
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.