Escriba un programa que le permita al usuario ingresar la precipitación total para cada uno de los 12 meses en una matriz de dobles.
IDIOMA: C ++
DESAFÍO:
Escriba un programa que le permita al usuario ingresar la precipitación total para cada uno de los 12 meses en una matriz de dobles.
El programa debe calcular y mostrar la precipitación total del año, la precipitación mensual promedio y los meses con las cantidades más altas y más bajas.
SOLUTION:
#include <iostream> #include <string> using namespace std; int main(int argc, char *argv[]){ const int TOTALMONTHS = 12; double highest, lowest, getAverage; double total = 0; double rainfall[TOTALMONTHS]; string months[TOTALMONTHS] = { "January", "February", "March", "April","May", "June", "July", "August", "September","October", "November", "December" }; for ( int month = 0; month < TOTALMONTHS; month++ ){ cout << " Enter rainfall for " << months[month] << ": "; cin >> rainfall[month]; total += rainfall[month]; while (rainfall[month] < 0){ cout << "\nRainfall must be zero or more per month..."; cout << "\nPlease enter positive amount for " << months[month] << " again: "; cin >> rainfall[month]; total += rainfall[month]; } } cout << "Total rainfall: \t" << total << endl; getAverage = total / TOTALMONTHS; cout << "Average rainfall: " << getAverage << endl; string maxMonth, minMonth; for ( int month = 0; month < TOTALMONTHS; month++ ){ highest = rainfall[0]; for ( int count = 0; count < TOTALMONTHS; count++ ){ if ( rainfall[count] > highest ){ highest = rainfall[count]; maxMonth = months[count]; } } lowest = rainfall[0]; minMonth = months[0]; for ( int count = 0; count < TOTALMONTHS; count++ ){ if ( rainfall[count] < lowest ){ lowest = rainfall[count]; minMonth = months[count]; } } } cout << "Least rainfall in " << minMonth << endl; cout << "Most rainfall in " << maxMonth << endl; return 0; }
Posted in Aprender a codificar, C ++