School-Coding-Cpp/sfusi/trovaPosizioneMassimo.cpp

37 lines
834 B
C++

//Patriche Robert Cosmin
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int trovaPosizioneMassimo(int arr[], int size);
int main() {
const int DIM = 15;
int numeri[DIM];
srand(time(0));
cout << "Array: ";
for (int i = 0; i < DIM; i++) {
numeri[i] = rand() % 100 + 1;
cout << numeri[i] << " ";
}
cout << endl;
int posizione = trovaPosizioneMassimo(numeri, DIM);
cout << "La posizione del valore massimo è: " << posizione << endl;
return 0;
}
int trovaPosizioneMassimo(int arr[], int size) {
int massimo = arr[0], posizione = 0;
for (int i = 1; i < size; i++) {
if (arr[i] > massimo) {
massimo = arr[i];
posizione = i;
}
}
return posizione;
}