Grupo
Funciones en C++
Ojetivo
1. Crear una función que reciba un array y su tamaño como parámetros.
2. Inicializar una variable para almacenar el valor mínimo (comenzando con el primer elemento).
3. Iterar por el array y comparar cada elemento con el mínimo actual.
4. Actualizar el mínimo si se encuentra un valor menor.
5. Devolver el valor mínimo final.
Implementar una función que devuelva el valor más pequeño de una lista de números.
Ejemplo de Código C++
Mostrar Código C++
#include <iostream> // Include iostream for input and output
using namespace std; // Use the standard namespace
// Function to find the minimum value in an array
int findMinimum(int arr[], int size) {
int min = arr[0]; // Assume the first element is the minimum
// Loop through the array starting from the second element
for (int i = 1; i < size; i++) {
// If a smaller element is found, update the minimum
if (arr[i] < min) {
min = arr[i];
}
}
return min; // Return the smallest value
}
int main() {
// Define an array of numbers
int numbers[] = {42, 17, 23, 8, 15, 34};
int size = sizeof(numbers) / sizeof(numbers[0]); // Calculate the array size
// Call the function and store the result
int minimum = findMinimum(numbers, size);
// Display the smallest number in the array
cout << "The smallest number in the list is: " << minimum << endl;
return 0; // End of program
}
Salida
The smallest number in the list is: 8
Comparte este ejercicio C++