Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define MAX 20
- using namespace std;
- typedef struct {
- char gripe;
- char tos;
- int peso;
- } t_rpta;
- void genera_datos(t_rpta *respuestas, int n) {
- for (int i = 0; i < n; i++) {
- //gripe
- if (rand() % 2)
- respuestas[i].gripe = 'S';
- else
- respuestas[i].gripe = 'N';
- //tos
- if (rand() % 2)
- respuestas[i].tos = 'S';
- else
- respuestas[i].tos = 'N';
- //generar un valor entre 30..60 inclusive
- respuestas[i].peso = (rand() % 31) + 30;
- }
- }
- float porcentaje_tos_gripe(t_rpta *respuestas, int n) {
- int cantidad = 0;
- for (int i = 0; i < n; i++) {
- if (respuestas[i].gripe == 'S' && respuestas[i].tos == 'S') {
- cantidad++;
- }
- }
- return ((float)cantidad) / n;
- }
- float promedio_peso(t_rpta *respuestas, int n) {
- int cantidad = 0;
- float peso_acc = 0.0;
- for (int i = 0; i < n; i++) {
- if (respuestas[i].gripe == 'S' || respuestas[i].tos == 'S') {
- cantidad++;
- peso_acc += respuestas[i].peso;
- }
- }
- return peso_acc/cantidad;
- }
- void muestra_datos(t_rpta *respuestas, int n) {
- for (int i = 0; i < n; i++) {
- cout << "*** PACIENTE " << i + 1 << " ***" << endl;
- cout << "Gripe?: " << respuestas[i].gripe << endl;
- cout << "Tos?: " << respuestas[i].tos << endl;
- cout << "Peso: " << respuestas[i].peso << endl;
- }
- }
- void main() {
- int n;
- cout << "N:";
- do {
- cin >> n;
- } while (n < 1 || n>MAX);
- //Reservar espacio de memoria
- t_rpta *respuestas;
- respuestas = new t_rpta[n];
- genera_datos(respuestas, n);
- muestra_datos(respuestas, n);
- cout << "Tos y gripe: " << porcentaje_tos_gripe(respuestas, n)*100 << "%" << endl;
- cout << "Promedio peso: " << promedio_peso(respuestas, n) << " Kg."<< endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement