Max element in an array with the index in c++
#include <iostream>
int main() {
const int size = 5;
int arr[size] = {10, 5, 8, 3, 15};
int maxElement = arr[0];
int maxIndex = 0;
for (int i = 1; i < size; ++i) {
if (arr[i] > maxElement) {
maxElement = arr[i];
maxIndex = i;
}
}
std::cout << "The maximum element in the array is: " << maxElement << std::endl;
std::cout << "The index of the maximum element is: " << maxIndex << std::endl;
return 0;
}