Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <iostream>
  2. #define MAX 20
  3. using namespace std;
  4.  
  5. typedef struct {
  6.     char gripe;
  7.     char tos;
  8.     int peso;
  9. } t_rpta;
  10.  
  11. void genera_datos(t_rpta *respuestas, int n) {
  12.     for (int i = 0; i < n; i++) {
  13.         //gripe
  14.         if (rand() % 2)
  15.             respuestas[i].gripe = 'S';
  16.         else
  17.             respuestas[i].gripe = 'N';
  18.         //tos
  19.         if (rand() % 2)
  20.             respuestas[i].tos = 'S';
  21.         else
  22.             respuestas[i].tos = 'N';
  23.         //generar un valor entre 30..60 inclusive
  24.         respuestas[i].peso = (rand() % 31) + 30;
  25.  
  26.     }
  27. }
  28.  
  29. float porcentaje_tos_gripe(t_rpta *respuestas, int n) {
  30.     int cantidad = 0;
  31.     for (int i = 0; i < n; i++) {
  32.         if (respuestas[i].gripe == 'S' && respuestas[i].tos == 'S') {
  33.             cantidad++;
  34.         }
  35.     }
  36.     return ((float)cantidad) / n;
  37. }
  38.  
  39. float promedio_peso(t_rpta *respuestas, int n) {
  40.     int cantidad = 0;
  41.     float peso_acc = 0.0;
  42.     for (int i = 0; i < n; i++) {
  43.         if (respuestas[i].gripe == 'S' || respuestas[i].tos == 'S') {
  44.             cantidad++;
  45.             peso_acc += respuestas[i].peso;
  46.         }
  47.     }
  48.     return peso_acc/cantidad;
  49. }
  50.  
  51.  
  52. void muestra_datos(t_rpta *respuestas, int n) {
  53.     for (int i = 0; i < n; i++) {
  54.         cout << "*** PACIENTE " << i + 1 << " ***" << endl;
  55.         cout << "Gripe?: " << respuestas[i].gripe << endl;
  56.         cout << "Tos?: " << respuestas[i].tos << endl;
  57.         cout << "Peso: " << respuestas[i].peso << endl;
  58.  
  59.     }
  60. }
  61. void main() {
  62.     int n;
  63.     cout << "N:";
  64.     do {
  65.         cin >> n;
  66.     } while (n < 1 || n>MAX);
  67.  
  68.     //Reservar espacio de memoria
  69.     t_rpta *respuestas;
  70.     respuestas = new t_rpta[n];
  71.  
  72.     genera_datos(respuestas, n);
  73.     muestra_datos(respuestas, n);
  74.     cout << "Tos y gripe: " << porcentaje_tos_gripe(respuestas, n)*100 << "%" << endl;
  75.     cout << "Promedio peso: " << promedio_peso(respuestas, n)  << " Kg."<< endl;
  76.     system("pause");
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement