//------------------------------------------------- // Jordi Binefa i Martínez // dubtes@excite.com // http://members.juara.com/asi //------------------------------------------------- //Write the program ARRAY1.CPP, that will be used to //calculate the average temperature of n towns. First //of all, the program asks how many towns you want to //work with (n). Then it asks every value of the //temperatures (temp[i]=_ ) , been i a number between //0 and n-1. Finally the program calculates the average //temperature and displays it. Supose a maximum number //of 20 towns. //Fes el programa MATRIU1.CPP, que serveix per calcular //la temperatura mitja de n poblacions. Primer de //tot el programa et demana quantes poblacions vols //entrar (n). Després et pregunta una per una (temp[i]=_) //les temperatures, essent i un número que va des de 0 a //n-1. Finalment el programa calcula la temperatura //mitja i la presenta per pantalla. Suposa que com a màxim //poden haver-hi 20 poblacions. #include #define MAX_TOWNS 20 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, int pobls){ int i; for(i = 0 ; i < pobls ; i++){ cout << "\n Temperature of the town[" << i << "] = "; cin >> t[i]; } } float doAverage(float *tem, int pobls){ int i; float mitjana = 0.0; for(i = 0 ; i < pobls ; i++) mitjana += tem[i]; return(mitjana/(float)pobls); // This is a type conversion } void main(){ float temperature[MAX_TOWNS]; int towns; towns = howManyTowns( MAX_TOWNS ); entryTemp( temperature , towns ); cout << "\n\nThe average temperature of all the towns is " << doAverage( temperature , towns) << " degrees.\n\n"; }