该如何去使用c++数组编写程序?

编写一个程序,使用一个二维数组来存储一年中每月的最高和最低温度。程序应该输出该年的平均最高温度、平均最低温度以及该年的最高温度和最低温度。程序必须包含下面的函数:

A. 函数getData():该函数将数据读入并存储到二维数组中。

B. 函数averageHigh():该函数计算并返回该年的平均高温。

C. 函数averageLow():该函数计算并返回该年的平均低温。

D. 函数indexHighTemp():该函数返回数组中最高温度的下标。

E. 函数indexLowTemp():该函数返回数组中最低温度的下标。

一只萌萌小番薯
浏览 308回答 2
2回答

LEATH

#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) &nbsp;{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];}}

千万里不及你

// 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;}
打开App,查看更多内容
随时随地看视频慕课网APP