//------------------------------------------------- // Jordi Binefa i Martínez // dubtes@excite.com // http://members.juara.com/asi //------------------------------------------------- //Write the program ARRAY2.CPP, that will be used to //calculate the average temperature of n towns all the //week. The algorithm is similar to the exercise 7, with //the difference of using a bidimensional array, one //dimension for the days of the week (7 days) and the //other for the towns. The way to ask the temperatures //will be : temp[i][j]=_, been i a number in the range //from 0 to n-1 (town number) and j goes between 1 and //7 (weekday). In other way, it asks first temp[0][1], //after temp[0][2] ... until arrives to temp[0][7] and //after asks temp[1][0]... until arrives to temp[n-1][7]. //At last the program shows the weekly average of the towns. //Fes el programa MATRIU2.CPP, que serveix per calcular //la temperatura mitja de n poblacions al llarg de la //setmana. El mètode de funcionament es similar al de //l’exercici 7, amb la diferència que has de fer servir un //array bidimensional, una dimensió per als dies de la //setmana (7 dies) i un altra per a les poblacions. La //forma de preguntar les temperatures serà : temp[i][j]=_, //essent i un número que va des de 0 a n-1 //(representa el número de població) i j varia entre 1 i 7 //(dia de la setmana). Es a dir, primer ens preguntarà //temp[0][1], després temp[0][2] ... fins que arribem a //temp[0][7] i després preguntarà temp[1][0]... //fins arribar a temp[n-1][7]. Al final el programa ens //presenta la temperatura mitja setmanal de cada població //i la temperatura mitja total. #include #define MAX_TOWNS 20 #define DAYS 7 int howManyTowns(int max){ int num; do{ cout << "\n\nHow many towns ( max : " << max << " )? : "; cin >> num; }while( (num < 1) || (num > max) ); return num; } void entryTemp(float t[][DAYS], int pobls){ int i,j; for( i = 0 ; i < pobls ; i++ ) for( j = 0 ; j < DAYS ; j++){ cout << "\n Temperature of the town[" << i << "] the day " << j+1 << " = "; cin >> t[i][j]; } } float doAverage(float tem[][DAYS], int pobls){ int i,j; float mitjana = 0.0; for( i = 0 ; i < pobls ; i++ ) for ( j = 0 ; j < DAYS ; j++) mitjana += tem[i][j]; return(mitjana/(float)(pobls * DAYS)); } void main(){ float temperature[MAX_TOWNS][DAYS]; int towns; towns = howManyTowns( MAX_TOWNS ); entryTemp( temperature , towns ); cout << "\n\nThe average temperature in all the towns is " << doAverage( temperature , towns) << " degrees.\n\n"; }