查看: 839|回复: 4
|
C++的问题。。需要帮忙。。。
[复制链接]
|
|
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 highaverage low,highest and lowest temperatures of the year.Your program must consist of the following functions:
a)FUNCTION getData:this function reads and stores data in the two-dimensional array.
b)FUNCTION averageHigh:this function calculates and returns the average high temperature of the year.
c)FUNCTION averageLow:this function calculates and returns the average low temperature of 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 tempperature in the array. |
|
|
|
|
|
|
|
楼主 |
发表于 19-3-2006 07:02 PM
|
显示全部楼层
这是我已做了的。。可是我不会做AVERAGE的PART。。。请教各位教教我。。。谢谢。。。
#include <iostream>
#include <iomanip>
using namespace std;
const int MONTHS = 12; // number of months
const int TEMPS = 2; // number of temperatures
int Minimum( int [ ][TEMPS], int, int );
int Maximum(int [ ][TEMPS], int, int );
float Average( int [ ], int);
void PrintArray( int [ ][TEMPS], int, int );
void main(){
//declare and initialize array
int monthtemp[MONTHS][TEMPS] =
{ { 21, 30 },
{ 28, 29 },
{ 27, 28 },
{ 26, 27 },
{ 37, 28 },
{ 36, 37 },
{ 27, 28 },
{ 26, 37 },
{ 25, 26 },
{ 33, 31 },
{ 29, 30 },
{ 32, 24 } };
cout << "The array is: "<<endl;
PrintArray(monthtemp, MONTHS, TEMPS);
cout << "\n\nLowest temperatures of the year: "
<< indexLowTemp(monthtemp, MONTHS, TEMPS)
<< "\nHighest temperatures of the year: "
<< indexHighTemp(monthtemp, MONTHS, TEMPS) << endl;
for (int month = 0; month < MONTHS; month++){
cout << "The average high temp for the year is:";
cout << "The average low temp for the year is:";
}
// Find the minimum temperatures
int indexLowTemp(int temperatures[ ][TEMPS], int mths, int tmp){
int lowTemp = 50;
for (int i = 0; i < mths; i++)
for (int j = 0; j < tmp; j++)
if (temperatures[i][j] < lowTemp)
lowTemp = temperatures[i][j];
return lowTemp;
}
// Find the maximum temperatures
int indexHighTemp( int temperatures[ ][TEMPS], int mths, int tmp ){
int highTemp = 0;
for (int i = 0; i < mths; i++)
for (int j = 0; j < tmps; j++)
if (temperatures[i][j] > highTemp)
highTemp = temperatures[i][j];
return highGrade;
}
// Determine the average temperatures for a particular month
float Average(int setOfTemp[], int tmp){
int total = 0;
for ( int i = 0; i < tmp; i++ )
total += setOfTemp[i];
return float(total)/tmp;
}
// Print the array
void PrintArray(int temperatures[][TEMPS], int mths, int tmp){
cout << " [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]";
for ( int i = 0; i < mths; i++ ) {
cout << "\n monthtemp[" << i << "] ";
for ( int j = 0; j < tmp; j++ )
cout << setiosflags(ios::left) << setw(5) << temperatures[i][j];
}
} |
|
|
|
|
|
|
|
发表于 19-3-2006 08:58 PM
|
显示全部楼层
CONCEPT WORK:
画一个TABLE
m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12
t1
t2
t1就是你的LOW TEMPERATURE。
t2是HIGH TEMPERATURE。
所以, AVERAGE OF HIGHEST TEMPERATURE就是(SUM OF t2)/month。
我真不明白你们怎么看QUESTION?
QUESTION已经说到了,要用TWO DIMENSIONAL ARRAY,就代表TABLE。
TABLE, TABLE, TABLE。
不懂TABLE吗?
你们的DATABASE也是RECORD TABLE。
你完全没有回答到你的问题。
D和E,不是要你找全部DATA最高的数目和最低数目。
反而是要找,t1的最低值和t2的最高值。
连基本的问题都没有仔细看,如果日后你管理一个PROJECT,你会害你公司赔钱。
做出来的软件达不到CLIENT的要求,他有权向你索取赔偿。 |
|
|
|
|
|
|
|
发表于 19-3-2006 10:18 PM
|
显示全部楼层
int mths 和 int tmp 可以不要,因为你有GLOBAL Declare
const int MONTHS = 12; // number of months
const int TEMPS = 2; // number of temperatures
所以可以直接用 MONTHS TEMPS 。
原帖由 轩辕民 于 19-3-2006 08:58 PM 发表
CONCEPT WORK:
画一个TABLE
m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12
t1
t2
t1就是你的LOW TEMPERATURE。
t2是HIGH TEMPERATURE。
所以, AVERAGE OF HIGHEST TEMPERATURE就是(SUM OF t2)/month。
...
他的table 应该是
t1 t2
m1
m2
m3
...
...
m12
row and column 要搞清楚。
row 用 i, column 用j, double for loop to read the two dimensional array。
轩辕民说得对,是要你找t1的最低值和t2的最高值,average t1 和average t2. |
|
|
|
|
|
|
|
楼主 |
发表于 20-3-2006 07:11 PM
|
显示全部楼层
|
|
|
|
|
|
| |
本周最热论坛帖子
|