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.