0

This is the code I'm currently using, in C++.

#include <iostream>
using namespace std;

struct Evento {
  string nombre;
  int frecuencia;
  double porcentajeMensual;
  double porcentajeAcumulado;
};

void capturarCantidadEventos(int& cantidad) {
  do {
    cout << "Ingrese la cantidad de eventos observados: ";
    cin >> cantidad;
  } while (cantidad <= 0);
}

void capturarFrecuencias(Evento eventos[], int cantidad) {
  for (int i = 0; i < cantidad; i++) {
    cout << "Ingrese el nombre del evento " << i + 1 << ": ";
    cin >> eventos[i].nombre;

    do {
      cout << "Ingrese la frecuencia absoluta observada en el evento " << i + 1
           << ": ";
      cin >> eventos[i].frecuencia;
    } while (eventos[i].frecuencia <= 0);
  }
}

void calcularPorcentajes(Evento eventos[], int cantidad) {
  int totalFrecuencia = 0;
  for (int i = 0; i < cantidad; i++) {
    totalFrecuencia += eventos[i].frecuencia;
  }

  double porcentajeAcumulado = 0.0;
  for (int i = 0; i < cantidad; i++) {
    eventos[i].porcentajeMensual =
        (eventos[i].frecuencia / static_cast<double>(totalFrecuencia)) * 100.0;
    porcentajeAcumulado += eventos[i].porcentajeMensual;
    eventos[i].porcentajeAcumulado = porcentajeAcumulado;
  }
}

void imprimirTabla(Evento eventos[], int cantidad) {
  cout << "Evento\tFrecuencia\tPorcentaje Mensual\tPorcentaje Acumulado\n";
  for (int i = 0; i < cantidad; i++) {
    cout << eventos[i].nombre << "\t" << eventos[i].frecuencia << "\t\t"
         << eventos[i].porcentajeMensual << "\t\t\t"
         << eventos[i].porcentajeAcumulado << "\n";
  }
}

int main() {
  int cantidadEventos;
  capturarCantidadEventos(cantidadEventos);

  Evento eventos[cantidadEventos];
  capturarFrecuencias(eventos, cantidadEventos);

  calcularPorcentajes(eventos, cantidadEventos);

  imprimirTabla(eventos, cantidadEventos);

  return 0;
}

I tried an Online Compiler, and it worked fine but when I tried to compile in Zinjal to be able to transform it into a Diagram it gives me an error which is

"ISO C++ forbids variable length array 'eventos' (-wvla) in line 58 which is

Evento eventos[cantidadEventos];

Not fluent in this language or in programming at all, just learning the basics.

273K
  • 29,503
  • 10
  • 41
  • 64
  • 3
    The VLA is `Evento eventos[cantidadEventos];` because `cantidadEventos` is a non-const value determined at runtime, not compile time. You can use a `std::vector` instead, and pass `eventos.data()` as the array parameter to the functions expecting it (_e.g._ `capturarFrecuencias(eventos.data(), cantidadEventos);`). Or, just rewrite those functions to accept a `std::vector&` parameter, and do away with the size parameter. Basically your code at the moment is using very much C-style data handling with C++ I/O. Recommend you write C++ everywhere. – paddy Mar 16 '23 at 03:37
  • [Start with this](https://godbolt.org/z/sPefaj38c). It was unfortunate that you initially used a compiler that by default, lets you use syntax that is not actual C++. – PaulMcKenzie Mar 16 '23 at 04:31

0 Answers0