2 回答
TA贡献1936条经验 获得超6个赞
#include<iostream>
using namespace std;
void getData(double tem[][2]);
double averageHigh(double tempData[][2]);
double averageLow(double tempData[][2]);
int indexHighTemp(double tempData[][2]);
int indexLowTemp(double tempData[][2]);
int main(){
double tempData[12][2];
getData(tempData);
double avgHigh = averageHigh(tempData);
double avgLow = averageLow(tempData);
int indexHigh = indexHighTemp(tempData);
int indexLow= indexLowTemp(tempData);
cout<<"average high temperature:"<<avgHigh<<endl;
cout<<"average low temperature:"<<avgLow<<endl;
cout<<"index of high temperature:"<<indexHigh<<endl;
cout<<"index of low temperature:"<<indexLow<<endl;
return 0;
}
int indexLowTemp(double tempData[][2]){
int index = 0;
double temp = tempData[0][0];
for (int i = 0; i < 12; i++) {
if (tempData[i][0]<temp) {
temp = tempData[i][0];
index = i;
}
}
return index;
}
int indexHighTemp(double tempData[][2] ){
int index = 0;
double temp = tempData[0][1];
for(int i = 0; i< 12; i++) {
if ( tempData[i][1]>temp) {
temp = tempData[i][1];
index = i;
}
}
return index;
}
double averageHigh(double tempData[][2] ){
double avg = 0.0;
for(int i=0; i < 12; i++){
avg += tempData[i][1];
}
return avg/12.0;
}
double averageLow(double tempData[][2] ){
double avg = 0.0;
for(int i=0; i < 12; i++) {
avg += tempData[i][0];
}
return avg/12.0;
}
void getData(double tempData[][2]){
for (int j = 0; j < 12; j++)
{
cin>>tempData[j][0]>>tempData[j][1];
}
}
TA贡献1784条经验 获得超9个赞
// hpp
#ifndef __TEMP__
#define __TEMP__
class Data
{
public:
Data();
~Data();
void getData();
double averageHigh();
double averageLow();
int indexHighTemp();
int indexLowTemp();
private:
double t[12][2];
};
#endif
// cpp
#include "Temp.hpp"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
Data::Data(){}
Data::~Data(){}
void Data::getData()
{
double maxt, mint;
for(int m=0; m<12; ++m)
{
cout<<"Month: "<<m+1<<endl;
cout<<"High temp: \n";
cin>>maxt;
cout<<"Low temp: \n";
cin>>mint;
t[m][0] = (maxt > mint ? maxt : mint);
t[m][1] = (maxt < mint ? maxt : mint);
}
}
double Data::averageHigh()
{
double res = 0.;
for(int m=0; m<12; ++m)
{
res += t[m][0];
}
return res /= 12.;
}
double Data::averageLow()
{
double res = 0.;
for(int m=0; m<12; ++m)
{
res += t[m][1];
}
return res /= 12.;
}
int Data::indexHighTemp()
{
int i;
double maxt = -1e9;
for(int m=0; m<12; ++m)
{
if(t[m][0] > maxt)
{
i = m;
maxt = t[m][0];
}
}
return i;
}
int Data::indexLowTemp()
{
int i;
double mint = 1e9;
for(int m=0; m<12; ++m)
{
if(t[m][1] < mint)
{
i = m;
mint = t[m][1];
}
}
return i;
}
//main
#include "Temp.hpp"
#include <iostream>
using std::cout;
using std::endl;
int main()
{
Data d;
d.getData();
cout<<"Average High Temp: "<<d.averageHigh()<<endl;
cout<<"Average Low Temp: "<<d.averageLow()<<endl;
cout<<"Month of maximal temp: "<<d.indexHighTemp()+1<<endl;
cout<<"Month of minimal temp: "<<d.indexLowTemp()+1<<endl;
return 0;
}
- 2 回答
- 0 关注
- 304 浏览
添加回答
举报