Advertisement
rihardmarius

final 1b - temperatura promedio

Nov 24th, 2013
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include "libreria archivos.cpp"
  2. #include <sstream>
  3. #define LAT 181
  4. #define LON 361
  5.  
  6. using namespace std;
  7.  
  8. struct par {
  9.     int cantidad = 0;
  10.     double promedio = 0;
  11. };
  12.  
  13. struct file_t {
  14.     int segundo = 0;
  15.     int latitud = 0;
  16.     int longitud = 0;
  17.     double temperatura = 0;
  18. };
  19.  
  20. void generar_matriz (string nombre, array<array<par,LON>,LAT>& arr)
  21. {
  22.     file_t r;
  23.     ifstream file (nombre, ios::binary);
  24.     check_open(file);
  25.  
  26.     while (readblock(file,r))
  27.     {
  28.         arr.at(r.latitud+90).at(r.longitud+180).promedio += r.temperatura;
  29.         arr.at(r.latitud+90).at(r.longitud+180).cantidad++;
  30.     }
  31.     for (int i=0; i<LAT; i++)
  32.         for (int j=0; j<LON; j++)
  33.             if (arr.at(i).at(j).cantidad != 0)
  34.                 arr.at(i).at(j).promedio /= arr.at(i).at(j).cantidad;
  35. }
  36.  
  37. /*
  38. void generar_matriz (ifstream& file, array<array<par,LON>,LAT>& arr)
  39. {
  40.     file_t r;
  41.     while (readblock(file,r))
  42.     {
  43.         arr.at(r.latitud+90).at(r.longitud+180).promedio += r.temperatura;
  44.         arr.at(r.latitud+90).at(r.longitud+180).cantidad++;
  45.     }
  46.     for (int i=0; i<LAT; i++)
  47.         for (int j=0; j<LON; j++)
  48.             if (arr.at(i).at(j).cantidad != 0)
  49.                 arr.at(i).at(j).promedio /= arr.at(i).at(j).cantidad;
  50. }
  51. */
  52. void mostrar_matriz (array<array<par,LON>,LAT>& arr)
  53. {
  54.     for (int i=0; i<LAT; i++)
  55.     {
  56.         for (int j=0; j<LON; j++)
  57.             cout << arr.at(i).at(j).cantidad << ' ' << arr.at(i).at(j).promedio << "  ";
  58.         cout << endl;
  59.     }
  60.     cout << endl;
  61. }
  62.  
  63. double promedio_de_region (array<array<par,LON>,LAT>& arr, int lat1, int lon1, int lat2, int lon2)
  64. {
  65.     double acumulador = 0; int c = 0;
  66.     for (int i=lat1+90; i<lat2+91; i++)
  67.         for (int j=lon1+180; j<lon2+181; j++)
  68.         {
  69.             if (arr.at(i).at(j).cantidad != 0)
  70.             {
  71.                 acumulador += arr.at(i).at(j).promedio;
  72.                 c++;
  73.             }
  74.         }
  75.     if (c != 0)
  76.         return acumulador/c;
  77.     else
  78.         return 0;
  79. }
  80.  
  81. int main()
  82. {
  83.     array<array<par,LON>,LAT> arr;
  84.     stringstream ss; string nombre;
  85.     ss << "medidas.bin";
  86.     //ifstream input ("../final 1b - crear archivo/medidas.bin", ios::binary);
  87.  
  88.     cout << "Ingrese el nombre del archivo: ";
  89.     ss >> nombre;
  90.     cout << nombre << endl << endl;
  91.  
  92.     generar_matriz (nombre, arr);
  93.  
  94.     cout << "El promedio de la region es: " << promedio_de_region(arr,-50,0,49,0) << endl;
  95.  
  96.     //mostrar_matriz(arr);
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement