Skip to content

Instantly share code, notes, and snippets.

@diazvictor
Created June 15, 2024 02:07
Show Gist options
  • Save diazvictor/0536803f956f434c396538381b09b9e8 to your computer and use it in GitHub Desktop.
Save diazvictor/0536803f956f434c396538381b09b9e8 to your computer and use it in GitHub Desktop.
Análisis de voltajes en un arreglo bidimensional en C++
#include <iostream>
using namespace std;
int main() {
// Definir el tamaño del arreglo bidimensional
const int filas = 3;
const int columnas = 5;
// Crear el arreglo bidimensional
float voltajes[filas][columnas];
// Introducir los valores del arreglo de forma interactiva
for (int i = 0; i < filas; ++i) {
for (int j = 0; j < columnas; ++j) {
cout << "Ingrese el voltaje en la fila " << i + 1 << ", columna " << j + 1 << ": ";
cin >> voltajes[i][j];
// Validar la entrada
while (voltajes[i][j] < 0) {
cout << "Valor inválido. Ingrese un voltaje real no negativo: ";
cin >> voltajes[i][j];
}
}
}
// Contar voltajes en cada rango
int conteoRango1 = 0;
int conteoRango2 = 0;
int conteoRango3 = 0;
int conteoRango4 = 0;
int conteoRango5 = 0;
for (int i = 0; i < filas; ++i) {
for (int j = 0; j < columnas; ++j) {
if (voltajes[i][j] < 60) {
++conteoRango1;
} else if (voltajes[i][j] >= 60 && voltajes[i][j] < 70) {
++conteoRango2;
} else if (voltajes[i][j] >= 70 && voltajes[i][j] < 80) {
++conteoRango3;
} else if (voltajes[i][j] >= 80 && voltajes[i][j] < 90) {
++conteoRango4;
} else {
++conteoRango5;
}
}
}
// Mostrar resultados
cout << "\nResultados:\n";
cout << "Voltajes menores que 60: " << conteoRango1 << endl;
cout << "Voltajes entre 60 y 70 (inclusive): " << conteoRango2 << endl;
cout << "Voltajes entre 70 y 80 (inclusive): " << conteoRango3 << endl;
cout << "Voltajes entre 80 y 90 (inclusive): " << conteoRango4 << endl;
cout << "Voltajes mayores o iguales a 90: " << conteoRango5 << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment