Grupo
Arrays y Vectores en C++
Ojetivo
1. Defina un array con varios valores enteros.
2. Implemente una función que itere sobre el array.
3. Compare cada elemento del array con el número mayor actual.
4. Actualice el número mayor cada vez que encuentre un valor mayor.
5. Después del bucle, devuelva o imprima el número mayor.
6. Pruebe el programa con diferentes arrays para garantizar su precisión.
Implemente una función que encuentre el número mayor en un array.
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 largest number in the array
int findLargestNumber(int arr[], int size) {
int largest = arr[0]; // Assume the first element is the largest
// Loop through the array starting from the second element
for (int i = 1; i < size; i++) {
if (arr[i] > largest) { // If the current element is larger than the current largest
largest = arr[i]; // Update the largest number
}
}
return largest; // Return the largest number found
}
int main() {
// Define an array of numbers
int numbers[] = {15, 22, 4, 67, 30, 90, 12};
int size = sizeof(numbers) / sizeof(numbers[0]); // Calculate the size of the array
// Call the function to find the largest number
int largest = findLargestNumber(numbers, size);
// Output the largest number
cout << "The largest number in the array is: " << largest << endl;
return 0; // End of program
}
Salida
The largest number in the array is: 90
Comparte este ejercicio C++